
We’ve compiled a list of some of the most popular Hibernate interview questions and Answers. If you’re searching for job, Hibernate is the most common tool that interviewers inquire about these days. If you are a new or experienced candidate, this is a fantastic place to start learning and preparing for an interview.
Table of Contents
What is ORM in Hibernate?
Object Relational Mapping (ORM) is an acronym for object-relational mapping. It’s the tool or approach we use to map data from a database to an object. We employ ORM technologies to easily create, modify, and retrieve data. Internally, the ORM interacts with the database via the Java API.
What is Hibernate and what is the purpose of using Hibernate?
Hibernate is a Java framework that makes it easier to construct Java applications. Hibernate is an object-relational mapping (ORM) solution that saves old Java objects to a database table.
The purpose of Hibernate is to eliminate the need for writing JDBC code. Hibernate enables us to concentrate solely on the business logic. We write less code and get greater results when we use Hibernate.
What are the most important advantages of Hibernate Framework?
- Hibernate is a lightweight and open-source tool.
- Hibernate has an extremely quick performance when compared to other systems.
- You’ll write less code and won’t have to deal with JDBC code.
- Hibernate produces database independent queries, allowing you to simply switch databases (for example, from Postgres to MySQL).
- Hibernate allows you to generate tables automatically. You don’t need to use the CREATE TABLE command or go to the database.
What are the main interfaces of Hibernate?
- Configuration
- Criteria
- Query
- Session
- SessionFactory
- Transaction
What exactly does “lightweight” mean?
When we talk about lightweight, we’re talking about an application, a computer program, or a gadget that doesn’t consume a lot of resources in the computer system. If a computer system has limited memory, the program will operate more smoothly and consume less CPU amount.
Why is Hibernate better than JDBC (Java Database Connectivity)?
- Hibernate code is cleaner and more readable than JDBC code.
- Hibernate does away with the boilerplate code present in JDBC.
- As you may be aware, the JDBC API does not support associations, collections, or inheritance, but Hibernate does.
- Working with Hibernate will be simple if you have a decent understanding of the OOPs idea.
- Hibernate makes it possible to develop applications more quickly.
- In the case of Hibernate, exception handling is optional.
What is a Session in Hibernate?
In Hibernate applications, the most frequently used interface is session. The connection between the database and the Java application is maintained by the session. You may construct queries using Session. To store, retrieve, change, and remove data from database you can use Session.
The methods in Session that may be used for database operations are listed below.
- persist()
- load()
- get()
- update()
- delete()
Session maintain the a first-level (mandatory) cache of persistent objects. There is a factory method available in Session that returns a Query, Criteria, and Transaction objects.
Click here to learn What is the difference between get and load method?
Is the Session object thread safe?
Thread safety is not provided by the Session object. In a single thread program, we should use sessions.
Each thread should have its own session object if you’re using multiple threads. Most essential, after the job is completed, the session should be closed.
What is a SessionFactory in Hibernate?
The factory class SessionFactory returns a Session object based on the configuration parameters. The SessionFactory is a heavy weight object. If you just have one instance of SessionFactory in your application, it’s a smart idea.
The data of the second level cache is stored in SessionFactory. We must enable the second level cache because it is not enabled by default.
Is the SessionFactory object thread safe?
Yes, SessionFactory is a thread-safe object that cannot be accessed by multiple threads at the same time.
What are object states in Hibernate?
In Hibernate, there are three types of object (instance) states:
- Persistent : When an object is associated to a Session, it is considered persistent state. To put it another way, the session is open, and all you’ve done so far is save and retrieve data from the database.
- Transient : When an object is created but never associated with a session, it is said to be in a transient state.
- Detached : It is considered an detached state if the session is closed and the object is not part of the session.
What are the two types of collections in Hibernate?
Sorted Collection and Ordered Collection are two types of collections in Hibernate.
# | Sorted Collection | Ordered Collection |
---|---|---|
1 | It is used to sort a group of items. Internally, it uses the Java Sorting API. | It uses order by clause while fetching the object. |
2 | It is enabled by default. | Need to enable explicitly. |
What is lazy loading?
The term “lazy loading” refers to a strategy in which objects are loaded as desired rather than the complete page.
What is Dirty Checking in Hibernate?
The benefit of dirty checking is that we don’t have to invoke the session manually Session. save() is a method for saving data.
In Hibernate, dirty checking is a important mechanism . This mechanism looks to see if any of the object’s fields have been changed in the same transaction. Hibernate will automatically execute the SQL update if any changes are made. It is also known as automatic dirty checking in hibernate.
How many different ways can an object from Hibernate’s database be retrieved?
Objects from the Hibernate database can be retrieved in one of four ways:
- HQL
- You can use Standard SQL
- By using the identifier.
- By using the Criteria API.
In Hibernate, how many different forms of association mapping are there?
In Hibernate, there are four different forms of association mapping.
- One to One
- One to Many
- Many to One
- Many to Many
List of collections that are used in one-to-many relationship mapping
The following is a list of collections that are used in one-to-many relationship mapping.
- Array
- Bag
- Set
- List
- Map
What are the advantages of using Hibernate templates?
Session ending will be handled automatically if you use the Hibernate template. You may make interaction with sessions simple by using a hibernate template. You’ll also benefit from automated exception handling.
What is HQL in Hibernate (Hibernate Query Language)?
HQL is a strong language that is database-independent. It’s an object-oriented system. HQL is identical to SQL, except that the it uses objects rather than table names.
You can easily build HQL commands if you have a decent understanding of OOP and a basic understanding of SQL.
Because HQL is database independent, queries created in it will work for any database. You can also refer to HQL as Object-Oriented Query Language.
Learn here the difference between HQL and SQL.
How to make an class immutable in Hibernate?
By marking class as mutable=false
List down some of the most important Hibernate annotations?
JPA annotations are supported by Hibernate, as we all know. The following are some of the more important annotations:
- @Entity : An entity is a type of object that has a distinct value. Entity is simply a POJO that represents data that may be stored in a database.
- @Table : We use @Table annotation to map class to table name in database.
- @Access
- @Id
- @GenerateValue
- @EmbeddedId
- @Column : To map an attribute to a database column, we use @Column.
- @OneToOne,
- @ManyToOne,
- @ManyToMany
- @JoinColumn
Recommended Articles
- Hibernate Interview Questions and Answers
- What is difference between GET and load in Hibernate?
- Spring Bean Scope with Example