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)
| 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 |
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
Application
|
v
Spring Proxy EntityManager
|
v
Hibernate SessionImpl
|
v
JDBC
|
v
Database
EntityManager ≈ Hibernate Session
Hibernate internally converts EntityManager operations to Session operations.
| JPA EntityManager | Hibernate Session |
|---|---|
| persist() | save() |
| find() | get() |
| merge() | update() |
| remove() | delete() |
Transaction management ensures:
Spring supports two transaction types:
| Type | Description |
|---|---|
| Programmatic | Manual transaction control |
| Declarative | Using annotations |
Most Spring applications use Declarative Transactions.
Spring provides the annotation:
@Transactional
@Service
@Transactional
public class StudentService {
@PersistenceContext
private EntityManager entityManager;
public void saveStudent() {
Student student = new Student();
student.setName("Rahul");
entityManager.persist(student);
}
}
Controller
|
v
Spring AOP Proxy
|
v
Transaction Start
|
v
Service Method
|
v
EntityManager.persist()
|
v
Transaction Commit
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
| Transaction Manager | Used For |
|---|---|
| JpaTransactionManager | JPA / Hibernate |
| DataSourceTransactionManager | JDBC |
| HibernateTransactionManager | Hibernate native |
| JtaTransactionManager | Distributed transactions |
Most Spring Boot JPA applications use:
JpaTransactionManager
Service Layer
|
v
@Transactional
|
v
JpaTransactionManager
|
v
EntityManager
|
v
Database
@Entity
public class Student {
@Id
@GeneratedValue
private Long id;
private String name;
}
@Service
public class StudentService {
@PersistenceContext
private EntityManager entityManager;
@Transactional
public void createStudent() {
Student student = new Student();
student.setName("Rahul");
entityManager.persist(student);
}
}
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)
| 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)
By default:
Rollback → RuntimeException Commit → CheckedException
@Transactional(rollbackFor = Exception.class)
Client Request
|
v
Controller
|
v
@Service Method
|
v
@Transactional Proxy
|
v
JpaTransactionManager
|
v
EntityManager
|
v
Hibernate Session
|
v
JDBC
|
v
Database
EntityManager is a JPA interface. Actual implementation is provided by Hibernate SessionImpl.
JpaTransactionManager
@Transactional
using AOP proxy-based transaction management.
EntityManager is a wrapper over Hibernate Session.