Hibernate lazy Loading

Source: Internet
Author: User

What is lazy loading? What's his role?

Deferred loading, also known as lazy loading, is a mechanism that hibernate provides to improve program execution efficiency, which is only created when data is actually used for that object.

Hibernate is mainly implemented by proxy mechanism to implement lazy loading. It's specific process: hibernate when a database acquires an object's data, gets the collection property value of an object, or gets another object associated with an object, hibernate is not the database that loads the real data, because there is no data for that object. Instead of just creating a proxy object for the object to represent the object, all the properties on that object are the default values, and the real object is created only when you really need to use the object's data, actually loading its data from the database, which in some cases can improve query efficiency.

Lazy, deferred load

The duration of the lazy: only valid when the session is opened, and lazy when the session is closed.

The lazy policy can be used in:

* <class> Label: can be value true/false

* <property> tag, can be value true/false, this feature requires class enhancement

* On the <set>/<list> and other sets, you can take a value of True/false/extra

* <one-to-one>/<many-to-one> and other labels, can be value false/proxy/no-proxy

6.1 The difference between get and load:

* Get does not support lazy loading, while load is supported.

* When querying for data that does not exist in a particular database, GET returns NULL, while load throws an exception.

Deferred loading of Class 6.2:

* Set the Lazy= "true" in the <class> tab, or leave the default (that is, do not configure the Lazy property)

* If the property value of lazy is true, then when loading data using the Load method, the SQL statement is issued only when the data is actually used, which can reduce the overhead of the system.

*//Do not issue query SQL

System.out.println ("group id=" + Group.getid ());

Here's a question as to why you don't need to emit a SQL statement when you load a primary key.

6.3 Set (collection) Delay load: can be value True,false,extra

* Keep the default value of lazy on the set, the effect at this time and lazy= "Extra" is basically the same.

* Set the Lazy=extra on the collection, where the effect and the default value of the Lazy property are essentially the same. However, it is recommended to use this property value because it is more intelligent at the time of statistics. Of course, the delay is effective.

* Set the Lazy=false on the collection

True: The default value, which means that the query statement is emitted and the data of its collection elements is loaded only when the collection is called to get the element object inside.

False: Cancels the lazy load attribute, that is, while loading the object, the second query is emitted to load the data of its associated collection

Extra: A clever lazy loading strategy, called the Size/contains of the collection, hibernate

Does not load the entire collection of data, but instead emits a smart SQL statement to get the values needed, only to issue a query statement to load the data of all objects when the object data is actually needed for these collection elements

6.4 Hibernate single-ended associated lazy load policy: Configurable on <one-to-one>/<many-to-one> tab

Lazy load policy. You can take a value of: False/proxy/no-proxy

False: Cancels the lazy load policy, which is to issue a query statement and load its associated object while loading the object

Proxy: This is Hibernate's default lazy load policy for single-ended associations, that is, only when a method called to its associated object is actually issued a query statement queries its object data, and its associated object is the proxy class

No-proxy: This lazy loading feature requires the enhancement of the class, using No-proxy, whose associated object is not a proxy class

Note: The lazy attribute configured on the class label does not affect the associated object!!!

Proxy mode

Proxy mode is a very widely used design pattern, when the client code needs to call an object, the client does not really care whether or not to get the object accurately, as long as a can provide the function of the object, at this time we can return the object's agent (proxy).

In this design mode, the system provides a proxy object for an object, and the proxy object controls the reference to the source object. A proxy is a Java object that represents another Java object to take action. In some cases, the client code does not want or can not directly invoke the callee, and the proxy object can act as an intermediary between the client and the target object.

To the client, it can not distinguish between the proxy object and the real object, it does not need to distinguish between the proxy object and the real object. The client code does not know the real proxy object, the client code is interface-oriented, and it only holds the interface of a Proxied object.

All in all, as long as the client code cannot or does not want to access the called object directly-there are a number of reasons, such as the need to create an object with a high overhead, or the object being called on the remote host, or the target object's functionality is not sufficient to meet the requirements ..., but instead create a proxy object to return to the client using , then this design method is the proxy mode.

The default use of lazy loading in Hibernate is mainly due to the following types of cases:

(1) Lazy loading is used when the load () method on the session is called to load an entity.

(2) When a session loads an entity, deferred loading is applied to the value of the collection attribute in the entity.

(3) When a session loads an entity, a deferred load is applied to another entity object that is associated with the entity's single-ended.

In hibernate, you only need to modify the configuration of the response to enable or disable the Lazy load feature:

(1) When loading a single entity, you can use the Get () method of the session if you do not need to delay loading.

(2) When a session loads an entity, it does not need to delay loading the value of the collection attribute in the entity, but to load it immediately. You can then map the configuration elements in the file for this collection (<set>, <bag>, <list>, ...). ) to add the property lazy=false.

(3) When the session house is in an entity, there is no need to delay loading another entity object associated with the entity's single-ended, which can be used in the mapping file for this single-ended associated configuration element (<one-to-ong>, <many-to-one> ) to add the property lazy=false.

A, lazy Loading of entity objects:If you want to use lazy loading on an entity object, you must configure it in the entity's mapping profileB, deferred loading of collection types:In hibernate's lazy loading mechanism, the significance of the application for the collection type is most significant, as it is possible to significantly improve performance, and this is a great effort for hibernate, including the standalone implementation of the JDK collection, In a one-to-many association, we define the set of sets that hold the associated object, Rather than the Java.util.Set type or its subtypes, but the Net.sf.hibernate.collection.Set type, hibernate implements lazy loading of collection types by using the implementation of a custom collection class. C, attribute lazy loading:in Hibernate3, a new feature-deferred loading of attributes is introduced, which provides a powerful tool for obtaining high-performance queries. In the front we talked about big data object reads, there is a resume field in the user object, This field is a Java.sql.Clob type, contains the user's CV information, when we load the object, we have to load this field every time, regardless of whether we really need it, and this big data object's reading itself will bring a lot of performance overhead. In Hibernate2, we can only solve this problem by decomposing the user class with the granularity of the surface performance we've talked about (refer to the section), but in Hibernate3, we're able to use the property delay-loading mechanism to To get us to the ability to read this field data only when we really need to manipulate this field

Hibernate lazy Loading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.