Hibernate lazy load and crawl policies

Source: Internet
Author: User

One, delayed loading

1. Simple query Get,load delay or immediate for the object itself

When using the Load method to get an object, Hibernate uses the mechanism of lazy loading to load the object, that is, when we use the Session.load () method to load an object, the SQL statement is not emitted at this time, and the currently obtained object is actually a proxy object , this proxy object only holds the ID value of the Entity object, and only when we want to use this object to get other properties, we will issue the SQL statement to query our object from the database.

The get is much more straightforward than load's lazy loading, and when we use the Session.get () method to get an object, regardless of whether we make the object non-use, the SQL statement is issued to query the database.

2. Delay or immediate of related objects, lazy= "True/false/proxy" through configuration

In hibernate, we can implement the delayed loading by some methods encapsulated by time-delay loading strategy, we can not only use the load () method to delay loading, but also the lazy attribute in the <property> element in the mapping file to implement this function. As shown

Note: When using get to load an object, it is not necessarily an immediate query, depending on what you are querying and the information in the configuration file. (The object in the get Load statement, of course. class must be executed immediately, just saying that when querying other tables through a foreign key, then those query statements are not necessarily immediately executed)

For example:

Lazy: The default is True (lazy loading), and when set to False, the query's statement is executed immediately.

Lazy also has a property value is proxy: (and select almost, automatically according to the actual situation, with whom to check who)

Second, crawl strategy connection fetching (join fetching)

Connection crawl, use the connection crawl can the original need to query two times (or multiple) table multiple query integration into only one query can be completed, for example, when querying multiple tables need to send at least two SQL statements, and if you use a join query, it will be queried according to the needs of the table and table linked to the check Query, only a single SQL statement can be all the required data back;

query Fetch (Select fetching)

query Fetching, this strategy is the default policy when the collection is crawled, that is, if the collection needs to be initialized, a SQL statement will be re-issued to query; This is the collection of the default crawl strategy, that is, we often appear n+1 query strategy;

subquery fetch (subselect fetching)

subquery fetch, and another SELECT statement fetches the associated collection of all entity objects that were previously queried (or crawled to)

Transferred from: http://www.cnblogs.com/hq233/p/6525475.html

Hibernate lazy Load and crawl strategy (RPM)

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.