开发者

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

开发者 https://www.devze.com 2023-02-13 18:38 出处:网络
I created a Role entity, I would like to do CRUD operations on it. Everything works well, except the \"D\" (delete) operation.

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 simil开发者_运维知识库ar 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.


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.


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.


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

userTransaction.commit();

this one will work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号