Hibernate Session vs JPA EntityManager
Fast recall
Quick revision
Core answer
Whether you understand that Session and EntityManager are two APIs over the same persistence context, and which to prefer
Not realising Session and EntityManager manage the same underlying persistence context, and reaching for Session in Spring Boot services
Explain that EntityManager wraps Session and you can unwrap it to reach Hibernate-specific methods without abandoning the standard
Interview-ready guide
Interview answer
For example, entityManager.find(Order.class, id) is portable JPA. If a Hibernate-only operation is required, entityManager.unwrap(Session.class) makes that dependency explicit at the narrow point of use. Do not hold a raw Session as application-wide mutable state; let transaction/persistence-context management define its usable lifetime.
Hibernate Session vs JPA EntityManager
Direct Answer
EntityManager is Jakarta Persistence's standard persistence-context API. Hibernate Session is Hibernate's native API and, in Hibernate, EntityManager operations are implemented by a Session. Use EntityManager for portable JPA work; unwrap a Session only for a provider-specific feature that has a clear benefit and acceptable coupling.
Code Example
import jakarta.persistence.*;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class OrderService {
@PersistenceContext
private EntityManager entityManager; // JPA standard interface
public Order createOrder(Order order) {
entityManager.persist(order); // JPA method
return order;
}
public Order findById(Long id) {
return entityManager.find(Order.class, id);
}
// Reach a Hibernate-only feature without leaving the JPA standard elsewhere
public void enableBatching() {
Session session = entityManager.unwrap(Session.class);
session.setJdbcBatchSize(50); // Hibernate-specific tuning
}
}
Common Mistakes & Pitfalls
- Using Session in Spring Boot — injecting Session directly because of an old tutorial, when EntityManager is the modern standard.
- Not knowing they're related — treating Session and EntityManager as unrelated systems when they share one persistence context.
- Afraid to unwrap — not realising
entityManager.unwrap(Session.class)lets you reach Hibernate features without abandoning JPA.
Check your understanding
Answer these without looking back at the article.
- What is SessionFactory in Hibernate?
- When would you need to use Hibernate Session directly?
- What is @PersistenceContext?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.