Skip to main content

Auto merge of collections 'OneToMany' inside an entity

11 replies [Last post]
trouby
Offline
Joined: 2006-02-27

Hey,

I have an entity(X) which has a collection of (Y) entities,

After loading an X entity, modifying one of the Y's entities in the collection really persist the data into Y, even WITHOUT calling EntityManager.merge(X).

Why my Y collection gets updated automatically? sometimes I really want to modify the collection without persisting,

This is the declaration of the getCollection of Y's entities I use:

@OneToMany(cascade = {CascadeType.REMOVE}, mappedBy="userValue", fetch=FetchType.EAGER)
public Collection getUserValues() {
return userValues;
}

Thanks in advanced,

Asaf.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
apeto
Offline
Joined: 2010-07-08

I think there is a clear( java.lang.Object o ) too to clear ONLY that entity in particular.

But your problem was well answered, your entity is mannaged by the persistence unit so any change on it, is reflected

in the DB. No marge is needed becouse the entity is already "merged".

Albert.

gyorke
Offline
Joined: 2005-06-21

When 'X' is read within a transaction all changes to 'X' and any related objects will be detected by the persistence provider. This helps the user by not requiring a Persistence call for every update the user performs. If you do not want your changes sent to the database. You can 'clear()' the EntityManager before the end of the transaction.
--Gordon

ss141213
Offline
Joined: 2005-03-30

Or just don't commit the transaction; rollback the tx.

trouby
Offline
Joined: 2006-02-27

So persistence provider will detect and update changes even if no merge(entity) was explicit called?

ss141213
Offline
Joined: 2005-03-30

merge is required when the entity is not in [b]managed[/b] state. Your case is different because the entity is in managed state as you loaded it from database.

stvconsultants
Offline
Joined: 2006-03-06

and you can always explicitly take your entity out of the managed state bay calling em.detach on it.

trouby
Offline
Joined: 2006-02-27

Heh, enough options,

Although I'm pretty sure the object is already detached, but i'd like to verify that, is there any way to determine the current object state via entitymanager?

and btw, I never seen a method named 'detach' in entitymanager, I'm pretty sure it does not even exist...

thanks

ss141213
Offline
Joined: 2005-03-30

Use EntityManager.contains(java.lang.Object o) to see if an entity belongs to a persistence context or not.

trouby
Offline
Joined: 2006-02-27

Hey,

Back to this old thread,

Is there any way to detach an entity? I see a suggestion here to call a method named 'detach' on the EntityManager class but I dont see such a method at all.

Thanks.

mvatkina
Offline
Joined: 2005-04-04

No, there is no em.detach(). em.clear() will detach all entities managed by this EM.

-marina

trouby
Offline
Joined: 2006-02-27

Good enough,
thanks.