Hibernate Interview Questions and Answers

What is Hibernate?

Hibernate is an Object/Relational Mapping solution for Java environments.

Hibernate not only takes care of the mapping from Java classes to database tables, but also provides data query and retrieval facilities.

What is Object Relational Mapping (ORM)?

Object/Relational Mapping refers to the technique of mapping data from an object model representation (Java Object) to a relational data model (database tables) representation.

Explain advantage and disadvantage of ORM model?

Advantages of ORM frameworks:

Database vendor independence: An ORM is abstracts layer for an application and gives you the portability to support multiple databases.

Reduction in development effort: An ORM frameworks manage metadata and API for database access and management,that increases productivity and speed up in coding.

Maintainability: An ORM work through configuration which allow sharing, reusability and fewer lines codes for DB operation that requires less maintenance.

Disadvantages of ORM frameworks:

Overhead: For simple and small applications, ORM framework can be overhead which required configuration, mapping and object management.

Performance impact: An ORM API do extra work to support efficient object state management with database table.

What are the advantage of Hibernate?Or Difference between JDBC and Hibernate?

Hibernate is database independent, same code will work for all kind of database like Oracle,MySql,SQLServerDerby, DB2PostGreSQL etc.

There is no need to create connection pools in case of Hibernate. Where in JDBC, connection pools need to be created.

Hibernate Supports Caching(1st level & 2nd level cache), that improve application’s performance andretains the objects in cache that reduce repeated hits to the database.While in JDBC you need to implement your java cache.

Development become fast,you don’t need to be an expert of writing complex and tune queries, as Hibernate take care of Query writing and tuning process .While in case of JDBC, its job of developer to write queries and tune them.

Hibernate support Lazy loading and Load objects only when required.

Explain hibernate architecture? Or Basic Hibernate APIs?

Hibernate architecture has following components:

Configuration (org.hibernate.cfg.Configuration):It represents database connection and O/R mapping setup. It will be used to build SessionFactory.

SessionFactory (org.hibernate.SessionFactory):It’s a Singleton, thread-safe and immutable cache of compiled mappings for a single database. SessionFactory instantiate Session object.

Session (org.hibernate.Session): A single-threaded, lightweight, short-lived object and designed to be instantiated on an interaction with database for persistent store.

Persistent objects and collections: Short-lived, single threaded objects containing persistent state and business function.These can be ordinary JavaBeans/POJOs and associated with org.hibernate.Session.

Transient objects and collections: Instances of persistent classes that are not currently associated with an org.hibernate.Session.

Transaction (org.hibernate.Transaction): A single-threaded, short-lived object used by the application to specify units of work and to maintain integrity in the database.

Query (org.hibernate.Query):It is an object oriented representation of Hibernate query that help w to retrieve data from database. A query instance is obtained by calling session.createQuery().

What is hibernate SessionFactory and Session?

SessionFactory: The SessionFactory is a heavyweight object, and there would normally be a single instance of this per application. It is thread-safe as it is created one per database and can be concurrently accessed by multiple threads. It is created using Configuration object and used to create session object.

Session: The Session is used to communicate between application and persistent store (database). This is factory for org.hibernate.Transaction. As it is not thread-safe, application should create and destroy it once a unit of work done though it may be used to multiple units as per requirement. It also represents a transaction in a database.

Is hibernate SessionFactory thread safe?

The SessionFactory is thread-safe as it is created one per database and can be concurrently accessed by multiple threads.

Is hibernate Session thread safe?

Session is not thread-safe, hence it is recommended that application should create and destroy it once a unit of work done.

What is POJOs?

POJO (Plain Old Java Objects) is a simple Java Bean class with some properties. This class uses standard Java Bean naming conventions with private visibility of properties and getter and setter methods of that.

What is caching and define it for ORM perspective?

Caching is one of the important features implemented by an application for better Performance. an ORM perspective, data retrieved from a database is cached in memory or to disk, so there is no need to hit database for every request.
Hibernate provides a way to configure caching at the class level, at the collection level, and also at the query result-set level.

Cached data is available at three different scopes in an application:

Transaction scope: data is fetched in one session, and the same query is executed again in that same session before that unit of work is completed. This data is stored in memory/disk by default, so the call to the database for the second query is avoided. Because a unit of work is for one request, this data isn't shared or accessed concurrently.

Process scope: Data is cached across sessions or across units of work. If a query is executed in one session, and the same query is executed in a second session after closing the first, this data is cached so that the second call to the database is avoided. Because two requests can access the same data in these two sessions, this data is accessed concurrently. You should use this scope with caution.

Cluster scope: Data is shared between multiple processes on the same machine or on different machines in a cluster.

Explain different caching strategies are in Hibernate?

Hibernate have following three different caching strategies:

First-level cache: It is at the transaction level and enabled by default in Hibernate. Caching at the first level is associated with a session. If the same query is executed multiple times in the same session, the data associated with the query is cached.

Second-level cache: It is configured at the process level or the cluster level. In Hibernate, you can configure it for a particular class or for a collection to improve performance. You can use the second-level cache with large and complex object graphs that may be loaded often. It is associated with one SessionFactory and can be reused in more than one Session created from the same SessionFactory.

Query result sets cache: It is useful when you run a particular query often with the same parameters. By default, the queries aren't cached and requires two additional physical cache regions that hold the cached query result sets and the timestamp when a table was last updated.

What is difference between transient,persistent and detached object in hibernate? Or Explain life cycle of an object in Hibernate?

Object in hibernate has following states:

Transient - Objects instantiated using the new operator are called transient objects.

An object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore.

Persistent - An object that has a database identity associated with it is called a persistent object.

A persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded; however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes.

Detached - A detached instance is an object that has been persistent, but its Session has been closed.

A detached instance can be reattached to a new Session at a later point in time, making it persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

What is removed state of object in hibernate?

A persistent object is considered to be in the removed state when a delete() or remove() operation is called on it. The delete() method is provided by a Hibernate session, whereas the remove() method is provided by the JPA.

How many ways we can query data from Database in Hibernate?

Hibernate provides three different queries way to retrieve data

  • Hibernate query language (HQL)
  • Native SQL queries
  • Criteria Query API
What’s HQL?

Hibernate supports an object oriented query language (HQL) which is an extension of SQL. It is a database independent and translated into SQL at runtime.

Hibernate query language is very efficient, simple and flexible query language to do various type of operations on relational database without writing complex database queries.

HQL syntax is not case sensitive but Java classes and properties names are case sensitive in HQL query.

Can we execute native SQL query in hibernate?

Hibernate does provide a way to use native SQL statements to supports special database features through its dialect of SQL that are not supported in HQL.

Hibernate provides createSQLQuery(String query) method to execute native SQL queries.

Latest Updates

Child Development & Pedagogy

The child will be able to use proper sentences by the age of?

Indian Politics

______is an institutional mechanism to provide representation to the states of India?

Books and Authors

Which text is not written by Kalidasa?

Indian Constitution

Which Constitutional Amendment introduced Right to Education as a Fundamental Right?

Indian History

Which animal is engraved on Harappan seals?

Indian Politics

Who is known for initiating electoral reforms in India to ensure free and fair elections?

Indian Constitution

The sequence in which the given terms are mentioned in the preamble to the Constitution of India is?

Which of the following parts of the Constitution of India provides fundamental rights ?

Books and Authors

Who among the following is the author of ‘Politics’?

Which one is the text not written by Abul Fazl?

Which King was the author of Ratnawali, Nagananda, Priyadarsika?

Fill in the blank

Fill in the blanks with the most appropriate preposition in the given sentence.
The class begins _______ 7:30 am _____ the morning.

The workers were so skilled that their _____ was quite visible even to a layman.


Choose appropriate article for the given sentence:
______ German is ___ easy language to learn for English speaking students.


What will be the synonym of ‘Eradicate’?

What will be the synonym of the word ‘disaster’?

Pipes and Cistern

Three taps A,B and C can fill a tank in 12, 15 and 20 hours respectively. If A is open all the time and B and C are open for one hour each alternatively, the tank will be full in?

Two pipes A and B can fill a tank in 6 hours and 4 hours respectively. If they are opened on alternate hours and if pipe A s opened first, in how many hours, the tank shall be full?

Latest Job Notifications

Banking Jobs

बैंकिंग कार्मिक चयन संस्थान Institute of Banking Personnel Selection(IBPS) - 7855 पद - 29/07/2022 अंतिम तिथि

SSC Recruitment

कर्मचारी चयन आयोग (एसएससी) Staff Selection Commission (SSC) - 1411 पद - 29/07/2022 अंतिम तिथि

Jobs in Rajasthan

राजस्थान अधीनस्थ और मंत्रिस्तरीय सेवा चयन बोर्ड Rajasthan Subordinate and Ministerial Services Selection Board (RSMSSB) - 5546 पद - 22-Jul-2022 अंतिम तिथि

Railway Recruitment

उत्तर मध्य रेलवे भर्ती प्रकोष्ठ Railway Recruitment Cell(NCR) - 1659 पद - 01/08/2022 अंतिम तिथि

Jobs in West Bengal

इंडियन पोस्ट सर्विस Indian Post Service - 2357 पद - 19/08/2021 अंतिम तिथि - पश्चिम बंगाल पोस्टल सर्कल

Banking Jobs

भारतीय स्टेट बैंक State Bank of India(SBI) - 6100 पद - 26/07/2021 अंतिम तिथि - CRPD/APPR/2021-22/10

SSC Recruitment

कर्मचारी चयन आयोग Staff Selection Commission(SSC) - 25271 पद - 31/08/2021 अंतिम तिथि

Jobs in Madhya Pradesh

राष्ट्रीय स्वास्थ्य मिशन मध्य प्रदेश National Health Mission MP(NRHM) - 5215 पद - 22/06/2021 अंतिम तिथि

Jobs in Punjab

पंजाब स्टेट पावर कॉर्पोरेशन लिमिटेड Punjab State Power Corporation Limited (PSPCL) - 2632 पद - 20/06/2021 अंतिम तिथि

Jobs in Chhattisgarh

स्वास्थ्य सेवा निदेशालय, छत्तीसगढ़ Department of Health & Family Welfare, Chhattisgarh - 267 पद - 26/06/2021 अंतिम तिथि