【发布时间】:2019-05-16 19:37:19
【问题描述】:
我有三个实体:凭据、用户和管理员。 User 和 Admin 实体都有一个字段 credentials,它与使用 OneToOne 注释的 Credentials 实体相关。
当通过 entityManager.merge 更新现有的用户或管理员条目时,我在 Credentials.login 列上获得了重复的密钥,该列具有唯一约束。
@Entity
@Table
public class Credentials {
@Id
@Column(name="id", unique=true, nullable=false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique=true, nullable=false, length=50)
private String login;
@Column(nullable=false, length=50)
private String password;
/*********************************************
* getters and setters here
**********************************************/
}
@Entity
@Table
public class User{
@Id
@Column(name="id", unique=true, nullable=false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/*********************************************
* Specific user columns here
**********************************************/
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(nullable=false, name = "idCredentials")
private Credentials credentials;
/*********************************************
* getters and setters here
**********************************************/
}
@Entity
@Table
public class Admin{
@Id
@Column(name="id", unique=true, nullable=false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/*********************************************
* Specific admin columns here
**********************************************/
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(nullable=false, name = "idCredentials")
private Credentials credentials;
/*********************************************
* getters and setters here
**********************************************/
}
我希望在调用 entityManager.merge(user) 后在各自的数据库表中更新用户和 user.credentials,但我收到错误“重复条目 'loginname' 键'login_UNIQUE。管理实体也会发生同样的情况。
提前感谢您的帮助。
【问题讨论】:
标签: java hibernate jpa design-patterns persistence