01: ♥♦ 15+ Hibernate basics Q1 – Q7 interview questions & answers

Hibernate interview questions are as popular as Spring interview questions & answers.

Q1. What is an Object-to-Relational Mapping (i.e. ORM) tool?
A1. There are several ways to persist data to a database from your Java applications.

1) Direct JDBC SQL statements for the CRUD (Create, Read, Update, and Delete) operations. Direct JDBC is more SQL centric. You need to write all the SQL statements. This is covered in detail at JDBC Overview Interview Questions and Answers.

2) Persistence frameworks like Spring JDBC Template, iBatis, etc to make things easier to use. Spring JDBC Template is very popular, and provides an abstraction over JDBC.

3) ORM frameworks like Hibernate, TopLink from Oracle, etc which are more object centric. You create your domain objects in Java and use Hibernate annotations to map your objects to underlying database tables and columns, and ORM tool will do a lot of work like generating the CRUD SQL statements, caching, optimistic locking, etc. These out of the box benefits come at a price of increased complexity. Using ORM tools like Hibernate as a black-box without properly understanding its object life cycle, proxying concepts, caching strategies, lazy loading vs eager fetching, etc can not only cause performance issues, but also lots of frustrations during development.

Spring-data with Hibernate: Spring Data unifies the access to different kinds of persistence stores, both relational database systems and NoSQL data.

Q2. Where will you use ORM tools like Hibernate as opposed to using direct Spring JDBC Template?

1) If you have complete control of your database schema and if you don’t have an extremely high throughput requirement then Hibernate can work quite well when used properly by the developers who know what they are using. Never use it as a black box framework, which can lead to performance issues and frustrations during development.

2) ORM is a technique of mapping data representation from an object model to a SQL based relational model, and is well suited for “read, modify and write centric applications” and not suited for bulk write centric applications like batch processes with large data sets like 5000 rows or more where data is seldom read. Although this was generally true of many earlier ORM mapping frameworks, most today’s ORM tools including Hibernate allow for efficient ways of performing large batch style write operations.

Q3. What are the features & strengths of ORM tools?
A3. ORM tools/frameworks allow your application to be:

Less verbose (e.g. transparent persistence , Object Oriented query language , transitive persistence, etc)
More portable (i.e. vendor independence due to multi dialect support ).
More maintainable (i.e. transparent persistence, inheritance mapping strategies, automatic dirty checking etc).

Takes care of much of the plumbing like

1) Connection establishment & Multi-dialect support.
2) SQL code generation under the covers.
3) Cache management.
4) Optimistic locking.
5) Support for auditable fields like CreatedBy, UpdatedBy, CreatedDateTime, and UpdatedDateTime.
6) Soft deletes with @SQLDelete annotation.
7 Exception handling.

and you can often focus on the functional aspects. You can also leverage the framework’s strategies and capabilities to get efficiencies via features such as eager fetching, lazy loading (i.e. using proxy objects), caching strategies, detached objects to be passed to the other layers, dirty checking for optimistic concurrency control, etc.

Q4. Can you explain the Hibernate architecture?
A4. The Hibernate architecture is layered to keep you isolated from having to know the underlying APIs. Configuration, SessionFactory, Session, Transaction, Query, and Criteria are some of the Hibernate objects.

Hibernate Layered Architecture

Hibernate Layered Architecture

Q5. What is a SessionFactory object? Is it a thread-safe object?
A5.SessionFactory is Hibernate’s concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed & reused in an application code.

Q6. What is a Session object? Can you share a session object between different threads?
A6. Session is a light weight and a non-threadsafe object. So, you cannot share it between threads. Session represents a single unit-of-work with the database. Sessions are opened by a SessionFactory and then are closed when all work is complete. Session is the primary interface for the persistence service. A session obtains a database connection lazily (i.e. only when required). To avoid creating too many sessions, a ThreadLocal object can be used to have a session per thread. In JPA 2.0, you use an EntityManager instead of a Session.

Q7. What is the link between EntityManagerFactory/EntityManager and SessionFactory/Session?
A7. EntityManagerFactory and EntityManager are JPA (java Persistence API) specific. SessionFactory and Session are hibernate-specific.

Q8. Why favor JPA APIs like EntityManagerFactory and EntityManager over Hibernate APIs like SessionFactory & Session?

1) JPA API is a standard and evolving whereas Hibernate API is proprietary. So, use “javax.persistence.*” annotations as opposed to “org.hibernate.annotations.*”. Standard APIs in theory has better portability than proprietary APIs. This does not mean that you can easily port your application to use other ORM tools like TopLink or EclipseLink.

2) JPA API has better semantics than Hibernate as it has rectified some of the the early mistakes of Hibernate API.

3) The EntityManager invokes the hibernate session under the hood, and if you need some specific features that are not available in the EntityManager, you can obtain the session by calling:


4) EntityManagerFactory approach allows us to use callback method annotations like @PrePersist, @PostPersist, and @PreUpdate with no extra configuration. Using similar callbacks SessionFactory requires extra effort.

Q8 – Q15 15+ Hibernate basics Q8 – Q15 interview questions & answers

More Hibernate Interview Questions & Answers

1. Hibernate mistakes

2. Hibernate cache First & second level interview questions and answers

3. Hibernate automatic dirty checking

4. Hibernate entities with auditable, soft delete & optimistic locking fields

5. Understanding Hibernate proxy objects

6. Hibernate custom data type

7. 8 JPA interview questions and answers

Print Friendly
The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.
Arulkumaran Kumaraswamipillai

Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.

Posted in FAQs Hibernate, Hibernate
Tags: , , ,

600+ Java Interview Q&As ♥Free|♦FAQ

open all | close all

200+ Java Interview FAQs – Memory Joggers

open all | close all

16 Java Key Areas to be a top-notch

open all | close all

80+ Java Tutorials – Step by step

open all | close all

100+ Java Coding Exercises

open all | close all

How good are your "Career Skills"?

open all | close all