Hibernate Interview Questions


11. What are the states of object in hibernate?


There are 3 states of object (instance) in hibernate.

  1. Transient: The object is in transient state if it is just created but has no primary key (identifier) and not associated with session.
  2. Persistent: The object is in persistent state if session is open, and you just saved the instance in the database or retrieved the instance from the database.
  3. Detached: The object is in detached state if session is closed. After detached state, object comes to persistent state if you call lock() or update() method.

12. How to make a immutable class in hibernate?


There can be 4 types of association mapping in hibernate.

  1. One to One
  2. One to Many
  3. Many to One
  4. Many to Many

13.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, it is mutable="true".


14.Is it possible to perform collection mapping with One-to-One and Many-to-One?


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

15.What is lazy loading in hibernate?


Lazy loading in hibernate improves the performance. It loads the child objects on demand. Since Hibernate 3, lazy loading is enabled by default, you don't need to do lazy="true". It means not to load the child objects when parent is loaded.

16. What is HQL (Hibernate Query Language)?


Hibernate Query Language is known as an object oriented query language. It is like structured query language (SQL).

  • You don't need to learn SQL
  • Database independent
  • Simple to write query

17 .What is Hibernate Query Language (HQL)?


Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate query Language (HQL), is an object-oriented extension to SQL.

18 .How do you map Java Objects with Database tables?


  • First we need to write Java domain objects (beans with setter and getter).
  • Write hbm.xml, where we map java class to table and database columns to Java class variables.

19 What is the difference between load() and get()?

load()
Only use the load() method if you are sure that the object exists. If you are not sure that the object exists, then use one of the get()methods.
load() method will throw an exception if the unique id is not found in the database. get() method will return null 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() will hit the database immediately.

20 .What is the difference between and merge and update ?

Use update() if you are sure that the 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.