Skip to main content

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

  • From: michael keith <michael.keith@...>
  • To: users@...
  • Subject: [jpa-spec users] Re: What does pre-existing mean when speaking of managed instances in a persistence context?
  • Date: Thu, 01 Aug 2013 13:06:53 -0400
  • Organization: Oracle Corporation

Hi Laird,

Looks like you never got a response. I'll fill in the blanks as best I can.
(BTW, it's great that you love the spec so much that have read it umpteen-thousand times! Oh wait, I guess that wasn't meant as a compliment...)

"If X is a detached entity, 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."

A detached instance, by definition, means that there is a row corresponding to the instance identifier value in the database but the detached instance just doesn't happen to be in the persistence context. The above quoted statement is describing the behavior of only those "detached" entity instances to be merged. Once you get that, the rest is hopefully easier.

So if you assume that the entity exists in the db then either there is an instance with the same identity in the pc (i.e. a "pre-existing managed" instance) or there isn't. If there isn't then part 2 kicks in and the provider reads the entity from the database (that is the "new managed copy" part). The state of the detached instance is then merged into either the "pre-existing managed" instance or the "new managed" instance.

Not sure if this answers your question, but hopefully it clarifies your spec understanding a little, at least :-)

-Mike

On 24/07/2013 5:31 PM, Laird Nelson wrote:
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 <http://stackoverflow.com/questions/17844932/>

Thanks,
Best,
Laird

--
http://about.me/lairdnelson



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

michael keith 08/01/2013
 
 
Close
loading
Please Confirm
Close