Hibernate Interview Questions and Answers
Q1. What is Hibernate?
A: Hibernate is a Java framework that helps in mapping Java classes to database tables and managing the
interaction between Java code and the database using ORM.
Q2. What are the advantages of Hibernate over JDBC?
A: Advantages include: Less boilerplate code, automatic table creation, easy database switching, caching
support, and built-in ORM support.
Q3. What is ORM (Object Relational Mapping)?
A: ORM stands for Object Relational Mapping. It lets you interact with a database using Java objects instead
of writing SQL queries.
Q4. What is a Session in Hibernate?
A: A Session is a lightweight object that represents a connection between the Java application and the
database for performing CRUD operations.
Q5. What is a SessionFactory?
A: SessionFactory is a heavyweight object used to create and manage Session objects. It is created once
and shared across the application.
Q6. Difference between get() and load()?
A: get() returns the actual object or null if not found. load() returns a proxy and throws an exception if the
object is not found.
Q7. What is a Hibernate configuration file?
A: A Hibernate configuration file (hibernate.cfg.xml) contains database connection details and other settings
required by Hibernate.
Q8. What are the different states of an object in Hibernate?
A: The states are: Transient (not associated with session), Persistent (associated with session), and
Detached (was persistent but now session is closed).
Q9. What are mapping files or annotations in Hibernate?
A: Mapping files or annotations define how Java classes map to database tables using either XML or
annotations like @Entity, @Table, etc.
Q10. What is the difference between @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany?
A: @OneToOne: one object to one, @OneToMany: one to many, @ManyToOne: many to one,
@ManyToMany: many to many relationship mappings.
Q11. What is Lazy Loading in Hibernate?
A: Lazy loading delays the loading of related data until it is actually accessed, improving performance.
Q12. What are Cascading types in Hibernate?
A: Cascade types allow operations on a parent entity to automatically apply to associated child entities.
Q13. What is HQL (Hibernate Query Language)?
A: HQL is Hibernate's query language, similar to SQL but works with Java class and field names instead of
table and column names.
Q14. Difference between HQL and SQL?
A: HQL is object-oriented, database-independent, and uses entity names. SQL uses actual table and column
names and is DB-specific.
Q15. How do you perform CRUD operations in Hibernate?
A: CRUD operations in Hibernate are done using methods like save(), get(), update(), and delete() within a
transaction.
Q16. What is the difference between save() and persist()?
A: save() returns the generated identifier; persist() doesn't return anything and is part of the JPA specification.
Q17. What is the use of merge() in Hibernate?
A: merge() is used to copy the state of a detached object into a persistent object within the current session.
Q18. What is the difference between update() and merge()?
A: update() attaches a detached object to the session; merge() creates a copy and merges it into the session.
Q19. What is the role of @Entity, @Table, @Id, etc. annotations?
A: @Entity marks a class as a Hibernate entity, @Table specifies the table, @Id defines the primary key.
Q20. How do you map relationships (like One-to-Many) using annotations?
A: Use annotations like @OneToMany, @ManyToOne, @JoinColumn to map entity relationships in
Hibernate.
Q21. What are the caching levels in Hibernate?
A: First-level cache is session-scoped and enabled by default. Second-level cache is SessionFactory-scoped
and optional.
Q22. What is the use of @JoinColumn and @JoinTable?
A: @JoinColumn defines a foreign key column. @JoinTable is used for many-to-many associations through a
join table.
Q23. What is N+1 Select Problem in Hibernate?
A: N+1 select problem happens when Hibernate executes 1 query for the main entity and N more for each
associated entity, causing performance issues.
Q24. How to handle transactions in Hibernate?
A: Transactions are managed using session.beginTransaction(), transaction.commit(), and
transaction.rollback() methods.
Q25. What are common exceptions in Hibernate and how to handle them?
A: Common exceptions include LazyInitializationException and ObjectNotFoundException, often handled
using proper session and transaction handling.
Q26. How to configure Hibernate with a database (MySQL, H2, etc.)?
A: You configure Hibernate using hibernate.cfg.xml or annotations by providing DB URL, username,
password, driver, and dialect.
Q27. How to improve performance in Hibernate?
A: Performance improvements include lazy loading, using second-level cache, batch processing, and
optimizing queries.
Q28. What is the difference between Criteria, Query, and NativeQuery?
A: Criteria API is for dynamic queries, Query uses HQL, and NativeQuery allows writing raw SQL queries.
Q29. How does Hibernate generate primary keys (@GeneratedValue)?
A: Hibernate uses @GeneratedValue with strategies like AUTO, IDENTITY, SEQUENCE, and TABLE to
generate primary keys.
Q30. Can you integrate Hibernate with Spring Boot? How?
A: Yes, Hibernate integrates with Spring Boot using Spring Data JPA, with configuration in
application.properties and repository interfaces.