2

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

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 对象数组列表的新电子邮件。

当我尝试时:

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正确?

谢谢

编辑:

电子邮件收件人.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 属性添加到 Recipient.hbm,因为它在 email.hbm 中声明为 OneToMany。

另一个奇怪的方面是,这个 (saveOrUpdate) 在我的单元测试中运行良好,但在从应用程序运行时却不行。

4

1 回答 1

0

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

EmailRecipientId 被声明为“Long”并将其更改为原始类型解决了该问题。

感谢@Jeroen 要求检查

于 2012-07-25T18:09:30.387 回答