@UniqueConstraints 更新失败
我有一个实体类,在其中添加了 uniqueconstraint 注释,
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"staffRecord_id", "defaultLabel_id","company_id","keyCode"})})
public class AllowanceDeduction implements Serializable{
---
我注意到当我尝试保存在表上时 使用
if (allowanceDeduction.getId() == null) {
this.entityManager.persist(allowanceDeduction);
} else {
this.entityManager.merge(allowanceDeduction);
}
when the save or update fails due to a unique constraint. Isn't it only supposed to fail when trying to save a new record that is identical to a record that already exist.
为什么尝试合并或更新时会失败? 请需要帮助
I have an Entity class in which I put uniqueconstraint annotation
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"staffRecord_id", "defaultLabel_id","company_id","keyCode"})})
public class AllowanceDeduction implements Serializable{
---
What I have noticed that is when I try to save on the table
using
if (allowanceDeduction.getId() == null) {
this.entityManager.persist(allowanceDeduction);
} else {
this.entityManager.merge(allowanceDeduction);
}
when the save or update fails due to a unique constraint. Isn't it only supposed to fail when trying to save a new record that is identical to a record that already exist.
Why would it fail when trying to merge or update?
Please help needed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能肯定地说,但看起来你正试图保留一个 null id
I can't say for sure but it looks like you're trying to persist a null id