Reputation: 353
I have a page with two buttons, clicking each actually calls methods in two different classes who have entityManager injected into them.
Now when in class 1 the method save() is called, an entity is updated with the most recent values. Before the method returns, I call entityManager.flush()
so that changes are flushed to the database.
Immediately after that if I click the other button which calls method advance() of class 2 and load the same entity using entityManger.find(entity.class, Long.valueOf(entityId))
, the fields that were updated in the previous method call show to be null
.
Do I need to do any configuration to make sure that this does not happen, or how can I share the Entity Manager between these two classes so that I can make sure that calls after the flush operation work on the updated database.
Upvotes: 0
Views: 177
Reputation: 28706
The transaction is commited. That's not the problem.
From the java doc of the EntityManager.find() method:
If the entity instance is contained in the persistence context, it is returned from there.
It means that : the find won't fetch the object from the DB if it is already present in your entityManager.
To refresh the entity, simply call refresh(entity):
MyEntity myEntity = entityManger.find(MyEntity.class, Long.valueOf(entityId));
entityManger.refresh(myEntity);
Upvotes: 1