本文介绍了Spring Data JPA多对多,带有额外的列用户和角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过UserRole实体之间的Spring Boot JPA使用多对多关系.

I want to use many-to-many relationships using spring boot JPA between User and Role entity.

我可以使用BUT下面的代码来实现此要求,我需要在数据透视表(users_to_role)中多一列.

I was able to achieve this using code below BUT in my requirement, I need one extra column in the pivot table(users_to_role).

@ManyToMany
@JoinTable(
        name = "users_to_roles",
        joinColumns = @JoinColumn(
                name = "user_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(
                name = "role_id", referencedColumnName = "id"))
private List<Role> roles;

这是我的新代码

User.java

@Entity
@Table(name = "users")
public class User {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;

   private String username;

   @OneToMany(mappedBy="users", cascade = CascadeType.ALL)
   private List<Role> roles;

   //getters and setters here
}

Role.java

@Entity
@Table(name = "roles")
public class Role {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;

   private String name;

   @OneToMany(mappedBy="roles", cascade = CascadeType.ALL)
   private List<User> users;

   //getters and setters here
}

UserRole.java

@Entity
@Table(name = "users_to_role")
public class UserRole {

   @Id
   @ManyToOne
   @JoinColumn(name = "user_id")
   private User user;

   @Id
   @ManyToOne
   @JoinColumn(name = "role_id")
   private Role role;

   private Date createdAt;

  //getters and setters here
}

有人可以帮助我指出我做错了什么吗?

Could someone help me to point out what I am doing wrong?

这是错误堆栈:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Illegal use of mappedBy on both sides of the relationship: com.example.entities.Role.users

推荐答案

这是因为在@OneToMany中的用户实体中,您将mappedBy指定为users,而在UserRole实体中,将User实体指定为由对象user表示(不是用户).角色实体也是如此.

It is because in your User Entity in @OneToMany you are specifying mappedBy as users while in your UserRole Entity , User Entity is represented by the object user (not users).Similar is the case with Role Entity.

只需将mappingBy属性中的值与您在UserRole中提到的对象名称同步即可.

Just synchronize the value in mappedBy attribute with name of objects you are mentioning in UserRole .

第二,在UserRole实体中,您都应具有List<UserRole>(而不是用户或角色列表),因为您将在 users_to_role 表中具有外键.

Secondly in both User and Role Entities you should have List<UserRole> (not the list of User or Role)because you will have foreign key in users_to_role table.

以下更改将起作用

User.java

@OneToMany(mappedBy="user", cascade = CascadeType.ALL)
private List<UserRole> roles;

Role.java

@OneToMany(mappedBy="role", cascade = CascadeType.ALL)
private List<UserRole> users;

UserRole.java

public class UserRole implements Serializable {}

这篇关于Spring Data JPA多对多,带有额外的列用户和角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 00:08