Hibernate framework Learning (10)-query optimization, hibernate framework

Source: Internet
Author: User

Hibernate framework Learning (10)-query optimization, hibernate framework
I. Class-level Query

1. get method: If there is no policy, the database load data is queried immediately after the call.

2. load Method: when an object is returned without sending any SQL statement, the query is executed only when the object is used. The load policy at the application class level is applied.

1> delayed loading: only obtained, not used, not queried, and used for query
2> whether to perform delayed loading: You can configure the lazy attribute on the class element to control

Lazy (default): When the Query Class is true, the proxy object is returned. When the attribute is used, the database is queried based on the associated session and the data is loaded.

It is not queried during loading. It is only queried during use.

 Lazy: Query immediately when loading with false, no difference with get Method

Conclusion: To improve efficiency, it is recommended to use delayed loading (lazy loading)

3> Note: Make sure that the session is opened when calling the property to load data. Otherwise, an exception is thrown.

2. Association-level Query

1. Set policies

Lazy attribute: determines whether to delay Loading
True (default): delayed loading and lazy loading
False: load immediately
Extra: extremely lazy, basically consistent with lazy loading
Fetch attribute: determines the load policy and type of SQL statements used to load set data.
Select (default): Single Table query Loading
Join: use multi-table queries to load a set
Subselect: Use a subquery to load a set

2. Associate an attribute Policy

Lazy attribute: determines the loading time
False: load immediately
Proxy: determined by the class-level loading policy of the Customer

Fetch attribute: determines the loaded SQL statement
Select: query using a single table
Join: Multi-Table query

3. Conclusion

To improve efficiency, select fetch, and select true for lazy. The default value is used for all fetch operations.

4. no-session Solution

Expand the scope of the session

3. Batch capture

Batch-size: Number of captured Sets
When capturing a customer's set, the customer's set of contacts is captured at a time.

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.