2016-05-30 32 views
2

我一直遇到實體管理器merge()方法的問題。HibernateException:foo實例的標識符已從X更改爲Y

有兩個實體'FOO'和'BAR',它們在模式中沒有關係,只能保持這種方式,可悲的是我不能改變它。 FOO中有一個名爲PROCESS_CD的列,與BAR中的CLASS_CD完全相同。 CLASS_CD是BAR的主鍵,但FOO中的PROCESS_CD不是外鍵。

我想將PROCESS_CD中FOO的值從X更新爲Y,但是hibernate會嘗試更改BAR中的值,從而發生錯誤。

我不知道如何指定它應該在FOO只更新而不是在BAR

因此,這裏是我的數據庫架構

Database Schema

我的Foo類

@javax.persistence.Entity 
@Table(name = "FOO") 
public class FooImpl implements Foo { 

    private Long callCode; 
    private Journal journal; 

    @Id 
    @Column(name = "CALL_CD") 
    public Long getCallCode() { 
     return callCode; 
    } 

    public void setCallCode(Long aLong) { 
     callCode = aLong; 
    } 


    @ManyToOne(fetch = FetchType.LAZY, targetEntity = BarImpl.class) 
    @JoinColumn(name = "PROCESS_TYPE") 
    public Journal getJournal() { 
     return journal; 
    } 
} 

我的酒吧班

@javax.persistence.Entity 
@Table(name = "BAR") 
public class BarImpl implements Bar { 

    private String Code; 
    private String Description; 

    public List<Interaction> interaction; 

    @Id 
    @Column(name = "CLASS_CD") 
    public String getCode() { 
     return Code; 
    } 

    public void setCode(String code) { 
     Code = code; 
    } 

    @Column(name = "CLASS_LONG_DESCR") 
    public String getDescription() { 
     return Description; 
    } 

    public void setDescription(String description) { 
     Description = description; 
    } 
} 

這是我fooDao類

public class FooDao { 

    public void saveFoo(Foo instance) { 
     log.debug("update instance"); 
     try { 
      if (getEntityManager().contains(instance)) { 
       getEntityManager().merge(instance); 
      } else { 
       getEntityManager().persist(instance); 
      } 
      log.debug("update successful"); 
      return instance; 
     } catch (RuntimeException re) { 
      log.error("update failed", re); 
      throw re; 
     } 
    } 

    public void startTransaction() throws TransactionException{ 
     EntityTransaction t = getEntityManager().getTransaction(); 
     t.begin(); 
     TransactionManager.setTransaction(t); 
    } 
    public void commitTransaction()throws TransactionException { 
     EntityTransaction t = TransactionManager.retrieveTransaction(); 
     if(t == null) 
      throw new TransactionException("No transaction associated witht his thread"); 
     t.commit(); 
    } 
    public void rollbackTransaction() throws TransactionException{ 
     EntityTransaction t = TransactionManager.retrieveTransaction(); 
     if(t == null) 
      throw new TransactionException("No transaction associated witht his thread"); 
     t.rollback(); 
    } 
} 

,然後最後我打電話的方式來保存

以下是錯誤我得到

Caused by: org.hibernate.HibernateException: identifier of an instance of BarImpl was altered from X to Y 

我希望你能幫忙,我可以」 t找到任何東西在國際

謝謝你的時間

+0

嗯還是找不到問題 – SandMan

回答

0

首先感謝大家回覆我的問題,你們都非常有用。 :D

我發現我的問題,它與我的實際實施居住。我不是創建一個新的barImpl,而是去搜索一個,如果它是空的,它需要創建一個新的bar,但是如果它找到一個,它會繼續並輸入新的值。

請參閱我不知道實體類與數據庫中的實際記錄保持關係。所以當我改變DAO發現的實體的值時,實際上是在嘗試保存foo實體時嘗試更改數據庫中的值。

祝你有個愉快的日子