Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Transient Entity in Hibernate to Update/Merge an existing Persistent Object
    text
    copied!<p>I am dealing with a fairly complex object graph in my database. I am using XStream to serialize and deserialize this object graph which works fine. When I import an object graph of an object that exists in the database, it is initially transient, since there are no IDs and hibernate knows nothing of it. I then have business logic which sets IDs on parts of my object graph by figuring out which objects in the newly transient imported object map to existing persistent objects. I then use Hibernate's merge() and saveOrUpdate(). </p> <p>Some pseudocode to give you a better idea of what I'm doing:</p> <pre><code>ComplexObject transObj = xstream.import("object.xml"); ComplexObject persistObj = someService.getObjByName(transObj.getName()); for (OtherObject o : c.getObjects()) { if (persistObj.getObjects().contains(o.getName())) { o.setId(persistObj.getObjectByName(o.getName()).getId()) } ... set a bunch of other IDs deeper in the object graph ... } transObj = session.merge(transObj); session.saveOrUpdate(transObj); </code></pre> <p>Now this doesn't work as I get errors such as:</p> <pre><code> org.springframework.dao.InvalidDataAccessApiUsageException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.......SomeObject#353296]; nested exception is org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.......SomeObject#353296] </code></pre> <p>and it seems like hibernate merge was not meant for associating transient objects to persistent ones.</p> <p>Is there any way to achieve what I want to do without having to get the persistent object in session, and modifying that, instead of modifying the transient one, and trying to save that and override the existing persistent one? </p>
 

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