Goto: Hibernate lazy load in-depth analysis

Source: Internet
Author: User

can lazy loading improve performance ?
Can not simply say "can", because Hibernate's relational mapping dragged down the performance of SQL, so think of lazy loading to compensate. Just make up and not go beyond. So let's not think about the use of lazy loading overall performance is improved, in fact, the overall performance does not fall on the lucky.
----------------------------------------------------------
The lazy properties of hibernate can be configured in:

(commonly used) <set><list> on label, you can take a value True,false,extra
The default is true, when true, lazy loading, and then issuing SQL statements when accessing the collection properties. But Set.size () is not smart and does not generate a count () statement, but instead finds all records assigned to set.
Extra Description: When calling the collection's Set.size (), a SELECT count (*) from TableName is generated, which is more intelligent. Recommended Use
(less) <many-to-one><one-to-one> on label, you can take a value false,proxy,noproxy
When true, there will be lazy loading characteristics, when false will produce n+1 problems, such as a student corresponding to a class, with a SQL to identify 10 students, when accessing the student's class attributes hibernate will generate 10 SQL to identify each student corresponding class.
(not) <class> on label, you can take a value true,false
The default is true, when false, the load () method loses the lazy load property as with Get (), without affecting the lazy attribute on the collection (<set><list>) label
(not) <property> on label, you can take a value true,false
The default value is False, lazy loading a field, meaningless, do not use

----------------------------------------------------------
the difference between get () and load ()
Get () No lazy load feature, execute SQL query immediately.
Load () has lazy loading characteristics, will be returned to a proxy object, so never null, first do not execute SQL, to fetch the value of the object to execute the SQL statement, the previous session can not close the,<class> label lazy is not false.

----------------------------------------------------------
Prerequisites for lazy loading:1 PO cannot be final.
2 objects that can be lazy loaded (PO) are proxy objects that are overwritten by cglib, so they cannot be final decorated.
3 need to asm,cglib two jar packages
4 The corresponding lazy property is True
5 The corresponding Fetch attribute is select
----------------------------------------------------------
when to run out of lazy loading
1 Using Load ()
2 One-to-one <one-to-one>
The Master object uses join connection by default and lazy loading does not occur
Check from object default will occur lazy loading, first execute a select isolated from the object, when the main object by accessing from the object, then execute a select to isolate the main object.

More than 3 to one <many-to-one>
When using HBM.XNL, when taking more than one side, the default will be lazy loading, do not take a party
When using JPA, the default is to automatically use the join ON statement to remove one party (user and group, the user is a party, and the group is one party).

41 to More (<set><list>)
The default is lazy loading, which is required and is heavily used.

----------------------------------------------------------
To implement lazy loading scenarios:


Method One: (Lazy loading is not used)
Use Hibernate.initialize (De.getemps ()) to load it in advance.

Method Two:
Rebind the object that was detached from the session
The lock () method is used to allow the application to re-associate an unmodified object to the new session method.

[Java]View PlainCopy
    1. Direct Re-association <br>
    2. <br>
    3. Session.lock (Fritz, Lockmode.none);<br>
    4. <br>
    5. Post-version check correlation <br>
    6. <br>
    7. Session.lock (Izi, Lockmode.read);<br>
    8. <br>
    9. Use SELECT ... For update after version check correlation <br>
    10. <br>
    11. Session.lock (PK, Lockmode.upgrade);<br>
    12. <br>

————————————————————————————————————————————————————————————————————
Method Three:
Opensessioninview

http://www.iteye.com/topic/32001

—————————————————————————————————————————————————————————————————————

Fetch and lazy configure queries for data

The lazy parameter values are often false and the default lazy = True in the true,hibernate3 mapping file;

FETCH specifies the way in which the associated object is fetched, the parameter values are usually select and join, the default is select, the Select method first queries the main object, and then according to the associated foreign key, each object sends a select query, obtains the associated object, forms the N+1 times the query The Join method is the left outer join query, and the main object and the associated object are queried at the same time with the SQL associated with a foreign key, and no multiple queries are formed.

In the mapping file, different combinations use different queries:
1, lazy= "true" fetch = "select", using the delay policy, start querying only the main object, the association object will not query, only when the use of the time will be issued SQL statements to query;

2, lazy= "false" fetch = "select", do not use a delay policy, while querying out the main object and associated objects, resulting in 1+n SQL.

3, lazy= "true" or lazy= "false" fetch = "join", the delay will not work, because the use of an outer join query, while the main object and associated objects are queried.

In addition, in the HQL query, the join method set in the configuration file is not working, and in other query methods such as GET, criteria, etc. are valid, use Select Mode, unless a join is specified in HQL to fetch an associated object. Fetch policy is used to get/load an object, how to get a non-lazy object/collection. These parameters are not valid in query.

Goto: Hibernate lazy load in-depth analysis

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.