Home / Interview / Hibernate :: General Questions

Interview :: Hibernate

21) What are the states of the object in hibernate?

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

  1. Transient: The object is in a transient state if it is just created but has no primary key (identifier) and not associated with a session.
  2. Persistent: The object is in a persistent state if a 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 a detached state if a session is closed. After detached state, the object comes to persistent state if you call lock() or update() method.
22)

What are the inheritance mapping strategies?

There are 3 ways of inheritance mapping in hibernate.

  1. Table per hierarchy
  2. Table per concrete class
  3. Table per subclass

23) How to make an immutable class in hibernate?

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

24) What is automatic dirty checking in hibernate?

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

Let's understand it by the example given below:

Here, after getting employee instance e1 and we are changing the state of e1.

After changing the state, we are committing the transaction. In such a case, the state will be updated automatically. This is known as dirty checking in hibernate.

25) How many types of association mapping are possible 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
26) 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.

27) 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, and you don't need to do lazy="true". It means not to load the child objects when the parent is loaded.

28) What is HQL (Hibernate Query Language)?

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

The main advantage of HQL over SQL is:

  1. You don't need to learn SQL
  2. Database independent
  3. Simple to write a query
29) What is the difference between first level cache and second level cache?
No.First Level CacheSecond Level Cache
1)First Level Cache is associated with Session.Second Level Cache is associated with SessionFactory.
2)It is enabled by default.It is not enabled by default.