The difference between the separate and remove entityManager methods

I would like to know what is the real difference between em.detach(entity) , em.remove(entity) and using a JPQL query, for example:

 em.createQuery("DELETE FROM Country").exceuteUpdate(); 

thanks.

+4
source share
1 answer
 void detach(java.lang.Object entity) 

Remove this object from the persistence context , as a result of which the managed object becomes disconnected. Unflushed changes made to the object, if any (including deleting the object), will not be synchronized with the database. Objects that previously referenced a single object will continue to reference it.


 void remove(java.lang.Object entity) 

Delete an instance of an object. The database is immediately affected.


 em.createQuery("DELETE FROM Country").exceuteUpdate(); 

Whether the deletion is directly in the database, if you have this object, for example, saved in any list or it is a simple reference object, it will not receive these changes and will surely cause an error if it tries to merge or do something with it. Believe me, do not do this if this is your last choice.

Hope this will be a clear answer!

Yours faithfully!

+6
source

Source: https://habr.com/ru/post/1499642/


All Articles