Hibernate Interview Questions

By | | Updated : 2020-12-30 | Viewed : 4603 times

Hibernate Interview Questions

Nowadays Each Enterprise Application contains one of the ORM tools which can handle data modeling and all. The interviewer may check skills on one of the ORM tools like Hibernate in the interview. Hibernate contains lots of topics that are very vast. Here, Listed some of the interview questions on Hibernate (ORM), which are very helpful for cracking the interview.

1. What Is The Java Persistence API ?

Java Persistence API (JPA) is a specification for Accessing/Persisting/Manipulating data between Java Objects and Relational Database. This is introduced as a part of EJB 3.0

2. What Is Hibernate Framework?

It is an ORM tool that is used for mapping Java Object with Relational Database. To persist/access/manipulate the data from Relational Databases, Hibernate will be used. It provides HQL which is very helpful for handling CRUD operations in the Database. It is a Database Independent Query Language. Hibernate provides the caching technique.

3. What Are The Advantages/ Benefits Of Hibernate Over JDBC?

JDBC Hibernate
Queries are data base dependent HQL queries are database independent.
JDBC does not support for Lazy Loading Hibernate supports Lazy Loading
Developer need to write queries for Curd operations. Here we can use for Hibernate methods for CURD operations.
No support for Caching mechanism Supports Caching mechanism.
JDBC does not provide support for connection pool Hibernate provides support for connection pool
It will not support Identifier generation for primary key to insert the record It will support Identifier generation for primary key to insert the record
Caching mechanism cannot be used here. Caching mechanism can be used here

4. What are the Core Interfaces of Hibernate?

SessionFactory (org.hibernate.SessionFactory): SessionFactory is the core interface wherein all the modeling operations can be handled. It is used to create the Sessions. It is designed as an immutable class So it is threadsafe and will not be created more than one. The reason behind the making immutability is that the SessionFactory object can not be modified internally with multithreading. Each database of the application should be represented and handle data with one SessionFactory.

Session (org.hibernate.Session): As per the definition of Session in hibernate docs, the Session is single-threaded and a short-lived object. It is used for the operating DB transactions. It is created by the SessionFactory. In the creation of the Session, the wrapping of the JDBC transaction will have occurred.

Transaction (org.hibernate.Transaction): As per the document in Hibernate it is also a short-lived object that should be used with Session to handle DB operations. It is equal to the JDBC transaction which exists with the session only. When you have a session then only it is possible to create the Transaction for Data modeling operations.

5. Explain the Entity life cycle In Hibernate?

There are three types of States are existed in hibernate. Which are very helpful for applying the DB operations. These all three Entity States existed with respect to the Persistency Context association (Session in Hibernate and EntityManager in JPA).

Transient State: It is the initial state of an object in Hibernate. Here the object is not associated with Persistency Context ( i.e., Session in Hibernate). So no operations can be performed with the respective DB.

The API for making object as Transient State from Persistent State

Making object as Transient State
session.delete(entity);

Persistent State: Persistent State in which the object is associated with a Persistency Context. So the state in which the operations can be applied with respective DB.

The API for making object as Persistent State from Detached State

Making object as Persistent State
session.save(entity); 
session.persist(entity);
session.saveOrUpdate(entity);

The API for making object as Persistent State from Detached State

Making object as Persistent State
session.update(entity);  
session.saveOrUpdate(entity);  
session.lock(entity)

Detached State: The Detached State in which the object state of Persistent state is removed and moved to Detached State. So here no persistency Context association exists.

The API for making object as Detached State from Persistent State

Making object as Detached State
session.session.close();  
session.clear();  
session.detach(entity);  
session.evict(entity);

6. What are the differences get() and load()?

Scenario get() load()
Usage for fetching the table record as an entity object. for fetching the table record as an entity object by id.
If no record exists return null returnnoObjectException.
Loading Mechanism it is eager loading means the entire object will be fetched directly from database. It is lazy loading means the object will be coming from proxy. Whenever a user requests the object's remaining properties then the object will be fetched and loaded.
Usage Use when not sure of the existence of record Use when sure about the table records existence with respective id.

7. What are the differences between persist() and save()?

Scenario persist() save()
Return Type For persist() method return type is void. For save() method return type is identifier object after completion of insertion.
insertion Insertion will not performed immediately. It might be inserting at flush time Insertion will perform immediately.
Use Case Can be used in long-running conversations Should not use in long-running conversations.
Out side of Transaction Record can not be saved outside of transaction Record can be saved to DB out side of the Transaction.

8. What is the difference between merge() and update in Hibernate?

In simple words, With the update() method, the update operation will not be succeeded when the object is already within the session whereas the update() method will save the object whether the object exists or not in the same.

Scenario update() merge()
Operation update() method is used for updating the record merge() method is used for the same i.e., update operation.
Usage When session does not contain object with the same identifier then update() method is very useful. Object can be updated with irrespective of session state for object existence for particular identifier.
With Detached Object Object can not be updated Object can be updated.

9.What are the differences between save(), saveOrUpdate() and persist()?

save(): is used for persisting the object into the database with not existence identifier. Save() method will provide immediate insertion.

persist(): Using this method the object can be saved. Unlike save(), the persist() will not store the object into DB.

saveOrUpdate(): is used for saving an entity when the entity contains an identifier that does not exist in DB. If the identifier exists then the update operation can be performed.

10. Explain about Session Factory in Hibernate?

SessionFactory(org.hibernate.SessionFactory): SessionFactory is instantiated at the time of bootstrapping of Hibernate. It is instantiated with configurations which in all the DB and other configuration properties are placed. Each application should contain only one SessionFactory per Database. The main goal of SessionFactory is to maintain and create the sessions in the application.

11. Is Session Factory thread-safe in Hibernate?

Yes. SessionFactory is threaded safe. In a multithreading environment, it might be modified by a different number of threads. So SessionFactory is designed and implemented as an immutable object.

Leave A Reply