Spring Data JPA多对多,带有额外的列用户和角色

编程入门 行业动态 更新时间:2024-10-23 09:25:00
本文介绍了Spring Data JPA多对多,带有额外的列用户和角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过User和Role实体之间的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 .

第二,在User和Role实体中,您都应具有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多对多,带有额外的列用户和角色

本文发布于:2023-10-28 19:35:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:角色   用户   Spring   Data   JPA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!