Hibernate Interview Questions and Answers

What is criteria API?

Criteria API allows you to build up a query object dynamically. The Criteria Query API lets you build nested, structured query expressions in Java, providing a compile-time syntax-checking that is not possible with a query language like HQL or SQL

Hibernate provided createCriteria() methods to create a Criteria object and Restrictions class for expression .

What is Named SQL Query?

Named query is features of Hibernate, in which queries can store in the mapping file or in an annotation on the class.It can be native SQL queries or HQL.
In mapping file, HQL queries can defined in XML <query>tag, and SQL queries can in <sql-query>tag.

Where getNamedQuery() method uses in Hibernate for named query, thst accept named query names and instantiated Query object from pulled result set.

How can we see hibernate generated SQL on console?

We can see hibernate generated SQL by set "show_sql" Hibernate property to true in the Hibernate configuration file hibernate.cfg.xml.

<property name="show_sql">true</property>  

What’s the difference between load() and get() method in hibernate?

load() methods retrieve data from database with provided identifier and if it doesn’t exist, an exception will throw.

get() methods doesn’t throw error and it will return a null value, if row doesn’t exist for provided identifier.

What’s the difference between save() and saveOrUpdate() methods in hibernate?

session.save() Saves an object to the database. This should not be called for an object that has already been saved to the database.

session.saveOrUpdate() Saves an object to the database, or updates the database if object already exists. This method is slightly less efficient than the save() method since it may need to perform a Select statement to check whether the object already or not.

What are various types of collection supported by Hibernate?

Hibernate support following type of collection:

  • Bag
  • Set
  • List
  • Array
  • Map

Latest Updates