奇怪的Hibernate问题saveOrupdate或merge(两者都不工作)

时间:2012-07-24 19:22:16

标签: hibernate merge

我们有一个电子邮件对象。

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&amp; amp; EmailStateRecipient对象。

当我尝试时:

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]

有人可以帮忙解决这个问题。我想我必须检查对象是否已存在&amp;如果是,则合并或者保存。但不确定如何。如果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>

尚未将emailId属性添加到R​​ecipient.hbm,因为它在email.hbm中声明为OneToMany。

另一个奇怪的方面是,这个(saveOrUpdate)在我的单元测试中工作正常,但在从应用程序运行时却没有。

1 个答案:

答案 0 :(得分:0)

哇!终于解决了这个问题。

EmailRecipientId被声明为“Long”&amp;将其改为原始类型解决了这个问题。

感谢@Jeroen要求检查

相关问题