Hibernate Interview questions and Answers

Last update: 11 Mar 2022, 24 Questions

Hibernate or Hibernate ORM is a framework and a mapping tool for Java. It is an object-relational mapping tool that gives an object-oriented domain model to the relational database. The structure is able to handle the problems of object-relational impedance mismatch by the replacement of persistent and direct database access with the functions of high-level object handling functions. There are several Hibernate Interview Questions which must be learned by the students to give their best shot and get the desired job.

Advantages of Hibernate

  • Database independent can work with DB2, SQL, MySQL, Oracle, etc.
  • Get the benefits of OOP Concepts such as encapsulation, inheritance, etc.
  • No requirement of hitting database for related queries, directly cache it and use it
  • Easy to write, support and read
  • Lazy loading is possible

Most Frequently Asked Hibernate Interview questions

Here in this article, we will be listing frequently asked Hibernate Interview questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

1. What is hibernate and why it is used?

It is an ORM developed for mapping tool for Java application program. It has been used in the development of Java application program as the primary function of Hibernate ORM is mapping the classes to the database tables and the types of Java data to SQL data types.

Other uses are-
  • Retrieving the data results
  • Writing data queries
  • Solves the problems of the mismatch of the traditional object and relational impedance issue
1 0
3. What is the Java Persistence API used for?

Java API or known as JPA is an interface specification of Java application program. It is used to describes the relational data’s management in the Java-based applications along with Enterprise Edition and Java platform.

1 0
4. How many types of transaction are there in hibernate?

There are four transactions in Hibernate ORM, and these are Atomicity, Consistency, Isolation, and Durability. Acronym used is ACID. These are the properties which every transaction follows.

0 0
6. What is flush in hibernate?

The procedure of synchronizing the persistence context state with the database is known as flushing. There are a set of methods sent by Hibernate Session and EntityManager with the help of which the persistence state of an entity can be changed by the application developer.

0 0
8. What is the use of evicting method in hibernate?

The evict method is used for detaching the object from hibernate provides evict () method and session cache. After this process, there is no persistence in the change of the object. The detachment of the associated objects can also take place with the mapping associated with the cascade=” evict.”

0 0
9. What is Hibernate session factory?

Hibernate SessionFactory is an interface, and it is created with the help of Configuration object. It consists of all the database related property details and in this, either hibernate.properties, the hibernate.cfg.xml file is used for pulling it. In the case of the increased number of multiple databases, then the requirement of creating one SessionFactory per database.

0 0
10. Why is SessionFactory used in hibernate?

Session Factory is used to configure objects which is further responsible for allowing instantiation of the Session object and turning the configuration Hibernation for supplied configuration file application. Threads of an application use SessionFactory in Hibernation.

0 0
11. What is the difference between hibernate save () saveOrUpdate () and persist () methods?
S.no Hibernate Save() SaveorUpdate method()
1. Work to generate a new identifier and then inserting the record into the database It either can UPDATE or INSERT because as per the existence of record.save() method can be failed with the persistence of the primary key
2. Used for bringing a transient object to a persisting state It can bring both new and existing, i.e., transient and detached into a persistent state or can be said that this method can be used for re-attaching a detached object into Session
0 0
12. What are the steps of calling a stored procedure in hibernate?
Write the following queries-

1. Use CreateNativeSQL Method for calling a procedure

  • Query query = session.createSQLQuery (“CALL GetAllFoos ()”).addEntity;
  • List allFoos= query.list();

2. Use @NamedNativeQueries to call a stored procedure

3. Use @NamedStoreProcedureQuery to call a stored procedure



Use @NamedNativeQueries to call a stored procedure



   name = "callGetAllFoos",

   query = "CALL GetAllFoos()",

      resultClass = Foo.class)



public class Foo implements Serializable {

         // Model definition


Use @NamedStoreProcedureQuery to call a stored procedure




     resultClasses = { Foo.class }



public class Foo implements Serializable {

 // Model Definition


1 0
14. What is the difference between SessionFactory and session in hibernate?
S.no SessionFactory objects Session
1. There is one SessionFactory object per application There is one Session object per client
2. Used for creation and management and it is threaded safe Sessions Session is not threaded safe and give CRUD interface to the mapped classes
0 0
15. List some of the database support by hibernate?
Hibernate use SQL databases for regularly testing by regularly handling in the Hibernate product lifecycle or Hibernate developers:
  • Oracle 11g, 11g RAC
  • MySQL 5.1, 5.5.
  • PostgreSQL 8.4, 9.1
  • DB2 9.7 or above.
  • Microsoft SQL Server 2008.
  • Sybase ASE 15.5 (jConnect 6.0)
0 0
16. Why is Hibernate called ORM?

Hibernate offers retrieval facilities and data query services along with generating SQL calls and relaxing the developer from handling the project manually and conversion of the object of the set of the result. These are the reasons which make Hibernate an object-relational model for the mapping tool for Java language.

0 0
17. What are the states of the object in hibernate?
Hibernate object states are as follows-
  • Detached Object State
  • Persistent Object State
  • Transient Object State
1 0
18. What are the main differences between persistent and transient objects?
S.no Persistent Objects Transient Objects
1. In this state, the database has the saved objects The objects are not kept in the database in this state of Hibernation
2. Example- An entity from a repository comes under persistent objects Until persisted, the creation of a new entity is called transients
0 0
19. Why do we need transactions in hibernate?

Transactions are required for protecting and maintaining the data and their consistency in Hibernate. Java application program framework, can modify several things in the databases, but transactions are needed for retaining the database and for this, there is an availability of Transaction Interface.

0 0
21. What is the default annotation for a property in hibernate?

The default annotation for a property in the Java framework is a @ld annotation, where Hibernate assumes that the annotation is on the object’s access properties and detects that it is on the field. Placing the annotation @ld on getId() method gives access to the features through getter and setter methods and that too by default.

0 0
22. What is a configuration in hibernate?

Configuration settings are required for the database and various other related parameters in Hibernate. The format for displaying such information is supplied as an XML file named hibernate.cffg.xml and hibernate.properties know as standard Java properties file.

0 0
23. What is Hibernate mapping file?

The object or relational mappings are the files which are defined in an XML document which instruct Hibernate in mapping the defined classes or class to the tables in the database.

0 0
24. What is the difference between JDBC and Hibernate?
S.no JDBC Hibernate
1. Standard API ORM (Object-Relational Mapping)
2. Java applications used JDBC for communicating with the database Used for mapping Java objects to a corresponding relational database.

To read more Hibernate Interview Questions, and make the concepts clear, you can visit the Best Interview Question.

0 0
Development History

Hibernate ORM was developed by Gavin King in 2001 as a replacement to using EJB2-style entity beans. The latest version is Hibernate ORM 5.4.0, and it was released in December 2018.

About Best Interview Question
Best Interview Question
Technical Consultant

With our 10+ experience in PHP, MySQL, React, Python & more our technical consulting firm has received the privilege of working with top projects, 100 and still counting. Our team of 25+ is skilled in distinct programming languages such as Python, Java, React.js, Angular, Node.js, PHP, HTML, CSS, Designing, iOS and Android apps, Database, .net, QA, Digital Marketing and Govt. jobs, etc. We are helping 10+ companies in solving their technical problems. When not found coding, our technical consultants can be seen helping out others.