Hibernate Lazy Load parsing

Source: Internet
Author: User

In the hibernate framework, when the amount of data we want to access is too large, obviously the cache is not appropriate, because the memory capacity is limited, in order to reduce the concurrency and reduce the consumption of system resources, hibernate with lazy loading mechanism to compensate for this flaw, But it's just a fix, not a lazy load. Overall performance is improved.

What we call lazy loading is also called deferred loading, which does not immediately access the database at query time, but instead returns the proxy object, which accesses the database when the object is actually used.

Prerequisites for lazy loading:

1 entity classes cannot be final

2 objects that can be lazy loaded are proxy objects that are overwritten by Cglib (reflection calls), 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
The following types of lazy loading can be implemented:

1, through the session.load () to achieve lazy loading

Load (Object, Serializable): Query by ID. The query returns a proxy object that does not immediately access the database and is lazy-loaded. The database is accessed only when the object is actually being used.

When you use Load (), you will find that the query statement is not printed, and a query statement is printed when you use Get ().

If you use Load () to query this object after the session is closed, the exception is reported: Could not initialize proxy-no Session. Workaround: Initialize the queried object before the session closes: hibernate.initialize (user);

Using load () can improve efficiency because the database was not queried at first. But rarely used.

2, one-to-one (element) to achieve lazy loading.

On a one-to-one, the default is not lazy loading when querying the main object. That is, when querying the main object, it will also query from the object.

The main object needs to be formulated as lazy= "true" constrained= "true" fetch= "select". When querying the main object, the object is not queried, thus lazy loading is implemented.

On a one-time, the query from the object is lazy loading by default. That is, the query does not query the main object when it is from the object. Instead, the query is the proxy object of the main object.

3, Many-to-one (element) to achieve lazy loading.

When querying the main object, the default is lazy loading. That is, when querying the main object, it is not queried from the object.

When querying from an object, the default is lazy loading. That is, the query does not query the main object when it is from the object.

hibernate3.0 in lazy has three values, True,false,proxy, the default is lazy= "proxy". What is set to look at your needs is not to say which setting is the best. On <many-to-one> and <one-to-one> Tags: when True, there is lazy loading, and when false, n+1 problems arise, such as a student corresponding to a class, Using a SQL to isolate 10 students, hibernate will generate 10 SQL to identify each student's class when accessing the student's class attributes.

Lazy= when to take it?

Fetch= Capture Method: Select= Association query; join= Connection Table query (high efficiency)

Fetch=join, lazy settings will have no meaning.

4, One-to-many (Element) Lazy loading: Default will be lazy loading, which is necessary, is heavily used.

One-to-many times, the default is lazy loading when querying the main object. That is, when querying the main object, it is not queried from the object.

A one-to-many time, the query from the object when the default is lazy loading. That is, the query does not query the main object when it is from the object.

You need to configure the set collection in the Master object Lazy= "false" so that it is not lazy to load. Or configure the crawl mode fetch= "Join" can also become non-lazy loading.

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.

Direct Re-association

Session.lock (Fritz,lockmode.none);

Associating after a version check

Session.lock (Izi,lockmode.read);

Use SELECT ... For update after version Check Association

Session.lock (Pk,lockmode.upgrade);

Method Three:

Opensessioninview

See http://www.javaeye.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 to select,select 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 query The Join method is the Leftouter 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.

Hibernate lazy Load parsing (GO)

Related Article

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.