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