Hibernate Note
Hibernate Note
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
8) List the key components of Hibernate.
Key components of Hibernate are:
o Configuration
o Session
o SessionFactory
o Criteria
o Query
o Transaction
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.
It is a factory of Query, Criteria and Transaction i.e. it provides factory methods to return these
instances.
more details...
1) returns the identifier (Serializable) of the Return nothing because its return type is
instance. void.
No get() load()
.
2) get() method always hit the database. load() method doesn't hit the database.
3) It returns the real object, not the proxy. It returns proxy object.
4) It should be used if you are not It should be used if you are sure that instance
sure about the existence of instance. exists.
2) update() should be used if the session doesn't contain merge() should be used if you don't
an already persistent state with the same id. It means know the state of the session, means
an update should be used inside the session only. After you want to make the modification
closing the session, it will throw the error. at any time.
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 empl
oyee
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.
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.
more details...
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.