users@jpa-spec.java.net

[jpa-spec users] What does pre-existing mean when speaking of managed instances in a persistence context?

From: Laird Nelson <ljnelson_at_gmail.com>
Date: Wed, 24 Jul 2013 14:31:59 -0700

It occurs to me from reading the JPA specification for the
umpteen-thousandth time that I still don't entirely know what
"pre-existing" means in all cases.

When the merge() operation is described, it says that when you pass in a
detached object X, "the state of X is copied onto a pre-existing managed
entity instance X' of the same identity or a new managed copy X' of X is
created".

What does pre-existing mean?

Does it mean pre-existing in the database, or pre-existing in the
persistence context?

If I call EntityManager#clear() beforehand, how many things in the
persistence context are there afterwards?

Does merge() obligate an EntityManager to do the equivalent of the
following code first, before attempting the actual semantics of the merge()
operation:

  Object xPrime = this.find(x.getClass(),
this.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(x));
  if (xPrime == null) {
    xPrime = x.getClass().newInstance();
    manage(xPrime);
  }
  copyProperties(x, xPrime);
  return xPrime;

Or can an EntityManager simply choose to skip the first half of the
sentence if it determines somehow that the persistence context is empty?

  Object xPrime = x.getClass().newInstance();
  manage(xPrime);
  copyProperties(x, xPrime);
  return xPrime;

Another way of asking the question: does EntityManager#clear() simply
guarantee that all objects logically in the persistence context **to which
I might retain references** are guaranteed to be unmanaged?

Long StackOverflow version of the question with background and motivation:
http://stackoverflow.com/questions/17844932/what-does-pre-existing-mean-in-the-context-of-a-managed-entity-instance-in-a-j

Thanks,
Best,
Laird

-- 
http://about.me/lairdnelson