Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hibernate is a framework in java that is used to map the objects that are stored in the database as we are mapping the objects from the database, so loading and initializing the data affects the performance of our software in the case of big data.
In this article, we are going to see how we use the lazy collection to improve the performance of our application.
Lazy Collection
As we have mentioned earlier, the lazy collection is used to improve the performance of our program. There are two types of fetching: one is lazy fetching, and another one is eager fetching. We switch between these two fetchings using the lazy collection.
Before understanding the lazy collection, we first need to understand both fetchings. So let’s get started.
Lazy fetching
In this type of fetching, the data is not initialized until it is actually needed.
Imagine we have a one-to-many relationship—for example, the course in the university and its students. A single course can have many students.
Using lazy fetching, if we load the course data at that time, we won't be loading the data of students. The student's data loading will be postponed until we explicitly call the function to load the student's data.
The advantage of this strategy is that we load less data at startup. The reason is that we might not require the course students, and since we don't intend to use them right away, there is no purpose in loading them.
Eager fetching
In eager fetching, all of the data is loaded at once. If we consider the above example, then using eager fetching the data for all the students will be loaded instantly with the course object.
The issue with eager fetching is that a lot of data is loaded that may not be required. As a result, we should only use it if we are certain that the fetched data will always be used once its object has been loaded.
How to enable or disable Lazy Collection
A few questions might arise in your mind: which fetching approach is used by lazy collection, and how we can turn on lazy collection in our application.
Lazy fetching is used by lazy collection, and starting from Hibernate 3.0, lazy collection is enabled by default, and we don’t need to do anything explicitly to enable the lazy collection in our application. In case we want to use eager fetching, we have to turn off the lazy collection in our application.
We can use the @LazyCollection annotation in our program to enable or disable it in our program.
Enabling Lazy Collection
We can enable the Lazy collection by simply setting the value of the LazyCollection annotation to false. We are basically telling our program to use lazy fetching by enabling the lazy collection.
In this example, the annotation can be skipped as the lazy collection is enabled by default.
@Entity
public class Course {
@Id
private Long course_id;
private String course_name;
@OneToMany(mappedBy = "course")
@LazyCollection(LazyCollectionOption.TRUE)
private List<Student> students;
}
Enabling in hibernate mapping file
Lazy collection can also be enabled in hibernate mapping file by setting the lazy attribute to “true”.
We can disable the Lazy collection by simply setting the value of LazyCollection annotation to false. We are basically telling our program to use eager fetching by disabling the lazy collection.
@Entity
public class Course {
@Id
private Long course_id;
private String course_name;
@OneToMany(mappedBy = "course")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Student> students;
}
That’s how we enable or disable the Lazy collection in our program.
Frequently Asked Questions
How do we switch between lazy fetching and eager fetching?
We can enable or disable lazy collection in order to switch between lazy fetching and eager fetching. If lazy collection is disabled in our program, then eager fetching will be used. Otherwise, lazy fetching will be used.
What is the purpose of Hibernate?
By maintaining the object-table mapping by itself and returning results to applications in the form of Java objects, Hibernate minimizes the number of lines of code. By relieving the programmer of manual constant data processing, it cuts down on both development time and maintenance costs.
What is the difference between lazy fetching and eager fetching?
In lazy fetching, the data is not initialized until it is actually needed. On the other hand, in eager fetching, all of the data is loaded and initialized at once.
Conclusion
In this article, we have extensively discussed the Lazy collection in hibernate. We have seen two different types of fetching. We have also seen how we can enable or disable lazy collection in our application.