Hibernate Interview Questions and Answers

Hello Friends,

Now a days its quite common thing that people are using ORM tools like Hibernate in Java. So if you are going to face an interview as a developer then you need to be ready with Hibernate as well. Gone are the days when people were writing native SQL queries in their Java Applications.

Now its Hibernate and ORM era so lets go through some quality questions on Hibernate.

1) What is benefit of using ORM tools ?

Ans - 

  • Improved productivity
  • No SQL
  • Sophisticated caching
  • Improved maintainability
2) What are the Core interfaces are of Hibernate framework ?

Ans - 
  • SessionFactory
  • Session
  • Transaction
  • Query
  • Configuration
3) What is the general flow of Hibernate communication with RDBMS ?

Ans - 
  • Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files
  • Create session factory from configuration object
  • Get one session from this session factory
  • Create HQL Query
4) What’s the difference between load() and get() ?

Ans - 
load() - 
Only use the load() method if you are sure that the object exists.
load() method will throw an exception if the unique id is not found in the database.
load() just returns a proxy by default and database won’t be hit until the proxy is first invoked.

get() - 
If you are not sure that the object exists, then use one of the get() methods.
get() method will return null if the unique id is not found in the database.
get() will hit the database immediately.

5) What is the difference between and merge() and update() ?

Ans - 
  • update(): if you are sure that the session does not contain an already persistent instance with the same identifier.
  • merge(): if you want to merge your modifications at any time without consideration of the state of the session.
6) What do you mean by Named – SQL query ?

Ans - Named SQL queries are defined in the mapping xml document and called wherever required.
Hibernate Named Query syntax is checked when the hibernate session factory is created, thus making the application fail fast in case of any error in the named queries.

Named native queries are possible with @NamedNativeQueries

7) Is Hibernate SessionFactory thread safe ?

Ans - Internal state of SessionFactory is immutable, so it’s thread safe. Multiple threads can access it simultaneously to get Session instances.

8) Is Hibernate Session thread safe ?

Ans - No. Its not thread safe.

9) What are different states of an entity bean ?

Ans - 

Transient: When an object is never persisted or associated with any session, it’s in transient state. Transient instances may be made persistent by calling save(), persist() or saveOrUpdate(). Persistent instances may be made transient by calling delete().

Persistent: When an object is associated with a unique session, it’s in persistent state. Any instance returned by a get() or load() method is persistent.

Detached: When an object is previously persistent but not associated with any session, it’s in detached state. Detached instances may be made persistent by calling update(), saveOrUpdate(), lock() or replicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge().

10) What is use of Hibernate Session merge() call ?

Ans - Hibernate merge can be used to update existing values, however this method create a copy from the passed entity object and return it. The returned object is part of persistent context and tracked for any changes, passed object is not tracked.

11) What will happen if we don’t have no-args constructor in Entity bean ?

Ans - Hibernate uses Reflection API to create instance of Entity beans, usually when you call get() or load() methods. The method Class.newInstance() is used for this and it requires no-args constructor. So if you won’t have no-args constructor in entity beans, hibernate will fail to instantiate it and you will get HibernateException.

12) Why we should not make Entity Class final ?

Ans - Hibernate use proxy classes for lazy loading of data, only when it’s needed. This is done by extending the entity bean, if the entity bean will be final then lazy loading will not be possible, hence low performance.

13) Which design patterns are used in Hibernate framework ?

Ans - Data Mapper – A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself.

Proxy Pattern for lazy loading.

Factory pattern in SessionFactory.

14) What is a dialect ?

Ans - Hibernate uses "dialect" configuration to know which database you are using so that it can switch to the database specific SQL generator code wherever/whenever necessary.

15) What is automatic dirty checking in hibernate ?

Ans - The automatic dirty checking feature of Hibernate, calls update statement automatically on the objects that are modified in a transaction.

16) What is lazy loading in hibernate?

Ans - Lazy loading in hibernate improves the performance. It loads the child objects on demand.

Since Hibernate 3, lazy loading is enabled by default, and you don't need to do lazy="true". It means not to load the child objects when the parent is loaded.

17) How can we get hibernate statistics ?

Ans - We can get hibernate statistics using getStatistics() method of SessionFactory class as shown below:

18) Difference between JPA and Hibernate.

Ans - 
  • JPA is the interface while Hibernate is the implementation.
  • JPA is the dance, Hibernate is the dancer.
  • Hibernate used HQL while JPA uses JPQL.
19) What is use of @ColumnDefination ?

Ans - columnDefinition will override the sql DDL generated by hibernate for this particular column.

Click Here to get Spring Framework with Social SignIn, Paypal, JMS course.

Check out some more posts that are related to this blog post.