Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

For a while now I have been wondering why when using JPA, do I have to write my delete methods like this:

@Transactional
public void delete(Account account)
{
    if (entityManager.contains(account))
    {
        entityManager.remove(account);
    }
    else
    {
        entityManager.remove(entityManager.merge(account));
    }
}

Perhaps the contains isn't needed since the transaction begins and ends with this method, but I still wonder why the remove couldn't just take an unmanaged object. Is it because it needs to be managed in order to know what the id is for that object? Any other insights would be great to hear. I just want to understand the hows and whys of the JPA remove.

like image 837
testing123 Avatar asked Apr 18 '13 15:04

testing123


1 Answers

The remove operation can be cascaded to associations of an entity.

To be able to know which associated entities to remove, the entity manager can't rely on a detached entity, since, by definition, this detached entity doesn't reflect the latest state of the entity, and doesn't necessarily have all its cascaded associations recursively loaded.

So, if it accepted a detached entity, remove() would have to decide for you: either merge the detached entity and execute the remove operation based on what the detached entity contains, or simply load the entity having the same ID as the detached entity, and execute the operation based on the loaded entity.

Instead of deciding for you, it simply requires an attached entity. That way, you decide what you want.

like image 66
JB Nizet Avatar answered Oct 22 '22 21:10

JB Nizet