HibernateInterview questions

Hibernate interview questions

What is Hibernate ?

Hibernate is an ORM tool that maps POJOs to tables in relational database.

The mapping is done through a xml configuration file or through annotations.


What is ORM ?

Object Relational Mapping frameworks like Hibernate, IBatis etc provide mapping between code and tables.

This helps productivity as the developers can focus on their business logic rather than coding for database connectivity.

It provides improved performance through features like caching, lazy loading, eager loading etc.

The framework helps removing application’s dependency on particular database. The application code remains portable across databases.


Explain Session interface in Hibernate:

Session represents a conversation between the application and database.

It is single threaded.

It holds a mandatory first level cache of persistent objects.

Session object is obtained from SessionFactory.


What is SessionFactory ?

Session is obtained from SessionFactory.

Typically there is only one Session Factory for the whole application.


Different types of caches in Hibernate:

Hibernate uses two different type of caches for objects: first-level cache and second-level cache.

First level of cache is associated with Session object, while second-level of cache is associated with the SessionFactory object.

By default, Hibernate uses first-level of cache on a per-transaction basis. Hibernate mainly use this cache to reduce the number of SQL queries it needs to generate within a given transaction.


Difference between get() and load() methods in Hibernate:


– Use load() method only when you are sure that object you want to read already exists.
– If unique Id of an object does not exists in database then load() method will throw an exception.
– load() method return proxy object default and database won’t be hit until the proxy is first invoked.


– Use get() method when you are not sure about the object existence in the database.
– If object does not exists in the database, the get() method will return null.
– get() method will hit database immediately.


Difference between merge and update method :

Use update() method when you are sure that session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session.


Hibernate Query Language (HQL):

HQL provides an easier way to query the tables.

Uses Class name in place of Table name.. Property name in place of column name

See below for syntax of Select, Update and Insert operations.



Query query = session.createQuery(“from Employee where empid = :empid”);

query.setParameter(empid, “123”);

List empList = query.list();


Query query = session.createQuery(“from Employee where empid = ?”);

query.setParameter(0, “123”); //can also use setString() or setProperties(<object>)

List empList = query.list();





Query query = session.createQuery(“Update Employee set empname = “:empname” where empid=:empid”);

query.setParameter(empname, “abc”);

query.setParameter(empid, “123”);


int result = query.executeUpdate();



insert into tablename values(…) is not supported. Inserting into a table by selecting values from another table is supported.

Query query = session.createQuery(“Insert into employee(empid, empname) select empid,empname from back_emp”);

int result = query.executeUpdate();


What is Criteria api ?

Criteria api provides an oo and elegant approach compared to HQL.

Here an example of how to use criteria to select data from a table.

Criteria criteria = session.createCriteria(Employee.class);


criteria.add(Restrictions.eq(“emp_id”, “123”));



For pagination, maxresults and first result need to be set as follows :




Issue with this:

– Query scattered across code.

  • Can’t control the query generated by hibernate.


Named Queries:

Named queries are easier to maintain as they are added in the mapping xml files.

Query names should be unique.

Here is an example of a named query:

&lt;query name=”query1”&gt;

<![CDATA[ from employee where emp_id = :emp_id   ]]>


CDATA blocks avoids warning because of > or < symbol.

Named query should be added only after the Class element in the mapping file.


Here is how the Named query can be invoked in code:

Query query = session.getNamedQuery(“query1”);

query.setString(emp_id, “123”);



Native Queries:

Native Queries can be added similar to Named Queries. However, the query added is database specific. Normally, native queries are used when a query is already optimized by database developers and dbas and should be used as is in the application rather than depending on Hibernate to generate and optimize it.

Here is an example:

&lt;sql-query name=”query1”&gt;

<![CDATA[ select * from employee where emp_id = :emp_id   ]]


© 2015, www.techkatak.com. All rights reserved.

One thought on “Hibernate interview questions

Comments are closed.