JPA – the difference between the methods of ‘detach’ and ‘Remove’ entitymanager
I want to know what is the real difference between em.detach (entity), em.remove (entity) and using jpql requests:
em.createQuery("DELETE FROM Country").exceuteUpdate();
thank you.
Solution
void detach(java.lang.Object entity)
void detach(java.lang.Object entity)
Deletes the given entity from the persistence context, causing the managed entity to detach Unrefreshed changes (if any) to entities, including deleting entities, will not be synchronized to the database Entities that previously referenced the detached entity will continue to reference it
void remove(java.lang.Object entity)
Delete entity instance The database was immediately affected
em.createQuery("DELETE FROM Country").exceuteUpdate();
Delete it directly to the database. If you have that object, for example, saved in any list, or it is a simple reference object, it will not be changed. If you try to merge or do something, it will definitely cause an error Trust me, don't delete like this unless it's your last choice
I hope this is a clear answer!
Best wishes!