Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two Entities in my Spring-Boot Application:

User.java

@Entity
public class User {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      Long id;
      String firstname;
      String lastname;
      String username;
      String password;
}

and

Role.java

Entity
@Table(name = "role")
public class Role {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      Long id;
      String name;
      String description;
}

for my MySql database

I have excluded the getter and setter methods for this question.

I want to realise a Many-to-Many-Relationship between both Entities. Every user should be able to assign multiple roles to himself

I already Created a mapping table for both tables in my database. It has the rows

  • user_id
  • role_id.

I also created a new Entity UserRole.java which looks like this:

@Entity
@Table(name = "user_role")
public class UserRole implements Serializable{

    private User user;
    private Role role;

    @Id
    @ManyToOne
    @JoinColumn(name = "user_id")
    public User getuser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Id
    @ManyToOne
    @JoinColumn(name = "role_id")
    public Role getrole(){
        return role;
    }
    public void setRole(Role role){
        this.role = role;
    }

}

Now my question: is this construction correct? If yes, how do i add existing roles to an existing user and get the roles of this user in spring-boot?

like image 244
Beytullah Güneyli Avatar asked Feb 22 '17 14:02

Beytullah Güneyli


People also ask

How do you write a many-to-many relationship in spring boot?

Modeling a many-to-many relationship with POJOs is easy. We should include a Collection in both classes, which contains the elements of the others. After that, we need to mark the class with @Entity and the primary key with @Id to make them proper JPA entities.

What is difference between JpaRepository and CrudRepository?

CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

What is many-to-many relationship JPA?

The Many-To-Many mapping represents a collection-valued association where any number of entities can be associated with a collection of other entities. In relational database any number of rows of one entity can be referred to any number of rows of another entity.

How get data from many-to-many relationship in JPA?

Shift model: @Entity @Table(name = "shift") public class Shift implements Serializable { private static final long serialVersionUID = -7065683873804696266L; @Id @GeneratedValue(strategy = GenerationType. AUTO) private Long id; @ManyToMany(fetch = FetchType. EAGER, cascade = CascadeType.


1 Answers

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;
}
like image 192
Oleksandr Yefymov Avatar answered Oct 04 '22 10:10

Oleksandr Yefymov