[英]updating an entity in many-to-many relationship
我有一个Spring项目,该项目将JPA与Hibernate和MySQL数据库一起使用。 数据库包括三个表:Users,Roles和多对多联接表UserRoles。 下面是表的相应类。
ApplicationUser.java
@Entity
@Table(name = "APPLICATION_USER")
public class ApplicationUser extends ExtAuditInfo {
public static final Long SYSTEM_USERID = 1000L;
@Id
@Column(name = "APPLICATION_USER_ID")
private long applicationUserId;
@Column(name = "LOGIN_NAME")
private String loginName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "MIDDLE_NAME")
private String middleName;
@OneToMany(mappedBy = "id.applicationUser", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<UserRole> roles =new ArrayList<>();
//get and set methods
角色.java
@Entity
@Table(name = "ROLE")
@NamedQueries({
@NamedQuery(name = "Role.getRoleById", query = "select r from Role r where r.roleId =:roleId"))}
public class Role extends AuditInfo {
@Id
@Column(name="ROLE_ID")
private long roleId;
@Column(name="ACTIVE_FLAG")
private String activeFlag;
@Column(name="ROLE_NAME")
private String roleName;
@OneToMany(mappedBy = "id.role", fetch = FetchType.LAZY)
private List<UserRole> users = new ArrayList<>();
//get and set methods
UserRole.java
@Entity
@AssociationOverrides({
@AssociationOverride(name = "id.applicationUser",
joinColumns = @JoinColumn(name = "APPLICATION_USER_ID")),
@AssociationOverride(name = "id.role",
joinColumns = @JoinColumn(name = "ROLE_ID")) })
@Table(name = "USER_ROLE")
public class UserRole extends ExtAuditInfo implements Serializable{
@EmbeddedId
private UserRoleID id = new UserRoleID();
@Column(name="USER_ROLE_VER")
private long userRoleVer;
public UserRole(){
}
@Transient
public ApplicationUser getApplicationUser() {
return this.id.getApplicationUser();
}
public void setApplicationUser(ApplicationUser applicationUser) {
this.id.setApplicationUser(applicationUser);
}
public long getUserRoleVer() {
return this.userRoleVer;
}
public void setUserRoleVer(long userRoleVer) {
this.userRoleVer = userRoleVer;
}
@Transient
public Role getRole() { return this.id.getRole(); }
public void setRole(Role role) { this.id.setRole(role); }
}
UserRoleID.java
@Embeddable
public class UserRoleID implements Serializable {
@ManyToOne(cascade = CascadeType.ALL)
private ApplicationUser applicationUser;
@ManyToOne(cascade = CascadeType.ALL)
private Role role;
private static final long serialVersionUID = 1L;
public UserRoleID() {
}
public ApplicationUser getApplicationUser() {
return this.applicationUser;
}
public void setApplicationUser(ApplicationUser applicationUser) {
this.applicationUser = applicationUser;
}
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
}
现在,当我创建一个具有查看者角色的示例用户时,该记录将插入到Application_user和User_Role表中,但是当我尝试更新该用户的角色时,它是向该用户添加一个新角色,而不是更新现有角色。
这就是我在做的
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void updateRole(ApplicationUser appUser, long roleId){
EntityManager em=getEntityManager();
TypedQuery<Role> query = em.createNamedQuery("Role.getRoleById", Role.class);
query.setParameter("roleId",roleId);
Role r = query.getSingleResult();
UserRole userRole= appUser.getRole().get(0);
userRole.setRole(r);
em.merge(userRole);
em.flush();
em.detach(userRole);
}
任何想法,如何更新现有角色而不是在user_role表中创建新角色?
您正在为用户分配新角色,因此在user_role表中添加了一条新记录,并删除了旧的user_role条目。 这是正确的行为。 因此,您不是被称为“更新用户角色”。
更新:多对多关系时,您应该手动删除角色。
appUser.getRoles().remove(userRole);
em.remove(userRole);
UserRole newUserRole = new UserRole();
newUserRole.setRole(r);
appUser.getRoles().add(newUserRole);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.