Extra uninitialised entities spawned on merge()

#1

In the following, Element is a base entity class, with a bi-directional @ManyToOne-@OneToMany relationship Element owner - List<Element> ownedElements, and Example is a subclass entity with a relationship @OneToOne Element child.

Case: DO_PERSIST_BEFORE_MERGE = true: If I create an Element 'root', persist it, load 'root' again with find() outside a transaction as a detached entity, then create an Example 'e' (with 'root' as owner), persist it, then add it as an ownedElement of 'root', then merge 'root', all is fine, and the output is as expected:

Case: DO_PERSIST_BEFORE_MERGE = false: If I create an Element 'root', persist it, load 'root' again with find() outside a transaction as a detached entity, then create an Example 'e' (with 'root' as owner), do NOT persist it, then add it as an ownedElement of 'root', then merge 'root', the output shows a number of additional persistent entities that are also not fully initialised (have for example null 'name' String):

I would like to understand why the merge() on the root element is not able to correctly persist the related Example entity cleanly and why these additional entities arise with ObjectDB.

Pro JPA2 says p.161:

"When merge() is invoked on a new entity, it behaves similarly to the persist() operation. It adds the entity to the persistence context, but instead of adding the original entity instance, it creates a new copy and manages that instance instead. The copy that is created is by the merge() operation is persisted as if the persist() operation were invoked on it."

"In the presence of relationships, the merge() operation will attempt to update the managed entity to point to managed versions of the entities referenced by the detached entity. If the entity has a relationship to an object that has no persistent identity, the outcome of the merge operation is undefined. Some providers might allow the managed copy to point to the non-persistent object, whereas others might thown an exception immediately. The merge() operation can be optionally cascaded in these cases to prevent an exception from occurring."

Grateful for feedback (although the problem is somewhat academic as a solution is clear),