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

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

I have two Entities in my Spring-Boot Application:

User.java

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


        
相关标签:
1条回答
  • 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<User> 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<Role> roles;
    }
    
    0 讨论(0)
提交回复
热议问题