Hibernate interview questions and answers

Hibernate is most popular Object relational Mapping (ORM) framework and if you have used hibernate and has good experience on it , then you may face good interview question on hibernate. I am sharing some important interview questions here.

Tutorial Content:

1. What is ORM?

ORM stands for Object Relational mapping. It is programming paradigm which is used to persist java objects  to database tables.

2. What is Hibernate?

Hibernate is pure ORM tool which is used to save old java objects to database tables. The main goal of hibernate to avoid old JDBC code and focus more on business logic. You need to write less code with it.

3. What are advantages of Hibernate?

Advantages of Hibernate are:

  • Lazy Loading
  • Caching
  • You do not need to maintain JDBC code , Hibernate takes care of it.
  • You need to write less code
  • It provides high level object oriented API

4. Explain architecture of Hibernate?

Following is detail architecture of hibernate with core classes.
Hibernate Architecture
Hibernate is like a bridge between java code and relation database and provide object oriented API to deal with JDBC tasks.

5. What are some core interfaces of hibernate?

  • Session
  • SessionFactory
  • Configuration
  • Transaction
  • Query and Criteria interface.

6. Explain brief about Session interface used in hibernate?

Session interface is primarily used by hibernate application. Session is light weight,short lived objects which are inexpensive to create and destroy. It allows you to create query objects to retrieve persistent objects.It wraps JDBC connection Factory for Transaction.It holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier .

7. Are session objects thread safe?

Session objects are not thread safe and need to be used in single threads.

8. Explain brief about SessionFactory object used in hibernate?

SessionFactory is heavy weight object and it should be created one per database. SessionFactory object is shared by multiple sessions.

9. What is Configuration class in hibernate?

Configuration class is used to load required hibernate configuration. It is used to bootstrap hibernate and it is used to locate to hibernate mapping file.

10. What are differences between openSession and getCurrentSession in hibernate?

Session object
It always create new Session object
It creates a new Session if not exists , else use same session which is in current hibernate context
Flush and close
You need to explicitly flush and close session objects
You do not need to flush and close session objects, it will be automatically taken care by Hibernate internally
In single threaded environment , It is slower than getCurrentSession
In single threaded environment , It is faster than getOpenSession
You do not need to configure any property to call this method
You need to configure additional property “hibernate.current_session_context_class” to call getCurrentSession method, otherwise it will throw exceptions.

11. What are differences between get and load methods in hibernate?

Database retrieval
It always hits the database
It does not hit database
If null
If it does not get the object with id, it returns null
If it does get the object with id, it throws ObjectNotFoundException
It returns real object
It returns proxy object
If you are not sure if object with id exists or not, you can use get
If you are sure about existence of object, you can use load

12. Can you declare Entity class as final in hibernate?

Yes, you can declare entity class as final but it is not considered as a good practice because hibernate uses proxy pattern for lazy initialisation, If you declare it as final then hibernate won’t be able to create sub class and won’t be able to use proxy pattern, so it will limit performance and improvement options.

13. Differentiate between first level cache and second level cache?

First level Cache
Second level Cache
It is associated at Session level.
It is associated at SessionFactory level and is generally exists one per application.
It is enabled by default
It is not enabled by default, you need to enable it explicitly

14. What are states of object in hibernate?

Transient : If object is in transient state, it means it was never associated with session and just created.
Persistent :  If object is in persistent state, it means it is associated with session and you just saved or retrieved object from database.
Detached : If object is in detached state, it means session is closed and object is no more part of session. If you call merge or update, object goes back to persistent state.

15. Does entity class in hibernate require no arg constructor?

Yes, Entity class in hibernate requires no arg constructor because Hibernate use reflection to create instance of entity class and it mandates no arg constructor in Entity class.

16. What is HQL?

HQL stands for Hibernate query language. It is very simple, efficient and object oriented query languages which simplifies complex SQL queries. Instead of table, you use object to write queries.

17. What are differences between save and saveOrUpdate method of session object?

save:  Save stores object in database. It generates identifier for the object and returns it. If object already exists in database, it will throw an error.
saveOrUpdate : SaveOrUpdate method save the object if identifies does not exist. If it exists , it calls update method.

18. What are two types of Collections in hibernate?

  • Sorted Collection
  • Ordered Collection

19. What are differences between sorted and ordered collection in hibernate?

Sorted Collection
Ordered Collection
Sorted collection uses java’s sorting API to sort the collection.
Ordered Collections uses order by clause while retrieval of objects
It is enabled by default
It is not enabled by default, you need to enable it explicitly

20. What is lazy loading in hibernate?

It is used for performance improvement. Lazy loading means when you load parent, child objects won’t get loaded until requested. Infact hibernate loads child objects autamtically when you access them. It improves performance as you are not loading whole object at once.

21. What is LazyInitializationException in Hibernate? Why do you get it?

This exception generally occurs when you use lazy loading and child objects are accessed after closing session.

You may also like:

Add Comment