many-to-many-relationship between two entities in spring boot

前端 未结 1 1853
独厮守ぢ
独厮守ぢ 2021-01-04 21:31

I have two Entities in my Spring-Boot Application:

User.java

@Entity
public class User {
      @Id
      @GeneratedValue(strategy =          


        
1条回答
  •  -上瘾入骨i
    2021-01-04 22:04

    You can find any tutorial connected with many-to-many relationship using Hibernate/Spring Data, example: Spring Data many-to-many

    With your model it's simple to add the relationship mappings, like this:

    @Entity
    public class Role {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String name;
        private String description;
    
        @ManyToMany(cascade = CascadeType.ALL)
        @JoinTable
        private Set users;
    }
    

    and this:

    @Entity
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String firstname;
        private String lastname;
        private String username;
        private String password;
    
        @ManyToMany(mappedBy = "users")
        private Set roles;
    }
    

    0 讨论(0)
提交回复
热议问题