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.