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
Surprisingly, none of answers talk about how it is achieved by hibernate behind the screens.
Lazy loading is a design pattern that is effectively used in hibernate for performance reasons which involves following techniques.
1. Byte code instrumentation:
Enhances the base class definition with hibernate hooks to intercept all the calls to that entity object.
Done either at compile time or run[load] time
1.1 Compile time
Post compile time operation
Mostly by maven/ant plugins
1.2 Run time
2. Proxies
The entity object that Hibernate returns are proxy of the real type.
See also: Javassist. What is the main idea and where real use?
Well it simply means loading data you need currently instead of loading whole bunch of data at once which you won't use now. Thereby making application load time faster than usual.
Bydefault lazy loading is true.Lazy loading means when the select query is executed it will not hit the database. It will wait for getter function i.e when we required then ,it will fetch from the datbase. for example: You are a parent who has a kid with a lot of toys. But the current issue is whenever you call him (we assume you have a boy), he comes to you with all his toys as well. Now this is an issue since you do not want him carrying around his toys all the time. So being the rationale parent, you go right ahead and define the toys of the child as LAZY. Now whenever you call him, he just comes to you without his toys.
In layman's language, it is like you are making a cake and you will need 5-10 ingredients from fridge. You have two options, get all ingredients from fridge and put it on your kitchen platform, or bring the item you want when you need.
Similarly, in eager loading, you fetch all information about bean and its related classes (not child or is-a relation but has a relationship, i.e. cake has flour, has milk, has cream etc), and in case of lazy loading, first you bring only its identifier and values that are coming from same table (necessary ingredients that first you will need in your bowl in case of cake). All information that is coming from other tables will be fetched as and when required/used.
Say you have a parent and that parent has a collection of children. Hibernate now can "lazy-load" the children, which means that it does not actually load all the children when loading the parent. Instead, it loads them when requested to do so. You can either request this explicitly or, and this is far more common, hibernate will load them automatically when you try to access a child.
Lazy-loading can help improve the performance significantly since often you won't need the children and so they will not be loaded.
Also beware of the n+1-problem. Hibernate will not actually load all children when you access the collection. Instead, it will load each child individually. When iterating over the collection, this causes a query for every child. In order to avoid this, you can trick hibernate into loading all children simultaneously, e.g. by calling parent.getChildren().size().
Martin Fowler defines the Lazy Load pattern in Patterns of Enterprise Application Architecture as such:
So, when loading a given object, the idea is to not eager load the related object(s) that you may not use immediately to save the related performance cost. Instead, the related object(s) will be loaded only when used.
This is not a pattern specific to data access and Hibernate but it is particularly useful in such fields and Hibernate supports lazy loading of one-to-many associations and single-point associations (one-to-one and many-to-one) also under certain conditions. Lazy interaction is discussed in more detail in Chapter 19 of the Hibernate 3.0 Reference Documentation.