0 Replies Latest reply on Mar 15, 2006 9:46 AM by M R

    merge vs. saveOrUpdate

    M R Newbie

      In the HibernateEntityManager documentation I found this statement and would like to ask for some clarifications.

      Merging in EJB3 is similar to the saveOrUpdateCopy() method in native Hibernate. However, it is not the same as the saveOrUpdate() method, the given instance is not reattached with the persistence context, but a managed instance is returned by the merge() method.


      This means, when in hibernate a "merged" object is mapped to the same objectReference in ejb3 you will get a new reference (=copy) --> is that true ?

      simple example for explanation
      hibernate
      Entity entity;
      System.out.println(entity.getVersion()) /* will show 1 on outputstream */
      hibernateSession.saveOrUpdate(entity)
      System.out.println(entity.getVersion()) /* will show 2 on outputstream */
      


      ejb3
      Entity entity;
      System.out.println(entity.getVersion()) /* will show 1 on outputstream */
      entityManager.merge(entity)
      System.out.println(entity.getVersion()) /* will show 1 on outputstream */
      
      /***** but *****/
      entity = entityManager.merge(entity)
      System.out.println(entity.getVersion()) /* will show 2 on outputstream */
      


      So, if I am right, I will get a copy of the merged object beeing responsible for merging that copy myself into my code (which is sometimes very painful !).

      Again a simple example


       ArrayList list; /* holds a number of already persisted enitites*/
       Entity activeEntity; /* a reference to an object within the list */
      
       activeEntity.setAnyValue("change need to be persisted");
       Entity activeEntityCopy = entityManger.merge(activeEntity);
      
       // now we do have to reassamble the list - delete the old entity and insert the new copy, but may be keep ordering + ... this is painful - isn't it ?
      


      hopefully I am wrong - or maybe there is also a feature that supports someting like saveOrUpdate or at least there is a very good reason not to make a saveOrUpdate like in hibernate ...

      thx for feedback ...