What Hibernate Solves
Hibernate is an ORM framework. ORM maps relational database tables to Java objects so developers can work with entities instead of manually writing every JDBC mapping step.
Core Interfaces
| Interface | Role |
|---|---|
| Configuration | Reads Hibernate configuration |
| SessionFactory | Thread-safe factory for Session objects |
| Session | Unit of work for interacting with the database |
| Transaction | Groups database operations atomically |
| Query | Executes HQL/JPQL-style queries |
| Criteria | Builds dynamic queries through an API |
SessionFactory vs Session
SessionFactory is heavyweight and thread-safe. It is usually created once per application. Session is lightweight but not thread-safe and should be short-lived.
ORM vs JDBC
Hibernate can reduce boilerplate, manage entity state, and hide repetitive SQL mapping. JDBC gives more direct control and may be simpler for small, explicit queries.
Hibernate Code Examples
Entity Mapping
@Entity
@Table(name = "orders")
public class OrderEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String customerEmail;
@Column(nullable = false)
private BigDecimal totalAmount;
}
Repository Style
@Repository
public class OrderRepository {
@PersistenceContext
private EntityManager entityManager;
public OrderEntity findById(Long id) {
return entityManager.find(OrderEntity.class, id);
}
public void save(OrderEntity order) {
entityManager.persist(order);
}
}
Session and Transaction Style
try (Session session = sessionFactory.openSession()) {
Transaction tx = session.beginTransaction();
OrderEntity order = session.get(OrderEntity.class, orderId);
order.setTotalAmount(newTotal);
tx.commit();
}
Interview Scenario Practice
Scenario 1: Shared Static Session
Scenario: A team stores a Hibernate Session in a static field and shares it across web requests.
Strong answer: Share SessionFactory, not Session. A SessionFactory is thread-safe and expensive to create; a Session is short-lived and not thread-safe.
Why it works: Each request or transaction gets its own unit of work while the factory remains reusable.
Common mistake: Treating Session like a global database connection.
Scenario 2: Optional Search Filters
Scenario: A search API has optional filters such as status, date range, customer email, and minimum amount.
Strong answer: Use Criteria-style query building or a repository abstraction that composes predicates safely.
Why it works: Dynamic filters can be added conditionally without fragile string concatenation.
Common mistake: Building query strings by manually concatenating user input, which risks bugs and injection issues.
Scenario 3: ORM Performance Issue
Scenario: A page becomes slow after adding Hibernate relationships.
Strong answer: Check generated SQL, lazy/eager loading, N+1 queries, indexes, and transaction boundaries. Hibernate reduces boilerplate but does not remove database performance work.
Why it works: ORM problems usually appear in the SQL and data access pattern, not only in Java code.
Common mistake: Saying "Hibernate is slow" without inspecting the actual queries.