This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.
No announcement yet.
getHibernateTemplate().merge vs. saveOrUpdate()Page Title Module
// Note: Hibernate3's merge operation does not reassociate the object with the
// current Hibernate Session. Instead, it will always copy the state over to
// a registered representation of the entity. In case of a new entity, it will
// register a copy as well, but will not update the id of the passed-in object.
// To still update the ids of the original objects too, we need to register
// Spring's IdTransferringMergeEventListener on our SessionFactory.
IMHO, saveOrUpdate is still needed as it's semantics are different then with merge.
We had quite a lot a lot of NonUniqueObjectExceptions when for example we get an updated object from the object and we try to save it while a previous findAll has already associated the object with the session(especially with OSIV filter).
As the methods are written independently the object association is unknown and the solution was using an unmananged session for saving or getting the list. With merge things are a lot easier.
I haven't checked but I think merge is more 'expensive' then saveOrUpdate so using it through out the application is going to add a penalty.