I am pretty new in Spring and in JPA and I am studying for Spring Core certification and I have some doubts about this question found on the study material:
Are you able to participate in a given transaction in Spring while working with JPA?
I think that the answer is no because I know that JPA provides configuration options so Spring can manage transactions and the EntityManager but I am absolutly not sure to have correctly understand the question. What exactly means with participate in a given transaction.
At the beginning I interpreted as to interact with an existing transaction and this is impossible because a transaction is atomic for definition.
But them into the documentation I found this informations:
To transparently participate in Spring-driven transactions:
Simply use one of Spring’s FactoryBeans for building the EntityManagerFactory
Inject an EntityManager reference with @PersistenceContext
Define a transaction manager: JpaTransactionManager
and show this example:
public class JpaOrderRepository implements OrderRepository {
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager (EntityManager entityManager) {
this. entityManager = entityManager;
}
public Order findById(long orderId) {
return entityManager.find(Order.class, orderId);
}
}
So is it related to my original question? How?
What exatly do the @PersistenceContext annotation and what is the exact role of the EntityManager object?
Tnx
Aucun commentaire:
Enregistrer un commentaire