Top 30 Hibernate Interview Questions
Top 30 Hibernate Interview Questions
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.
more details...
https://www.javatpoint.com/hibernate-interview-questions 1/7
6/22/2020 Top 30 Hibernate Interview Questions - javatpoint
Configuration
SessionFactory
Session
Query
Criteria
Transaction
Management of transaction.
The objects of criteria are used for the creation and execution of the object-oriented criteria queries.
DB2
MySQL
Oracle
HSQL
PostgreSQL
FrontBase
Configuration
Session
SessionFactory
Criteria
Query
Transaction
Database Connection
https://www.javatpoint.com/hibernate-interview-questions 2/7
6/22/2020 Top 30 Hibernate Interview Questions - javatpoint
The SQL query is created with the help of the following syntax:
Session.createSQLQuery
The HQL query is created with the help of the following syntax:
Session.createQuery
Classes whose objects are stored in a database table are called as persistent classes.
SessionFactory provides the instance of Session. It is a factory of Session. It holds the data of second level
cache that is not enabled by default.
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...
https://www.javatpoint.com/hibernate-interview-questions 3/7
6/22/2020 Top 30 Hibernate Interview Questions - javatpoint
No, Session is not a thread-safe object, many threads can access it simultaneously. In other words, you can
share it between threads.
1) returns the identifier (Serializable) of the Return nothing because its return type is void.
instance.
The differences between get() and load() methods are given below.
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 sure about the It should be used if you are sure that
existence of instance. instance exists.
The differences between update() and merge() methods are given below.
2) update() should be used if the session doesn't contain an merge() should be used if you
already persistent state with the same id. It means an don't know the state of the
update should be used inside the session only. After closing session, means you want to make
the session, it will throw the error. the modification at any time.
...
SessionFactory factory = cfg.buildSessionFactory();
Session session1 = factory.openSession();
e1.setSalary(70000);
https://www.javatpoint.com/hibernate-interview-questions 4/7
6/22/2020 Top 30 Hibernate Interview Questions - javatpoint
Transaction tx=session2.beginTransaction();
session2.merge(e1);
tx.commit();
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...
If you mark a class as mutable="false", the class will be treated as an immutable class. By default, it is
mutable="true".
The automatic dirty checking feature of Hibernate, calls update statement automatically on the objects that
are modified in a transaction.
...
SessionFactory factory = cfg.buildSessionFactory();
Session session1 = factory.openSession();
Transaction tx=session2.beginTransaction();
e1.setSalary(70000);
tx.commit();
session1.close();
Here, after getting employee instance e1 and we are changing the state of e1.
https://www.javatpoint.com/hibernate-interview-questions 5/7
6/22/2020 Top 30 Hibernate Interview Questions - javatpoint
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
No, collection mapping can only be performed with One-to-Many and Many-to-Many.
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.
Hibernate Query Language is known as an object-oriented query language. It is like a structured query
language (SQL).
30) What is the difference between first level cache and second level cache?
https://www.javatpoint.com/hibernate-interview-questions 6/7
6/22/2020 Top 30 Hibernate Interview Questions - javatpoint
https://www.javatpoint.com/hibernate-interview-questions 7/7