2012-07-24 37 views
2

我們有一個電子郵件對象。奇怪的休眠問題saveOrupdate或合併(都不起作用)

的email.hbm有

<bag name="emailRecipients" lazy="false" inverse="false" 
      cascade="save-update" fetch="select"> 
      <key column="EMAIL_ID" />   
      <one-to-many class="EmailRecipient" /> 
</bag>   
<bag name="emailStateRecipients" lazy="false" inverse="false" 
      cascade="save-update" fetch="select"> 
      <key column="EMAIL_ID" />   
      <one-to-many class="EmailStateRecipient" /> 
</bag> 

我試圖挽救它有EmailRecipient & EmailStateRecipient對象的ArrayList新的電子郵件。

當我想:

this.getHibernateTemplate().saveOrUpdate(email); 

得到了一個錯誤:

org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.abc.model.EmailRecipient#0]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.abc.model.EmailRecipient#0] 

所以我改爲:

this.getHibernateTemplate().merge(email); 

現在得到這個錯誤:

org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [com.abc.model.EmailRecipient#0]; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.abc.model.EmailRecipient#0] 

有人可以幫助解決這個問題。我想我必須檢查對象是否已經存在&如果是,則合併或保存。但不知道如何。另外如果HBM正確?

感謝

編輯:

EmailRecipient.hbm

<hibernate-mapping package="com.abc.model"> 
    <class name="EmailRecipient" table="EMAIL_RCPNT"> 
     <id name="emailRecipientId" column="EMAIL_RCPNT_ID" type="long"> 
      <generator class="sequence"> 
       <param name="sequence">SEQ_RCPNT_ID_PK</param> 
      </generator>    
     </id> 
     <property name="roleId" column="ROLE_ID" type="long" />  
    </class> 
</hibernate-mapping> 

,因爲它是在email.hbm聲明爲一對多已經不是EMAILID屬性添加到R​​ecipient.hbm。

另一個奇怪的方面是,這個(saveOrUpdate)在我的單元測試中工作正常,但不從應用程序運行時。

+0

com.abc.model.EmailRecipient#0 - #0意味着它具有爲0的ID,這意味着它尚未保存。你能發佈EmailRecipient的完整映射文件嗎?我認爲可以在那裏找到問題(可能是錯誤的生成器類) – Jeroen 2012-07-24 19:29:41

+0

Jeroen,我已編輯並添加了recipient.hbm。請檢查。 – Harry 2012-07-24 20:41:42

+0

您需要自行將元素合併到列表中。所以如果你有元素1,你需要添加元素1,不要添加。不知道這是否應該解決,但嘗試。 I.另外嘗試調用清除並添加元素 – 2012-07-24 20:46:37

回答

0

哇!終於解決了這個問題。

EmailRecipientId被聲明爲「長」&將其更改爲原始類型解決了問題。

感謝@Jeroen詢問檢查

+0

Java中的自動裝箱是一個非常棒的功能!很高興它的工作。 – Jeroen 2012-07-31 10:55:34