Just reviewing this JIRA and wanted to post my own thoughts. First, some clips from the JPA Specification that I think are relevant:
3.2 Entity Instance’s Life Cycle
This section describes the EntityManager operations for managing an entity instance’s lifecycle. An entity instance can be characterized as being new, managed, detached, or removed.
￼￼￼￼• • • •
A new entity instance has no persistent identity, and is not yet associated with a persistence context.
A managed entity instance is an instance with a persistent identity that is currently associated with a persistence context.
A detached entity instance is an instance with a persistent identity that is not (or no longer) associated with a persistence context.
A removed entity instance is an instance with a persistent identity, associated with a persistence context, that will be removed from the database upon transaction commit.
– What I take from this is that an Entity Class that has been freshly created by its constructor (ie, not created by em.find() or a JPA query) and has never, ever been touched by an em.persist() or em.merge() operation is a "new entity". Such an object will not have any StateManager (DetachedStateManager or otherwise, it is simply null) associated with its internal state (internal state being the hidden variables added by enhancement.)
– A "managed entity" is an Entity Class instance that has a link to a live StateManager in its internal state – and there's caveats to that. A StateManager is associated to one and only one Persistence Context (ie, EntityManager instance) - to those and only those Persistence Contexts is the entity a "managed entity". A new entity becomes a managed entity when em.persist() or em.merge() is called on it. (Managed entities are also provided by find/query operations (though it would return a detached entity if it's a CMTS entitymanager invocation outside of a transaction.))
– To any other Persistence Context, because it is not "new" (because it has a StateManager object associated with it) and has a non DetachedStateManager that is associated with another Persistence Context, it should be interpreted as a "detached entity" (even if the StateManager associated with the Entity Object instance's internal state is not a DetachedStateManager.) So calling em.merge(entityWithSomeOtherPCsStateManager) should be treated as a detached entity, and return a new Entity Object instance. Which could create "interesting problems" if the original entity doesn't have a committed row on the database yet (because the two different PCs will have two different db connections, and a merge operation will trigger a fetch operation from the database/cache.) Remember the JPA spec requires Read-Committed to be the minimum isolation level.