@UniqueConstraints 更新失败

发布于 2024-11-28 14:20:43 字数 694 浏览 1 评论 0原文

我有一个实体类,在其中添加了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裸钻 2024-12-05 14:20:43

我不能肯定地说,但看起来你正试图保留一个 null id

if (allowanceDeduction.getId() == null) {
        this.entityManager.persist(allowanceDeduction);
        /* don't you need the id set to a non-null value in order to persist it? */
    } else {

I can't say for sure but it looks like you're trying to persist a null id

if (allowanceDeduction.getId() == null) {
        this.entityManager.persist(allowanceDeduction);
        /* don't you need the id set to a non-null value in order to persist it? */
    } else {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文