AndreaNobili
AndreaNobili

Reputation: 43047

How exactly means partecipate in a given transaction in Spring working with JPA?

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:

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

Upvotes: 4

Views: 427

Answers (1)

Vitalie
Vitalie

Reputation: 413

I am also learning for the certification and after reading your post I think this excerpt from Spring documentation is what this question is all about:

Spring JPA also allows a configured JpaTransactionManager to expose a JPA transaction to JDBC access code that accesses the same DataSource, provided that the registered JpaDialect supports retrieval of the underlying JDBC Connection. Out of the box, Spring provides dialects for the EclipseLink, Hibernate and OpenJPA JPA implementations. See the next section for details on the JpaDialect mechanism.

also check What transaction manager should I use for JBDC template When using JPA ?

Upvotes: 0

Related Questions