Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird Hibernate issue saveOrupdate or merge (both not working)
    primarykey
    data
    text
    <p>We have an Email object.</p> <p>The email.hbm has</p> <pre><code>&lt;bag name="emailRecipients" lazy="false" inverse="false" cascade="save-update" fetch="select"&gt; &lt;key column="EMAIL_ID" /&gt; &lt;one-to-many class="EmailRecipient" /&gt; &lt;/bag&gt; &lt;bag name="emailStateRecipients" lazy="false" inverse="false" cascade="save-update" fetch="select"&gt; &lt;key column="EMAIL_ID" /&gt; &lt;one-to-many class="EmailStateRecipient" /&gt; &lt;/bag&gt; </code></pre> <p>I'm trying to save a new email which has an arraylist of EmailRecipient &amp; EmailStateRecipient objects.</p> <p>When I tried:</p> <pre><code>this.getHibernateTemplate().saveOrUpdate(email); </code></pre> <p>Got an error:</p> <pre><code>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] </code></pre> <p>So i changed to:</p> <pre><code>this.getHibernateTemplate().merge(email); </code></pre> <p>Now get this error:</p> <pre><code>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] </code></pre> <p>Can somebody help on resolving this. I think I have to check if the object already exists &amp; if yes, then merge or else save. But not sure how. Also if the hbm correct?</p> <p>Thanks</p> <p><strong>Edit:</strong></p> <p>EmailRecipient.hbm</p> <pre><code>&lt;hibernate-mapping package="com.abc.model"&gt; &lt;class name="EmailRecipient" table="EMAIL_RCPNT"&gt; &lt;id name="emailRecipientId" column="EMAIL_RCPNT_ID" type="long"&gt; &lt;generator class="sequence"&gt; &lt;param name="sequence"&gt;SEQ_RCPNT_ID_PK&lt;/param&gt; &lt;/generator&gt; &lt;/id&gt; &lt;property name="roleId" column="ROLE_ID" type="long" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>Have not added the emailId property to the Recipient.hbm as it is declared as OneToMany in the email.hbm.</p> <p>Another weird aspect is, this (saveOrUpdate) works fine on my unit test, but does not when running from the app.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload