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.
3) Explain hibernate architecture?
Hibernate architecture comprises of many interfaces such as Configuration, SessionFactory, Session, Transaction, etc.more details...
4) What are the core interfaces of Hibernate?
The core interfaces of Hibernate framework are:
5) Mention some of the advantages of using ORM over JDBC.
ORM has the following advantages over JDBC:
6) Define criteria in terms of Hibernate.
The objects of criteria are used for the creation and execution of the object-oriented criteria queries.
7) List some of the databases supported by Hibernate.
Some of the databases supported by Hibernate are:
8) List the key components of Hibernate.
Key components of Hibernate are:
9) Mention two components of Hibernate configuration object.
Class Mapping Setup
10) How is SQL query created in Hibernate?
The SQL query is created with the help of the following syntax:
11) What does HQL stand for?
Hibernate Query Language
12) How is HQL query created?
The HQL query is created with the help of the following syntax:
13) How can we add criteria to a SQL query?
A criterion is added to a SQL query by using the Session.createCriteria.
14) Define persistent classes.
Classes whose objects are stored in a database table are called as persistent classes.
15) What is SessionFactory?
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...
16) Is SessionFactory a thread-safe object?
Yes, SessionFactory is a thread-safe object, many threads cannot access it simultaneously.
17) What is Session?
It maintains a connection between the hibernate application and database.
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...
18) Is Session a thread-safe object?
No, Session is not a thread-safe object, many threads can access it simultaneously. In other words, you can share it between threads.
19) What is the difference between session.save() and session.persist() method?
20) What is the difference between get and load method?
The differences between get() and load() methods are given below.
21) What is the difference between update and merge method?
The differences between update() and merge() methods are given below.
Let's try to understand the difference by the example given below:
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.
22) What are the states of the object in hibernate?
There are 3 states of the object (instance) in hibernate.
23) What are the inheritance mapping strategies?
There are 3 ways of inheritance mapping in hibernate.
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".
25) What is automatic dirty checking in hibernate?
The automatic dirty checking feature of Hibernate, calls update statement automatically on the objects that are modified in a transaction.
Let's understand it by the example given below:
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.
26) How many types of association mapping are possible in hibernate?
There can be 4 types of association mapping in hibernate.
27) Is it possible to perform collection mapping with One-to-One and Many-to-One?
No, collection mapping can only be performed with One-to-Many and Many-to-Many.
28) What is lazy loading in hibernate?
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.
29) What is HQL (Hibernate Query Language)?
Hibernate Query Language is known as an object-oriented query language. It is like a structured query language (SQL).
The main advantage of HQL over SQL is:
30) What is the difference between first level cache and second level cache?