Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.persistence.Entitymanager: remove() method

Tags:

Does remove(Object entity) method of EntityManager work only on those objects got from find() method?

I have following code snippet:

public void deletePerson() {     EntityManager em = getEntityManager();     Person p = new Person("x", "y", 200);     em.remove(p); } 

But it is not removing the particular entry from database.

When I tried something like below:

public void deletePerson() {     EntityManager em = getEntityManager();     Person p = em.find(Person.class, 200);     em.remove(p); } 

It's working fine.

like image 904
sachinpkale Avatar asked Jul 18 '12 10:07

sachinpkale


People also ask

How do I delete EntityManager?

To delete a record from database, EntityManager interface provides remove() method. The remove() method uses primary key to delete the particular record.

How do you remove entity from persistence context?

You can prevent that by calling the flush method on the EntityManager. After you've done that, you can remove a specific entity from the persistence context by calling the detach method or you can call the clear method to clear the persistence context completely.

How do I delete a record using JPA?

The most efficient way to delete the records is with the help of the primary key. Because the primary key uniquely identifies each record of the table. We can use the JPA method deleteById() for deleting the record of the particular primary key.

What will happen when the clear () of an EntityManager class is invoked?

clear. Clear the persistence context, causing all managed entities to become detached. Changes made to entities that have not been flushed to the database will not be persisted.


2 Answers

Quoting from ObjectDB's manual on deleting JPA entity objects:

In order to delete an object from the database it has to first be retrieved (no matter which way) and then in an active transaction, it can be deleted using the remove method.

An IllegalArgumentException is thrown by remove if the argument is not a an instance of an entity class or if it is a detached entity.

When creating object with new operator, it becomes a detached entity, you need to persist it if you want to remove it.

When retrieving entity, you are retrieving persistent entity.

like image 190
JMelnik Avatar answered Oct 09 '22 14:10

JMelnik


Something to that direction. EntityManager.remove works only for managed entities. How you obtained these managed entities does not matter, it can be for example:

  • via JPQL query
  • via Criteria API query
  • find method in EntityManager
  • by following relationship from some other entity.
  • created new entity and persisted it

But simply creating new object and trying to remove it does not work, because this new object is not managed entity. Also entity should not be yet detached.

Life of entity is quite much as follows, all in same transaction (entities outside their transaction are not managed):

Entity ent = new Entity(1); //entity is in new state, EntityManager never know                             //anything about it em.persist(ent); //entity is managed as long as not disconnected                   //from EntityManager em.clear(); // all previously managed entities, including ent, are now detached  Entity same = em.find(1); //managed same em.remove(same); // entity is removed 
like image 20
Mikko Maunu Avatar answered Oct 09 '22 13:10

Mikko Maunu