Announcement Announcement Module
No announcement yet.
getHibernateTemplate().merge vs. saveOrUpdate() Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • getHibernateTemplate().merge vs. saveOrUpdate()

    I noticed the following in the 1.2 release notes:
    * reworked PetClinic's Hibernate DAO to use "merge" (plus IdTransferringMergeEventListener) instead of "saveOrUpdate"
    What's the reasoning behind this? Is there any reason to migrate existing apps that use saveOrUpdate() to use merge() instead?



  • #2
    First the quotes from the petclinic source :

    // 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.