@UniqueConstraints在更新时失败

时间:2011-08-08 13:56:40

标签: ejb-3.0 unique-constraint

我有一个Entity类,我在其中放入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.

尝试合并或更新时为什么会失败? 请帮助

1 个答案:

答案 0 :(得分:0)

我不能肯定地说,但看起来你正试图坚持一个空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 {