Hibernate framework Learning (10)--Query optimization

Source: Internet
Author: User

First, class-level query

1, get method: No policy, call immediately query database load data.

2, Load method: is executed without sending any SQL statements, return an object, use the object to execute the query, apply the class-level load policy.

1> Delay Loading: Only available, no use, no query, query when used
2> Lazy Loading of classes: You can control the load by configuring the Lazy property on the class element

Lazy (default): True when the query class returns a proxy object, the database is queried based on the associated session when the property is used, and the data is loaded

Do not query when loading, only query when using

lazy:false Query immediately when loading, no difference from Get method

Conclusion: Delayed loading (lazy loading) is recommended for increased efficiency

3> Note: When using lazy loading, make sure that the session is open when the call property loads the data, otherwise it throws an exception.

Second, relevance level query

1. Set Policy

Lazy property: Determines whether to delay loading
True (default): Lazy loading, lazily loading
False: Load Now
Extra: Extremely lazy, basically consistent with lazy loading effect
Fetch attribute: Determines the load policy, what type of SQL statement is used to load collection data
Select (default): Single-table query loading
Join: Loading a collection using a multiple-table query
Subselect: Loading a collection using a subquery

2. Association attribute Policy

Lazy attribute: Determines the loading time
False: Load Now
Proxy: Determined by the customer's class-level load policy

Fetch properties: Decide which SQL statement to load
Select: Use a single-table query
Join: Using multiple table queries

3. Conclusion

In order to improve efficiency, fetch should select Select,lazy should choose True, all using default values.

4, no-session problem solving

Expand the scope of the session

Third, Batch crawl

Batch-size: Fetching the number of collections
Grab a collection of contacts from several customers at a time when capturing a customer's collection

Hibernate framework Learning (10)--Query optimization

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.