Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Pre-existing managed entity instances in the persistence context: pre-existing on disk? Or...?

I had a specification-related question and was curious how EclipseLink chose to interpret it.

Suppose I have a table on disk to which I've mapped an entity class.  Let's just say there's a row in the table with a primary key of 6.  Simple straightforward INTEGER primary key mapped to a simple straightforward int field.

Now suppose I do this:

final SomeEntity x = new SomeEntity();
x.setID(6); // set the @Id -- the persistent identity

//
// Assumption: x is now "detached"; it is not "new"
// Please correct this assumption if it is false.
//

em.clear(); // empty persistence context

// 1

final SomeEntity xPrime = em.merge(x); // 2
em.flush(); // 3 -- Duplicate primary key error? If not, why not?

At point (1) above, there are no instances of anything in the persistence context.  It is entirely empty.

At point (2), the merge operation must EITHER locate a "pre-existing managed entity instance X' of the same identity [6]" OR create "a new managed copy X' of X".  Can the provider choose which strategy to use here?  Won't this have an effect on whether an INSERT or an UPDATE statement is ultimately issued at point (3)?  Or is that irrelevant?

At point (3), will I get a primary key duplication error?  Is it your understanding that merge() is required by the specification to do an UPSERT?  If so, where is that requirement mandated?  Does "pre-existing managed entity instance" actually mean "pre-existing managed entity instance loaded just-in-time into the persistence context behind the scenes by the provider if there's a corresponding row in the database"?

The relevant section in the specification is 3.2.7.1:

"The semantics of the merge operation applied to an entity X are as follows:
"If X is a detached entity [in my example it is], the state of X is copied onto a pre-existing managed entity instance X' of the same identity [bear in mind in my example I just cleared the persistence context so to my knowledge there are no instances at all of anything in there] or a new managed copy X' of X is created [the 'or' part makes me think the provider could just create a new one in all cases]."

Best,
Laird


Back to the top