我有三个实体:凭据、用户和管理员。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 和 user.credentials,但我收到错误“重复条目 'loginname' for key 'login_UNIQUE。管理实体也会发生同样的情况。
预先感谢您的任何帮助。