users@jpa-spec.java.net

[jpa-spec users] Clarification on JPA spec

From: <royce.ml-jmsspec_at_inomial.com>
Date: Wed, 29 Feb 2012 15:33:53 +0000 (GMT)

Hi all,

Please forgive me if this email is inappropriate for this list.

I have recently read the JPA 2.1 draft specification and I don't
understand part of section 3.2.7.1 (Merging Detached Entity State). I
wonder if it could use some clarification.

From the spec, the semantics of the merge operation:

• 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.

• If X is a new entity instance, a new managed entity instance X' is
created and the state of X is copied into the new managed entity
instance X'.

• If X is a removed entity instance, an IllegalArgumentException will
be thrown by the merge operation (or the transaction commit will fail).

• If X is a managed entity, it is ignored by the merge operation,
however, the merge operation is cascaded to entities referenced by
relationships from X if these relationships have been anno- tated with
the cascade element value cascade=MERGE or cascade=ALL annotation.

• For all entities Y referenced by relationships from X having the
cascade element value cascade=MERGE or cascade=ALL, Y is merged
recursively as Y'. For all such Y refer- enced by X, X' is set to
reference Y'. (Note that if X is managed then X is the same object as
X'.)

• If X is an entity merged to X', with a reference to another entity Y,
where cascade=MERGE or cascade=ALL is not specified, then navigation of
the same association from X' yields a reference to a managed object Y'
with the same persistent identity as Y.

The last bullet point is my pickle.

If Entity Y was stored, I read this to say that it'll be retrieved from
the DB as Y' and X' will reference Y', but Y's state will not be copied
to Y'.

But what if Y is a new object, not managed nor persisted? What should
JPA to do produce Y'?

I have done some playing around with this, and I have found that at
least one implementation (eclipselink 2.3.0) cascades the merge to Y in
order to produce Y', but I'm not sure that's the intended outcome,
especially given that cascade=MERGE is not specified.

For what it's worth, my expectation is that if Y was stored, it'll be
retrieved but the merge not cascaded. If Y was a new object an
exception would be thrown (IllegalArgumentException, perhaps?).

Does this part need clarification in the new spec, or am I too noob to
understand it? =)

Cheers,

--Royce