Hibernate Interview Questions & Answers for Freshers and Experienced
Master Hibernate concepts with practical interview questions covering ORM fundamentals, entity relationships, caching strategies, and real-world Hibernate usage in enterprise applications.
Top Hibernate Interview Questions for Freshers and Experienced
35 Questions
Easy · Medium · Hard
1 What is Hibernate?
Answer
Hibernate which is an implementation of JPA(Java Persistence API), is an ORM(Object Relational Mapping) tool which maps database tables with Java Objects and then provides various API’s to perform different types of operations on the data tables.
Did you know it?
2 Difference between JPA and Hibernate?
Answer
JPA is a specification for accessing , persisting and managing the data between java objects and the relational database. As the definition says, its API , it is only the specification. There is no implementation for the APIs.JPA is just the guidelines to implement the ORM and there is no underlying code for implementation.Whereas, Hibernate is the actual implementation of JPA guidelines.
Did you know it?
3 What is EntityManager?
Answer
EntityManager is an interface used to implement the persistence property to access entities in an application's persistent context.It provides the following 2 tpes of interfaces.
1.2. Application-managed Entitymanager
1.2. Application-managed Entitymanager
Did you know it?
4 What are the different approached to obtain entiryManager instance in Hibernate?
Answer
1. Using the container injection
2. Using the EntityManagerFactory interface
3. Looking up the EntityManager through JNDI.
2. Using the EntityManagerFactory interface
3. Looking up the EntityManager through JNDI.
Did you know it?
5 How do we create session factory in hibernate?
Answer
To create a session factory in hibernate, an object of configuration is created first which refers to the path of configuration file and then for that configuration, session factory is created as given in the example below:
Configuration config = new Configuration();
config.addResource("configuration.hbm.xml");
config.setProperties( System.getProperties() );
SessionFactory sessions = config.buildSessionFactory();
Configuration config = new Configuration();
config.addResource("configuration.hbm.xml");
config.setProperties( System.getProperties() );
SessionFactory sessions = config.buildSessionFactory();
Did you know it?
6 What is the significance of insertable and updatable property in column definition in hibernate?
Answer
Insertable and updatable parameter control the behaviour of the persistent provider. If the insertable parameter is set to false, then the specified field or property is not included in the insert statement.
If the updatable parameter is set to false, then the field or property of an entity can't be updated in the DB.
@Column(name = "CUST_ID", insertable = "false", updatable = "false")
If the updatable parameter is set to false, then the field or property of an entity can't be updated in the DB.
@Column(name = "CUST_ID", insertable = "false", updatable = "false")
Did you know it?
7 What is the behaviour of @Enumerated annotation in hibernate?
Answer
@Enumerated indicates that the field's persistent property should be stored in the form of an Enumeration. The possible values of EnumType can be ORDINAL or STRING.
@Enumerated(EnumType.ORDINAL) - Ordinal returns its posiion in its enum declaration, lke 0, 1, 2
@Enumerated(EnumType.STRING) - String value of the enum is saved in the DB.
@Enumerated(EnumType.ORDINAL) - Ordinal returns its posiion in its enum declaration, lke 0, 1, 2
@Enumerated(EnumType.STRING) - String value of the enum is saved in the DB.
Did you know it?
8 What is the behaviour of @Temporal annotation in hibernate?
Answer
The DATE, TIME, TIMESTAMP types can be used to map a temporal type.
@Temporal(TemporalType.DATE)
potected Date createdDate;
If no value is specified for the TemporalType parameter of the @Temporal annotations, the TIMESAMP i selected as default value.
@Temporal(TemporalType.DATE)
potected Date createdDate;
If no value is specified for the TemporalType parameter of the @Temporal annotations, the TIMESAMP i selected as default value.
Did you know it?
9 What’s HQL in Hibernate?
Answer
HQL is the query language used in Hibernate which is an extension of SQL. HQL is very efficient, simple and flexible query language to do various type of operations on relational database without writing complex database queries. We write HQL based on our entity model classes and their relationship.
Did you know it?
10 What are the benefits of using Hibernate template?
Answer
Following are some key benefits of using Hibernate template:
1. Session closing is automated.
2. Interaction with hibernate session is simplified.
3. Exception handling is automated.
1. Session closing is automated.
2. Interaction with hibernate session is simplified.
3. Exception handling is automated.
Did you know it?
11 How can we see hibernate generated SQL on console?
Answer
We need to add following in hibernate configuration file to enable viewing SQL on the console for debugging purposes:
<property name=”show_sql”>true</property>
<property name=”show_sql”>true</property>
Did you know it?
12 What the benefits are of hibernate over JDBC?
Answer
1.Hibernate can be used seamlessly with any type of database as its database independent while in case of JDBC, developer has to write database specific queries.
2.Using hibernate, developer doesn’t need to be an expert of writing complex queries as HQL simplifies query writing process while in case of JDBC, its job of developer to write and tune queries.
3.In case of hibernate, there is no need to create connection pools as hibernate does all connection handling automatically while in case of JDBC, connection pools need to be created.
2.Using hibernate, developer doesn’t need to be an expert of writing complex queries as HQL simplifies query writing process while in case of JDBC, its job of developer to write and tune queries.
3.In case of hibernate, there is no need to create connection pools as hibernate does all connection handling automatically while in case of JDBC, connection pools need to be created.
Did you know it?
13 What’s the usage of callback interfaces in hibernate?
Answer
These interfaces are used in the application to receive a notification when some object events occur. Like when an object is loaded, saved or deleted. There is no need to implement callbacks in hibernate applications, but they're useful for implementing certain kinds of generic functionality.
Did you know it?
14 How can we reattach any detached objects in Hibernate?
Answer
Objects which have been detached and are no longer associated with any persistent entities can be reattached by calling session.merge() method of session class.
Did you know it?
15 What are different ways to disable hibernate second level cache?
Answer
Hibernate second level cache can be disabled using any of the following ways:
1.By setting use_second_level_cache as false.
2.By using CACHEMODE.IGNORE
3.Using cache provider as org.hibernate.cache.NoCacheProvider
1.By setting use_second_level_cache as false.
2.By using CACHEMODE.IGNORE
3.Using cache provider as org.hibernate.cache.NoCacheProvider
Did you know it?
16 What are the collection types in Hibernate?
Answer
There are five collection types in hibernate used for one-to-many relationship mappings.
Bag
Set
List
Array
Map
Bag
Set
List
Array
Map
Did you know it?
17 The difference between sorted and ordered collection in Hibernate?
Answer
The main difference between sorted and ordered collection is that sorted collection sort the data in JVM's heap memory using Java's collection framework sorting methods while ordered collection is sorted using order by clause in the database itself. A sorted collection is more suited for small dataset but for a large dataset, it's better to use ordered collection to avoid OutOfMemoryError in Java application.
Did you know it?
18 what are the different entiry states in hibernate?
Answer
There are four entity states defined in hibernate:
New (transient): an entity is new if it has just been instantiated using the new operator, and it is not associated with a persistence context. It has no persistent representation in the database and no identifier value has been assigned.
Managed (persistent): a managed entity instance is an instance with a persistent identity that is currently associated with a persistence context.
Detached: the entity instance is an instance with a persistent identity that is no longer associated with a persistence context, usually because the persistence context was closed or the instance was evicted from the context.
Removed: a removed entity instance is an instance with a persistent identity, associated with a persistence context, but scheduled for removal from the database.
New (transient): an entity is new if it has just been instantiated using the new operator, and it is not associated with a persistence context. It has no persistent representation in the database and no identifier value has been assigned.
Managed (persistent): a managed entity instance is an instance with a persistent identity that is currently associated with a persistence context.
Detached: the entity instance is an instance with a persistent identity that is no longer associated with a persistence context, usually because the persistence context was closed or the instance was evicted from the context.
Removed: a removed entity instance is an instance with a persistent identity, associated with a persistence context, but scheduled for removal from the database.
Did you know it?
19 Which cache is used by Session Object in Hibernate? First level or second level cache?
Answer
A Session object uses the first-level cache.Second level cache is used at SessionFactory level.
Did you know it?
20 What is N+1 SELECT problem in Hibernate?
Answer
The N+1 SELECT problem is a result of lazy loading and load on demand fetching strategy. In this case, Hibernate ends up executing N+1 SQL queries to populate a collection of N elements. For example, if you have a List of N Items where each Item has a dependency on a collection of Bid object. Now if you want to find the highest bid for each item then Hibernate will fire 1 query to load all items and N subsequent queries to load Bid for each item. So in order to find the highest bid for each item your application end up firing N+1 queries.
Did you know it?
21 What are some strategies to solve the N+1 SELECT problem in Hibernate?
Answer
This is the follow-up question of previous Hibernate interview question. If you answer the last query correctly then you would be most likely asked this one. Here are some strategies to solve the N+1 problem:
1. pre-fetching in batches, this will reduce N+1 problem to N/K + 1 problem where K is size of batch.
2. subselect fetching strategy.
3. disabling lazy loading
1. pre-fetching in batches, this will reduce N+1 problem to N/K + 1 problem where K is size of batch.
2. subselect fetching strategy.
3. disabling lazy loading
Did you know it?
22 What are different types of caches available in Hibernate?
Answer
This is another common Hibernate interview question. Hibernate provides the out-of-box caching solution but there are many caches e.g. first level cache, second level cache and query cache.
1.>First level cache is maintained at Session level and cannot be disabled but the second level cache is required to be configured with external cache provider like EhCache.
2.second level cache is maintained at SessionFactory level and shared by all sessions
1.>First level cache is maintained at Session level and cannot be disabled but the second level cache is required to be configured with external cache provider like EhCache.
2.second level cache is maintained at SessionFactory level and shared by all sessions
Did you know it?
23 Does Hibernate Session interface is thread-safe in Java?
Answer
No, Session object is not thread-safe in Hibernate and intended to be used with-in single thread in the application.
Did you know it?
24 What is difference between openSession and getCurrentSession?
Answer
Hibernate SessionFactory getCurrentSession() method returns the session bound to the context. But for this to work, we need to configure it in hibernate configuration file. Since this session object belongs to the hibernate context, we don’t need to close it. Once the session factory is closed, this session object gets closed.
<property name="hibernate.current_session_context_class">thread</property>
Hibernate SessionFactory openSession() method always opens a new session. We should close this session object once we are done with all the database operations. We should open a new session for each request in multi-threaded environment.
<property name="hibernate.current_session_context_class">thread</property>
Hibernate SessionFactory openSession() method always opens a new session. We should close this session object once we are done with all the database operations. We should open a new session for each request in multi-threaded environment.
Did you know it?
25 Does SessionFactory is thread-safe in Hibernate?
Answer
SessionFactory is both Immutable and thread-safe and it has just one single instance in Hibernate application. It is used to create Session object and it also provide caching by storing SQL queries stored by multiple session. The second level cache is maintained at SessionFactory level. This can be a difficult and tricky question for less experienced Java developers who are not familiar with thread-safety and Immutability.
Did you know it?
26 What does mappedBy defines?
Answer
Defines the field which owns the relationship. This element is specified on the inverse(non-owning) side of the association. It is only needed when relationship is unidirectional.
Did you know it?
27 Which is owning side in entity mappong?
Answer
Owning side is the entity having foreign key column.
Did you know it?
28 What is Lazy initialiation in hibernate?
Answer
Lazy initialization means you don't initialize entire object, it only initializes the first level variables(member variable), t initializes the list when we access it. e.g. Supose an user have 100 list of addresses that he has visited and when we get details it only fetchesnthe first level variable but not the list.
Did you know it?
29 What is the difference between get() and load()?
Answer
Both are from session interfce and both will be used for retrieving object from DB.
Session.load(123) - it will return a proxy object with an unique identifier 123. It will hit the DB only when we try to retrieve the other properties of the object and if the object is not found in the DB it wil throw ObjectNotFoundException.
But when we call session .get(), it will directly hit the DB immmediately and returns the original object. If the object is not found in the DB, it returns null.
Session.load(123) - it will return a proxy object with an unique identifier 123. It will hit the DB only when we try to retrieve the other properties of the object and if the object is not found in the DB it wil throw ObjectNotFoundException.
But when we call session .get(), it will directly hit the DB immmediately and returns the original object. If the object is not found in the DB, it returns null.
Did you know it?
30 What is criterion query in hibernate?
Answer
Criteria is a simplified API for retrieving entities by composing Criterion objects also known as Criterion query. This is a very convenient approach for functionality like "search" screens where you can filter data on multiple conditions as shown in the following example:
List books = session.createCriteria(Employee.class)
.add(Restrictions.eq("name", "Dhiraj") )
.add(Restrictions.like("profile", "Lead"))
.addOrder(Order.asc("name") )
.list();
List books = session.createCriteria(Employee.class)
.add(Restrictions.eq("name", "Dhiraj") )
.add(Restrictions.like("profile", "Lead"))
.addOrder(Order.asc("name") )
.list();
Did you know it?
31 How to use application server JNDI DataSource with Hibernate framework?
Answer
For web applications, it’s always best to allow servlet container to manage the connection pool. That’s why we define JNDI resource for DataSource and we can use it in the web application. It’s very easy to use in Hibernate, all we need is to remove all the database specific properties and use below property to provide the JNDI DataSource name.
Did you know it?
32 What are the Collection types in Hibernate?
Answer
Following are collection types used in Hibernate.
Array
Map
Bag
Set
List
Array
Map
Bag
Set
List
Did you know it?
33 What is dirty checking in hibernate?
Answer
Dirty checking feature of the Hibernate allows users or developers to avoid time consuming data base write actions. This feature makes necessary updations and changes to the fields which require a change, remaining fields are left unchanged or untouched.
Did you know it?
34 Difference between persist() and Merge() in hibernte?
Answer
Persist takes n entity instance, adds it to context and makes that instance managed i.e. future updates to the entity will be traced.
Merge creates a new instance of your entity, copies the state from supplied entity and make the new copy managed.
Merge creates a new instance of your entity, copies the state from supplied entity and make the new copy managed.
Did you know it?
35 Types of inheritance in Hibernate?
Answer
Table Per Hierarchy - single table is required to map the whole hierarchy, an extra column (known as discriminator column) is added to identify the class.
Table Per Concrete class - tables are created as per class. But duplicate column is added in subclass tables.
Table Per Subclass - tables are created as per class but related by foreign key. So there are no duplicate columns.
Did you know it?
0 / 0 answered
