0

I have two methods, one that stores an entity object (EntityClass1 entity bean) using .persist() passing the EntityClass1 ID and another that is in charge to remove the entity object with the same ID.

I have a configuration in which i call, in a third method (in another class), both:

1) the method above that performs .persist() and

2) the method that performs .remove() (so, acting on the same entity object - same ID), after executing some code, if anything goes wrong.

The first operation goes right (i get to persist). When is the case of executing the method that performs the removal of the same entity object from the database, when i get to retrieve the object to remove by means of EntityManager.find(), this latter method returns NULL. HOW IS IT POSSIBLE?? I am sure I have stored that object.

I am sure to have used the needed to manage the transactions (in a CMT context): in each of the two methods i create an EntityManager object from a (@PersistenceUnit annotated) EntityManagerFactory, managing the transaction mechanism with the CMT context's TRY-CATCH-FINALLY block in whose FINALLY body i close the EntityManager object (in whose TRY body i execute the .persist() and the .remove(), respectively for the two methods above).

So, in my removal method i cannot retrieve the object to remove, even if that has just been persisted via my insertion method. What can be the problem?

Might there be any cause that made that object get removed, anyhow?

I am new to JavaEE-JPA-JTA. Thank you very much.

[EDIT]: I have fixed some of these issues. That em.find() that earlier returned null, NOW RETURNS MY WANTED OBJECT. I just modified my code in order to use a single entity manager ( @Jaspreet comments made me notice that) and i had to modify the CMT transaction management (didn't have to use that TRY-CATCH-FINALLY block). You can see JPA - strange behavior using EntityManager remove() method. Though, in this new discussion i posted new issues.

tcoddis
  • 21
  • 5
  • I think the problem is that you are trying to create 2 different contexts of entity manager while you are performing persist in single transaction which will be completed when your third method gets completed.After that only the 2nd method would find that entry in database and then only you will be able to delete it.Instead try to have single entity manager. – Jaspreet Jolly Feb 13 '19 at 11:15
  • jaspreet, i am going to do it immediately, thanks – tcoddis Feb 13 '19 at 11:27
  • i am getting: IllegalStateException: WFLYJPA0017: Container managed entity manager can only be closed by the container (auto-cleared at tx/invocation end and closed when owning component is closed.)... refers to my instruction "em.close();" in the FINALLY body – tcoddis Feb 13 '19 at 11:35
  • Hi jaspreet, after some edits on the code, i realized that this latter exception is no more raised, but i noticed that, even having a single entity manager, the method em.find() in my method that executes remove() still continues to return null. Does anybody else know about my issue? thanks – tcoddis Feb 13 '19 at 16:59
  • Hello, fixed something! I have posted, above, my new discussion in which I have other problems. Thank you! – tcoddis Feb 21 '19 at 15:03

0 Answers0