Hibernate Interview Question and Answer

1. What is hibernate?

  • Hibernate is an open source, lightweight, java based ORM tool that provides framework for mapping application objects to database tables and vice versa that means to store, manipulate and retrieve data from database.
  • Hibernate provides option to map plain old java objects to database tables by using JPA annotations or XML based configuration.

2. What are the advantages of Hibernate?

  • Hibernate supports features like inheritance, associations, collections which are not present with JDBC.
  • Hibernate supports annotations, as well as XML
  • Hibernate removes a lot of boiler-plate code so looks cleaner and readable.
  • Hibernate supports caching mechanism for better performance.
  • Pagination in hibernate is simple.
  • Hibernate implicitly provides transaction management but In JDBC API, we need to write code for transaction management using commit and rollback.
  • JDBC API throws SQLException (checked exception)that is all are checked exception in JDBC, so we need to write code in try-catch block, but in hibernate there is only Un-checked exceptions that is throw JDBCException or HibernateException . In hibernate there is the translator which converts checked to Un-checked .
  • Hibernate has its own query language, i.e Hibernate Query Language (HQL) which is database independent and is more object oriented but need to write native sql queries for JDBC.
  • Hibernate configuration helps us to use JDBC like connection as well as JNDI DataSource for connection pool which is very important feature in enterprise application and completely missing in JDBC API.

3. What is ORM?

  • ORM is abbreviation of Object Relational Mapping.
  • It is a programming technique to map object with data in database.
  • It simplifies creation manipulation, access of data.

4. Advantages of ORM

  • Hides details of SQL queries .
  • No need to deal with database implementation.
  • Transaction management and automatic key generation etc.

5. Important interfaces of Hibernate?

  • Important or Core interfaces of Hibernate framework are:
  • Configuration
  • SessionFactory((org.hibernate.SessionFactory)
  • Session(org.hibernate.Session)
  • Transaction(org.hibernate.Transaction)
  • Query
  • Criteria

6. What is the difference between get() and load()?


1. load() throws ObjectNotFoundException if object is not found .
2. should use when we sure instance exists i.e. loads data only when actually required.
3. It returns proxy object.
4. It doesnot hit database.
5. load() is better as it supports lazy loading.


1. Returns null if object is not found.
2. It should be used if you are not sure about the existence of instance
3. It returns real object not proxy.
4. It always hits database

7. Difference between persist and save in hibernate?


1. save() returns the generated database identifier a Serializable object.
2.public Serializable save(Object object) throws HibernateException
3.save method is used inside or outside transaction boundaries
4.save() takes comparatively more time to execute than persist()


1. persist() doesn't return anything because its return type is void.
2.public void persist(Object object) throws HibernateException .
3. Persist method is used only within transaction boundary

8. What is the difference between first level cache and second level cache?

First Level Cache

First Level Cache is always associated with Session and enabled by default.

Second Level Cache

Second level cache is optional.
Second Level Cache is associated with SessionFactory and is not enabled by default.

9. What is hibernate configuration file?

  • Hibernate configuration file contains database specific configurations.
  • It is used to initialize SessionFactory.

10. What is hibernate mapping file?

  • It is used for defining entity bean fields and database table column mapping.

11. What is JPA?

  • JPA stands for Java Persistence API.

12. What is root node of hbm.xml?

  • < hibernate-mapping> as the root element which contains all the elements.

13. What is hibernate Dialect file ?

  • By using it hibernate knows database type and mapping files and class details.

13. What is SessionFactory?

  • Internal state of SessionFactory is immutable
  • SessionFactory provides instance of Session.
  • SessionFactory is a thread-safe object.
  • It holds the data of second level cache and is not enabled by default.

14. What is Hibernate Session?

  • Hibernate Session object is not thread safe.
  • It maintains connection between hibernate application and database.
  • It provides methods to store, update, delete or fetch data from database such as persist(), update(), delete(),get() etc.

15. Is Session a thread-safe object?

  • No, Session is not a thread-safe object.

16. What are different states of an entity bean or object in hibernate?

  • There are 3 states of object (an entity bean) in hibernate.
  • Transient : The object is in transient state when an object is never persisted or associated with any session and it is just created but has no primary key (identifier)
  • Persistent : Object is in persistent state, when it is associated with a unique session and session is open and you just saved instance or retrieved the instance from the database
  • Detached : When an object is previously persistent but not associated with any session and session is closed. After detached state,object turns to persistent state if you call lock() or update() method.

17. What are collection types in Hibernate

  • List
  • Array
  • Bag
  • Set
  • Map

18. Inheritance mapping strategies in hibernate?

  • There are 3 types of inheritance mapping.
  • 1. Table per hierarchy
    2. Table per concrete class
    3. Table per subclass

19. How transaction management works in Hibernate?

  • A transaction is associated with Session and instantiated by calling session.beginTransaction().
  • So after getting the session from SessionFactory, call sessionbeginTransaction() to start the transaction and this method returns Transaction reference that we can use later on to either commit or rollback the transaction.
  • Example of transaction management in hibernate.

  • Session ss = null; 
    Transaction ts = null; 
    try {
     ss = sessionFactory.openSession();
     ts = ss .beginTransaction();
     catch (Exception e) {
     finally {
      ss .close();

20. How to make a immutable class in hibernate?

  • If you mark a class as mutable="false", class will be treated as an immutable class.
  • By default mutable="true"

21. How many types of association mapping in hibernate?

  • One to One
  • One to Many
  • Many to Many
  • Many to One

22. How to implement Joins in Hibernate?

  • Using associations like One to One etc
  • Using native sql query and using join keyword.
  • Using JOIN in the HQL query.

23. Is it possible to perform collection mapping, One-to-One and Many-to-One?

  • No, collection mapping can only be possible with One-to-Many and Many-to-Many

24. What is lazy loading in hibernate?

  • Since Hibernate 3, lazy loading is enabled by default.
  • Lazy loading is done in hibernate by lazy="true" .
  • It loads the child objects on demand .
  • @OneToMany and @ManyToMany associations are by default to LAZY loading.
  • @OneToOne and @ManyToOne are by default to EAGER loading.
  • Lazy loading in hibernate improves the performance.

25. What is HQL ?

  • HQL stands for Hibernate Query Language.
  • It is known as an object oriented query language similar to SQL(structured query language).
  • Main advantage of HQL is:
  • No need to learn SQL
  • Database independent
  • Simple to write query
  • Supports polymorphic queries
  • Example to find maximum mark of student using HQL
  • Query q=session.createQuery("select max(mark) from Student");






javaglobe adsense

javaglobe adsense