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
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?
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.
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With