Making sure a transaction commits before another one starts when using different classes in Seam

StackOverflow https://stackoverflow.com/questions/18781677

  •  28-06-2022
  •  | 
  •  

Pergunta

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.

Foi útil?

Solução

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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top