java.lang.IllegalArgumentException: Removing a detached instance com.test.User#5

JpaSql DeleteEntitymanagerIllegalargumentexception

Jpa Problem Overview


I have a java EE project using JPA (transaction-type="JTA"), hibernate as provider. I write my beans to handle the CRUD things. The program running in JBOSS 7 AS.

I have an EntityManagerDAO :

@Stateful
public class EntityManagerDao implements Serializable {

	@PersistenceContext(unitName = "dtdJpa")
	private EntityManager entityManager;

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public Object updateObject(Object object) {
		object = entityManager.merge(object);
		return object;
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void createObject(Object object) {
		entityManager.persist(object);
	}

	public void refresh(Object object) {
		entityManager.refresh(object);
	}

	public <T> T find(Class<T> clazz, Long id) {
		return entityManager.find(clazz, id);
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void deleteObject(Object object) {
		entityManager.remove(object);
	}
}

but when I invoke deleteObject, this exception comes out.

> java.lang.IllegalArgumentException: Removing a detached instance com.test.User#5

How is this caused and how can I solve it?

Jpa Solutions


Solution 1 - Jpa

EntityManager#remove() works only on entities which are managed in the current transaction/context. In your case, you're retrieving the entity in an earlier transaction, storing it in the HTTP session and then attempting to remove it in a different transaction/context. This just won't work.

You need to check if the entity is managed by EntityManager#contains() and if not, then make it managed it EntityManager#merge().

Basically, the delete() method of your business service class should look like this:

em.remove(em.contains(entity) ? entity : em.merge(entity));

Solution 2 - Jpa

In my case, I got the same error, when I tried to delete an object using,

session.delete(obj)

without creating any transaction before that.

And the problem is solved by creating the transaction first(session.beginTransaction() and then deleting the object.

I hope my answer will help someone :)

Solution 3 - Jpa

Sometimes its simply because you are missing the @Transaction annotation for add, remove, update operations.

Solution 4 - Jpa

I faced the same problem. The detached entity should be re-attached. As @BalusC mentioned, using EntityManager.merge() should be used to attach the detached entity. EntityManager.merge() generates SQL Query which fetches the current state of the entity, on which EntityManager.remove() has to be performed. But in my case it didn't worked. Try EntityManager.remove(EntityManager.find(Class<T>,arg)) instead. It worked for me.

Solution 5 - Jpa

In my experience, if I query an object from the DB then closed the entity manager then do a DB delete, the problem happens. Or if I copy that loaded object to another instance then do a delete, this problem also happens. In my opinion there are 2 things to keep note:

  • The object must be in the same session that was created by the Entity Manager
  • And the object mustn't be transferred to another object while the Entity Manager's session is still opened.

Cheers

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionneptuneView Question on Stackoverflow
Solution 1 - JpaBalusCView Answer on Stackoverflow
Solution 2 - JpaRajeshView Answer on Stackoverflow
Solution 3 - JpaLingying ZhangView Answer on Stackoverflow
Solution 4 - Jpamurali101002View Answer on Stackoverflow
Solution 5 - JpaArtanis ZeratulView Answer on Stackoverflow