Hibernate Interview Questions – Java ORM Preparation Guide

Hibernate is a widely used ORM (Object-Relational Mapping) framework in Java. Here are frequently asked Hibernate interview questions with answers.

1. What is Hibernate, and why is it used?

Hibernate is an ORM framework that maps Java objects to database tables, reducing the amount of boilerplate JDBC code needed for database operations.

2. What is the difference between JPA and Hibernate?

JPA is a specification for ORM in Java; Hibernate is a popular implementation of that specification, with some additional Hibernate-specific features.

3. What is a SessionFactory in Hibernate?

A thread-safe, heavyweight object created once per application that produces Session objects used to interact with the database.

4. What is the difference between get() and load() methods?

get() returns null if the object isn't found and hits the database immediately; load() returns a proxy and throws an exception later if the object doesn't exist.

5. What is the difference between first-level and second-level cache?

First-level cache is tied to a Session and enabled by default; second-level cache is shared across sessions and must be explicitly configured (e.g., with EhCache).

6. What are the different states of an entity in Hibernate?

Transient (not associated with a session), Persistent (associated with an active session), and Detached (was persistent, but the session has closed).

7. What is lazy loading vs eager loading?

Lazy loading fetches related data only when accessed; eager loading fetches related data immediately along with the parent entity.

@OneToMany(fetch = FetchType.LAZY)
private List<Order> orders;

8. What is the N+1 select problem?

It occurs when Hibernate executes one query to fetch a list of entities, then a separate query for each entity's related data, causing performance issues; solved using joins or fetch strategies.

9. What is HQL?

Hibernate Query Language is an object-oriented query language similar to SQL, but operates on entity objects and their properties instead of tables and columns directly.

10. What is the difference between save() and persist()?

save() returns the generated identifier immediately and can be used outside a transaction; persist() doesn't return the identifier and is meant to be used within a transaction.

Understanding caching and the N+1 problem in depth often separates junior from senior candidates in Hibernate interviews — these come up frequently in performance-focused discussions.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course