Search code examples
jpaopenjpaentitymanager

JPA EntityManager why merge() does not make the instance managed?


When I call merge() on an object, the object does not become managed. Instead a reference to a managed instance of the same entity is returned. What is the logic behind this? Is there a way to change this behavior (and make the object managed) through some settings? I am using OpenJPA 2.2. Thank you.


Solution

  • The point is that merge() is not attaching object to EntityManager context, it is returning attached objects. So, if we have:

    AEntity a2 = entityManager.merge(a1);
    

    a1 remains not managed, while a2 is managed.

    This is, by the way, clever approach since merge does not cause side effects, passed object state is not changed.