Hibernate Interview Questions & Answers

Posted On:June 12, 2019, Posted By: Latest Interview Questions, Views: 1185, Rating :

Best Hibernate Interview Questions and Answers

Dear Readers, Welcome to Hibernate Interview Questions and Answers have been designed specially to get you acquainted with the nature of questions you may encounter during your Job interview for the subject of Hibernate. These Hibernate Questions are very important for campus placement test and job interviews. As per my experience good interviewers hardly plan to ask any particular questions during your Job interview and these model questions are asked in the online technical test and interview of many IT companies. 

Hibernate Interview Questions 

1. What is Hibernate?  

Hibernate is a powerful, high performance object/relational persistence and query service. 

This lets the users to develop persistent classes following object-oriented principles such as association, inheritance, polymorphism, composition, and collections. 

 Interview Questions On Hibernate

2. What is ORM?  

ORM stands for Object/Relational mapping. It is the programmed and translucent perseverance of objects in a Java application in to the tables of a relational database using the metadata that describes the mapping between the objects and the database. It works by transforming the data from one representation to another.  


3. What does an ORM solution comprises of?  

• It should have an API for performing basic CRUD (Create, Read, Update, Delete) operations on objects of persistent classes  

• Should have a language or an API for specifying queries that refer to the classes and the properties of classes  

• An ability for specifying mapping metadata  

• It should have a technique for ORM implementation to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization 



4. What are the different levels of ORM quality?  

There are four levels defined for ORM quality.  

i. Pure relational  

ii. Light object mapping  

iii. Medium object mapping  

iv. Full object mapping  


5. What is a pure relational ORM?  

The entire application, including the user interface, is designed around the relational model and SQL-based relational operations.  


6. What is a meant by light object mapping?  

The entities are represented as classes that are mapped manually to the relational tables. 

The code is hidden from the business logic using specific design patterns. This approach is successful for applications with a less number of entities, or applications with common, metadata-driven data models. This approach is most known to all.  


7. What is a meant by medium object mapping?  

The application is designed around an object model. The SQL code is generated at build time. And the associations between objects are supported by the persistence mechanism, and queries are specified using an object-oriented expression language. This is best suited for medium-sized applications with some complex transactions. Used when the mapping exceeds 25 different database products at a time.  


8. What is meant by full object mapping?  

Full object mapping supports sophisticated object modeling: composition, inheritance, polymorphism and persistence. The persistence layer implements transparent persistence; persistent classes do not inherit any special base class or have to implement a special interface. Efficient fetching strategies and caching strategies are implemented transparently to the application.  


9. What are the benefits of ORM and Hibernate?  

There are many benefits from these. Out of which the following are the most important one.  

i. Productivity – Hibernate reduces the burden of developer by providing much of the functionality and let the developer to concentrate on business logic.  

ii. Maintainability – As hibernate provides most of the functionality, the LOC for the application will be reduced and it is easy to maintain. By automated object/relational persistence it even reduces the LOC.  

iii. Performance – Hand-coded persistence provided greater performance than automated one. But this is not true all the times. But in hibernate, it provides more 

optimization that works all the time there by increasing the performance. If it is automated persistence then it still increases the performance.  

iv. Vendor independence – Irrespective of the different types of databases that are there, hibernate provides a much easier way to develop a cross platform application.  


10. How does hibernate code looks like?  

Session session = getSessionFactory().openSession(); 

Transaction tx = session.beginTransaction(); 

MyPersistanceClass mpc = new MyPersistanceClass ("Sample App"); 




The Session and Transaction are the interfaces provided by hibernate. There are many other interfaces besides this.  


11. What are Extension interfaces?  

When the built-in functionalities provided by hibernate is not sufficient enough, it provides a way so that user can include other interfaces and implement those interfaces for user desire functionality. These interfaces are called as Extension interfaces.  


12. What is the file extension you use for hibernate mapping file?  

The name of the file should be like this : filename.hbm.xml The filename varies here. The extension of these files should be “.hbm.xml”. 

This is just a convention and it’s not mandatory. But this is the best practice to follow this extension.  


13. What do you create a SessionFactory?  

Configuration cfg = new Configuration(); 


cfg.setProperties( System.getProperties() ); 

SessionFactory sessions = cfg.buildSessionFactory();  

First, we need to create an instance of Configuration and use that instance to refer to the location of the configuration file. After configuring this instance is used to create the SessionFactory by calling the method buildSessionFactory().  


14. What should SessionFactory be placed so that it can be easily accessed?  

As far as it is compared to J2EE environment, if the SessionFactory is placed in JNDI then it can be easily accessed and shared between different threads and various 

components that are hibernate aware. You can set the SessionFactory to a JNDI by configuring a property hibernate.session_factory_name in the hibernate.properties file.


15. What are POJOs?  

POJO stands for plain old java objects. These are just basic JavaBeans that have defined setter and getter methods for all the properties that are there in that bean. Besides they can also have some business logic related to that property. Hibernate applications works efficiently with POJOs rather then simple java classes.  


16. What is object/relational mapping metadata?  

ORM tools require a metadata format for the application to specify the mapping between classes and tables, properties and columns, associations and foreign keys, Java types and SQL types. This information is called the object/relational mapping metadata. It defines the transformation between the different data type systems and relationship representations.  


17. What is HQL?  

HQL stands for Hibernate Query Language. Hibernate allows the user to express queries in its own portable SQL extension and this is called as HQL. It also allows the user to express in native SQL.  


18. What are the core interfaces of Hibernate framework?  

1. Session Interface: The basic interface for all hibernate applications. The instances are light weighted and can be created and destroyed without expensive process.

2. SessionFactory interface: The delivery of session objects to hibernate applications is done by this interface. For the whole application, there will be generally one SessionFactory and can be shared by all the application threads.

3. Configuration Interface: Hibernate bootstrap action is configured by this interface. The location specification is specified by specific mapping documents, is done by the instance of this interface.

4. Transaction Interface: This is an optional interface. This interface is used to abstract the code from a transaction that is implemented such as a JDBC / JTA transaction.

5. Query and Criteria interface: The queries from the user are allowed by this interface apart from controlling the flow of the query execution. 


19. What is a HibernateTemplate?  

HibernateTemplate is a helper class that is used to simplify the data access code. This class supports automatically converts HibernateExceptions which is a checked exception into DataAccessExceptions which is an unchecked exception. HibernateTemplate is typically used to implement data access or business logic services. The central method is execute(), that supports the Hibernate code that implements HibernateCallback interface.


20. What are the benefits of HibernateTemplate?  

The benefits of HibernateTemplate are: 

HibernateTemplate, which is a Spring Template class, can simplify the interactions with Hibernate Sessions. 

Various common functions are simplified into single method invocations. 

The sessions of hibernate are closed automatically 

The exceptions will be caught automatically, and converts them into runtime exceptions.


21. Explain the main difference between Entity Beans and Hibernate.  

Entity beans are to be implemented by containers, classes, descriptors. Hibernate is just a tool that quickly persist the object tree to a class hierarchy in a database and without using a single SQL statement. The inheritance and polymorphism is quite simply implemented in hibernate which is out of the box of EJB and a big drawback.


22. Explain the difference between hibernate and Spring.  

Hibernate is an ORM tool for data persistency. Spring is a framework for enterprise applications. Spring supports hibernate and provides the different classes which are templates that contains the common code.


23. Why hibernate is advantageous over Entity Beans & JDBC?  

An entity bean always works under the EJB container, which allows reusing of the object external to the container. An object can not be detached in entity beans and in hibernate detached objects are supported.

Hibernate is not database dependent where as JDBC is database dependent. Query tuning is not needed for hibernate as JDBC is needed. Data can be placed in multiple cache which is supported by hibernate, whereas in JDBC the cache is to be implemented. 


24. What is the difference between merge and update?  

update () : When the session does not contain an persistent instance with the same identifier, and if it is sure use update for the data persistence in hibernate.

merge (): Irrespective of the state of a session, if there is a need to save the modifications at any given time, use merge(). 


25. State the role of SessionFactory interface plays in Hibernate.

An application obtains Session instances from a SessionFactory which is typically single for the whole application created during its initialization. 

The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. 

It also holds cached data that has been read in one unit of work and may be reused in a future unit of work

SessionFactory sessionFactory = configuration.buildSessionFactory();