Cypher
Cypher

Reputation: 258

When calling JPA's remove() : "A JTA EntityManager cannot use getTransaction()"

I created a Role entity, I would like to do CRUD operations on it. Everything works well, except the "D" (delete) operation.

I use something similar to this ObjectDB example code.

As stated in the WebPage quoted above,

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.

How should I do that? What did I do wrong?

My Role entity is related to a User entity (a role can "have" several users, a user only one role). However, the Role instance I want to remove is by no means linked to any active user instances.

Upvotes: 2

Views: 4837

Answers (3)

Nithin karthick
Nithin karthick

Reputation: 25

userTransaction.begin();
                    
entityManager.remove(entityManager.merge(entity))

userTransaction.commit();

this one will work.

Upvotes: 0

Cypher
Cypher

Reputation: 258

Thank you for your answer, but it did not help me much. Though I do think it was related to JTA. I found out a solution however, so I 'd like to share it here :

Instead of :

public void removeRole(Role aRole) {
    entityManager.remove(aRole);    
} 

All I had to do was :

public void removeRole(Role aRole) {
entityManager.remove(entityManager.merge(aRole));   
}

And that does the trick. Go figure.

Upvotes: 3

James
James

Reputation: 18389

What error are you getting?

If you are using JTA, you cannot use EntityManager transactions, you must use JTA transactions (EJB, or lookup user transaction from conext). Either don't configure JPA to use JTA, or use JTA not EM transactions.

Upvotes: 1

Related Questions