What is lazy loading in Java? I don't understand the process. Can anybody help me to understand the process of lazy loading?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
"Lazy loading" means that an entity will be loaded only when you actually accesses the entity for the first time.
The pattern is like this:
This saves the cost of preloading/prefilling all the entities in a large dataset beforehand while you after all actually don't need all of them.
In Hibernate, you can configure to lazily load a collection of child entities. The actual lazy loading is then done inside the methods of the
PersistentSet
which Hibernate uses "under the hoods" to assign the collection of entities asSet
.E.g.
.
Lazy setting decides whether to load child objects while loading the Parent Object.You need to do this setting respective hibernate mapping file of the parent class.Lazy = true (means not to load child)By default the lazy loading of the child objects is true.
Lazy loading allows you to defer the association retrieval or to have a better control over the fetching strategy.
When you use EAGER loading, you define a global fetch plan which cannot be overridden at query time, meaning you are limited to a decision you took while designing your entity model. The EAGER fetching is a code smell, because the fetching strategy is a query-time policy and it might differ from a business use case to another.
The fetching strategy is a very important aspect, as too much EAGER fetching can cause serious performance related issues.
Wikipedia
Link of Lazy Loading from hibernate.org
Lazy fetching decides whether to load child objects while loading the Parent Object. You need to do this setting respective hibernate mapping file of the parent class.
Lazy = true
(means not to load child) By default the lazy loading of the child objects is true.This make sure that the child objects are not loaded unless they are explicitly invoked in the application by calling
getChild()
method on parent.In this case hibernate issues a fresh database call to load the child whengetChild()
is actully called on the Parent object.But in some cases you do need to load the child objects when parent is loaded. Just make the lazy=false and hibernate will load the child when parent is loaded from the database.
Example : If you have a TABLE ? EMPLOYEE mapped to Employee object and contains set of Address objects. Parent Class : Employee class, Child class : Address Class
In the Employee.hbm.xml file
In the above configuration. If
lazy="false"
: - when you load the Employee object that time child object Address is also loaded and set to setAddresss() method. If you call employee.getAdress() then loaded data returns.No fresh database call.If
lazy="true"
:- This the default configuration. If you don?t mention then hibernate consider lazy=true. when you load the Employee object that time child object Adress is not loaded. You need extra call to data base to get address objects. If you callemployee.getAdress()
then that time database query fires and return results. Fresh database call.Lazy Loading? Well, it simply means that child records are not fetched immediately, but automatically as soon as you try to access them.