373 words

Merge on detached entities

#1
2017-09-19 09:54

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

btc_es
btc_es's picture
Joined on 2014-10-20
User Post #209
#2
2017-09-19 11:48

> 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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,816
#3
2017-09-20 14:36

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

btc_es
btc_es's picture
Joined on 2014-10-20
User Post #210
#4
2017-09-20 15:16

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,818
#5
2017-09-21 06:23

Hello,

thank you for your answer.

Regards

btc_es
btc_es's picture
Joined on 2014-10-20
User Post #211

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel