Merge on detached entities

#1

Hello,

it is possible to merge detached entities?

There are some attached entities which load from the database. The transaction is finished and the entities become detached.

In a new transaction the fields of the detached entities will be changed and a merge by em.merge(entity) will be saved the entities.

Can this works correctly?

How are the rules of the merge process? Do the changed fields of the detached entity overwrite the fields in the database? Also changed fields which are set to null?

best regards

#2

> it is possible to merge detached entities?

Sure. See this manual page.

> Can this works correctly?

It should work.

> How are the rules of the merge process? Do the changed fields of the detached entity overwrite the fields in the database?

Yes.

> Also changed fields which are set to null?

Yes, maybe except the mapped by side of bidirectional relationships, which are owned by the other side.

ObjectDB Support
#3

We tried something out with detached entities. Normally they are not fully loaded when not all attributes or references are loaded eagerly. We had an example in which a collection of referenced entities was null - but just because that collection was not yet loaded. The cascade type for this reference was set to ALL. Then we tried to merge that detached entity back to the context by using merge() on that object. After the merge we commited the transaction.

In one rar moment we discovered that the value null for the collection was persisted. For lots of objects of the same class all went fine. Just some had this defect.

What we would like to know are the exact rules how to attach detached entities when the entity manager uses lazy loading. What settings on annotations are needed regarding fetch types and cascades.

Kind regards

#4

Because all the owned persistent fields (excluding mapped by fields) of a detached object are used using merge to override the existing state of the object in the database (as discussed in #2 above) merging a partially loaded detached entity object is impractical and should be avoided.

Detached objects do not have information about which fields are loaded and which are not because they are not managed by ObjectDB anymore, so ObjectDB cannot distinguish between loaded and unloaded fields in these detached objects.

ObjectDB Support
#5

Hello,

thank you for your answer.

Regards

Reply