Spring Boot – EntityManager Implemented Classes & Transaction Management

1. EntityManager Implementation Classes

In JPA, EntityManager is only an interface.

Actual implementations are provided by JPA providers like:

In Spring Boot, the most commonly used provider is Hibernate.

EntityManager (Interface)
        |
        v
Hibernate Implementation
        |
        v
Session (Hibernate Core)

1.1 Main EntityManager Implementations

Implementation Class Provided By Description
org.hibernate.internal.SessionImpl Hibernate Core implementation behind EntityManager
org.springframework.orm.jpa.SharedEntityManagerCreator Spring Creates proxy EntityManager
org.springframework.orm.jpa.EntityManagerProxy Spring Proxy used in Spring transactions
org.hibernate.engine.spi.SessionImplementor Hibernate Advanced internal interface

1.2 Real Runtime Flow in Spring Boot

When you inject EntityManager in Spring:

@PersistenceContext
private EntityManager entityManager;

Spring does not inject the real object directly. Instead it injects a proxy.

Spring Proxy EntityManager
        |
        v
Actual Hibernate Session
        |
        v
Database

Flow Diagram

Application
     |
     v
Spring Proxy EntityManager
     |
     v
Hibernate SessionImpl
     |
     v
JDBC
     |
     v
Database

2. Hibernate Relationship

EntityManager ≈ Hibernate Session

Hibernate internally converts EntityManager operations to Session operations.

JPA EntityManager Hibernate Session
persist() save()
find() get()
merge() update()
remove() delete()

3. Transaction Management in Spring Boot

Transaction management ensures:

Spring supports two transaction types:

Type Description
Programmatic Manual transaction control
Declarative Using annotations

Most Spring applications use Declarative Transactions.

4. Declarative Transaction Management

Spring provides the annotation:

@Transactional

Example

@Service
@Transactional
public class StudentService {

    @PersistenceContext
    private EntityManager entityManager;

    public void saveStudent() {

        Student student = new Student();
        student.setName("Rahul");

        entityManager.persist(student);
    }
}

What Happens Internally

Controller
    |
    v
Spring AOP Proxy
    |
    v
Transaction Start
    |
    v
Service Method
    |
    v
EntityManager.persist()
    |
    v
Transaction Commit

5. Transaction Lifecycle

Transaction Begin
        |
        v
Execute Business Logic
        |
        v
Persistence Context Flush
        |
        v
Commit Transaction

If an error occurs:

Transaction Begin
        |
        v
Exception Occurs
        |
        v
Transaction Rollback

6. Spring Transaction Managers

Transaction Manager Used For
JpaTransactionManager JPA / Hibernate
DataSourceTransactionManager JDBC
HibernateTransactionManager Hibernate native
JtaTransactionManager Distributed transactions

Most Spring Boot JPA applications use:

JpaTransactionManager

7. JpaTransactionManager Flow

Service Layer
      |
      v
@Transactional
      |
      v
JpaTransactionManager
      |
      v
EntityManager
      |
      v
Database

8. Example – Transaction Management

Entity

@Entity
public class Student {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

}

Repository / Service

@Service
public class StudentService {

    @PersistenceContext
    private EntityManager entityManager;

    @Transactional
    public void createStudent() {

        Student student = new Student();
        student.setName("Rahul");

        entityManager.persist(student);
    }
}

9. Transaction Propagation

Propagation defines how transactions behave when one method calls another.

Propagation Meaning
REQUIRED Join existing or create new
REQUIRES_NEW Always create new transaction
SUPPORTS Join if exists
NOT_SUPPORTED Run without transaction
MANDATORY Must have transaction
@Transactional(propagation = Propagation.REQUIRED)

10. Transaction Isolation Levels

Level Description
READ_UNCOMMITTED Dirty reads allowed
READ_COMMITTED Only committed data visible
REPEATABLE_READ Same row read multiple times
SERIALIZABLE Highest isolation
@Transactional(isolation = Isolation.READ_COMMITTED)

11. Transaction Rollback Rules

By default:

Rollback → RuntimeException
Commit → CheckedException
@Transactional(rollbackFor = Exception.class)

12. Complete Transaction Flow in Spring Boot

Client Request
      |
      v
Controller
      |
      v
@Service Method
      |
      v
@Transactional Proxy
      |
      v
JpaTransactionManager
      |
      v
EntityManager
      |
      v
Hibernate Session
      |
      v
JDBC
      |
      v
Database

13. Interview Questions

Q1: Is EntityManager a class or interface?

EntityManager is a JPA interface. Actual implementation is provided by Hibernate SessionImpl.

Q2: What transaction manager is used in Spring Boot JPA?

JpaTransactionManager

Q3: How does Spring manage transactions?

@Transactional

using AOP proxy-based transaction management.

Q4: What is the relationship between EntityManager and Hibernate Session?

EntityManager is a wrapper over Hibernate Session.