Hibernate Interview Questions
By AmarSivas | | Updated : 2020-12-30 | Viewed : 8642 times

Nowadays Each Enterprise Application contains one of the
1. What Is The Java Persistence API ?
Java Persistence API
2. What Is Hibernate Framework?
It is an
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?
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).
The API for making object as Transient State from Persistent State
session.delete(entity);
The API for making object as Persistent State from Detached State
session.save(entity);
session.persist(entity);
session.saveOrUpdate(entity);
The API for making object as Persistent State from Detached State
session.update(entity);
session.saveOrUpdate(entity);
session.lock(entity)
The API for making object as Detached State from Persistent 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()?
10. Explain about Session Factory in Hibernate?
11. Is Session Factory thread-safe in Hibernate?
Yes.