Hibernate Interview Questions
Hibernate Interview Questions
Hibernate interview questions are asked to the students because it is a widely used
ORM tool. The important list of top 20 hibernate interview questions and answers for
freshers and professionals are given below.
1) What is hibernate?
Hibernate is an open-source and lightweight ORM tool that is used to store,
manipulate, and retrieve data from the database.
more details...
2) What is ORM?
ORM is an acronym for Object/Relational mapping. It is a programming strategy to
map object with the data stored in the database. It simplifies data creation, data
manipulation, and data access.
o Configuration
o SessionFactory
o Session
o Query
o Criteria
o Transaction
o DB2
o MySQL
o Oracle
o Sybase SQL Server
o Informix Dynamic Server
o HSQL
o PostgreSQL
o FrontBase
o Configuration
o Session
o SessionFactory
o Criteria
o Query
o Transaction
9) Mention two components of Hibernate configuration object.
Database Connection
Session.createSQLQuery
Session.createQuery
more details...
It provides methods to store, update, delete or fetch data from the database such as
persist(), update(), delete(), load(), get() etc.
more details...
4) It should be used if you are not sure about the existence of It should be used if you a
instance.
2) update() should be used if the session doesn't contain an already merge() shoul
persistent state with the same id. It means an update should be used state of the s
inside the session only. After closing the session, it will throw the error. the modificati
1. ...
2. SessionFactory factory = cfg.buildSessionFactory();
3. Session session1 = factory.openSession();
4.
5. Employee e1 = (Employee) session1.get(Employee.class, Integer.valueOf(101));
//passing id of employee
6. session1.close();
7.
8. e1.setSalary(70000);
9.
10. Session session2 = factory.openSession();
11. Employee e2 = (Employee) session1.get(Employee.class, Integer.valueOf(101));
//passing same id
12.
13. Transaction tx=session2.beginTransaction();
14. session2.merge(e1);
15.
16. tx.commit();
17. session2.close();
After closing session1, e1 is in detached state. It will not be in the session1 cache. So
if you call update() method, it will throw an error.
Then, we opened another session and loaded the same Employee instance. If we call
merge in session2, changes of e1 will be merged in e2.
more details...
24) 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".
1. ...
2. SessionFactory factory = cfg.buildSessionFactory();
3. Session session1 = factory.openSession();
4. Transaction tx=session2.beginTransaction();
5.
6. Employee e1 = (Employee) session1.get(Employee.class, Integer.valueOf(101));
7.
8. e1.setSalary(70000);
9.
10. tx.commit();
11. session1.close();
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.
1. One to One
2. One to Many
3. Many to One
4. Many to Many
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.
30) What is the difference between first level cache and second
level cache?