When using JPA entityManager why do you have to merge before you remove? -


for while have been wondering why when using jpa, have write delete methods this:

@transactional public void delete(account account) {     if (entitymanager.contains(account))     {         entitymanager.remove(account);     }     else     {         entitymanager.remove(entitymanager.merge(account));     } } 

perhaps contains isn't needed since transaction begins , ends method, still wonder why remove couldn't take unmanaged object. because needs managed in order know id object? other insights great hear. want understand hows , whys of jpa remove.

the remove operation can cascaded associations of entity.

to able know associated entities remove, entity manager can't rely on detached entity, since, definition, detached entity doesn't reflect latest state of entity, , doesn't have cascaded associations recursively loaded.

so, if accepted detached entity, remove() have decide you: either merge detached entity , execute remove operation based on detached entity contains, or load entity having same id detached entity, , execute operation based on loaded entity.

instead of deciding you, requires attached entity. way, decide want.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -