2011-08-08 31 views
0

我有,我把的UniqueConstraint註解@UniqueConstraints失敗的更新

@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. 

爲什麼表 一個實體類嘗試合併或更新時會失敗嗎? 請幫助需要

回答

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 {