Hibernate Interview Questions & Answers for Freshers and Experienced
Hibernate interview questions with detailed answers on ORM, mappings, caching, HQL, and performance tuning for both freshers and experienced Java developers.
Top Hibernate Interview Questions for Freshers and Experienced Developers
Master Hibernate concepts with practical interview questions covering ORM fundamentals, entity relationships, caching strategies, and real-world Hibernate usage in enterprise applications.
35 Questions2 PagesEasy · Medium · HardPage 2 of 2
1
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
Did you know it?
2
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
Did you know it?
3
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?
4
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.
Did you know it?
5
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?
6
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?
7
Which is owning side in entity mappong?
Answer
Owning side is the entity having foreign key column.
Did you know it?
8
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?
9
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.
Did you know it?
10
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();
Did you know it?
11
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?
12
What are the Collection types in Hibernate?
Answer
Following are collection types used in Hibernate. Array
Map
Bag
Set
List
Did you know it?
13
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?
14
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.
Did you know it?
15
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.