Detailed description of the Application of retrieval policies in hibernate

Source: Internet
Author: User

Hibernate's retrieval policies include class-level retrieval policies and associated-level retrieval policies.

Class-level retrieval policies include instant retrieval and delayed retrieval. The default retrieval policy is instant retrieval. In
In the hibernate ing file, you can configure the lazy attribute on <no class> to determine the retrieval policy. Class-level retrieval policies for session Retrieval Methods
It applies only to the load method. That is to say, for get and qurey searches, persistent objects will be loaded immediately regardless of whether lazy is false or true. In general, we retrieve objects.
Is to access it, So instant retrieval is a common choice. Because the load method will throw an exception when the object cannot be retrieved (in the case of immediate retrieval), I personally do not recommend using load retrieval;
The lazy attribute in <class> also affects multiple-to-one and one-to-one retrieval policies. Therefore, it is unnecessary to use the load method.

Association-level retrieval policies include instant retrieval, delayed retrieval, and urgent left Outer Join retrieval. Association-level search can be divided into one-to-many, multiple-to-many, multiple-to-one, and one-to-two cases.

The <set> configuration is generally used for one-to-many and many-to-many associations. <Set> the lazy and outer-join attributes are available, and their different values are definitely the retrieval policy.

1) instant retrieval: This is the default one-to-multiple retrieval policy. In this case, lazy = false and outer-join = false. although this is the default search policy, if the associated set is useless, do not use this method.

2) Delayed retrieval: lazy = true and outer-join = false (outer-join = true is meaningless). This is the preferred retrieval method.

3) Urgent left Outer Join search: at this time
Lazy = false, outer-join = true. This retrieval policy applies only to the ID-based retrieval method (load, get), but not to the query set retrieval.
(It uses an immediate search policy ). Compared with instant retrieval, this retrieval policy reduces one SQL statement, but in hibernate, there can only be one Set it to outer-join = true.

Multiple-to-one and one-to-one retrieval policies are generally used <
Configure-to-one> and <one-to-one>. The attribute to be configured in <role-to-one> is
For outer-join, you also need to configure the lazy attribute associated with the One-end <class> (not configured <
After-to-one>, the combined retrieval policies are as follows:

1) Outer-join = Auto: This is the default value. If lazy = true is delayed search, if lazy = false is left Outer Join search.

2) Outer-join = true. If there is no lazy, the search results are all urgent left Outer Join searches.

3) Outer-join = false. If lazy = true, the query is delayed. Otherwise, the query is immediately performed.

As you can see, by default (outer-join = auto, lazy =
False), which is used to search for the urgent left Outer Join of the associated one-side object hibernate. In my opinion, in many cases, we do not need to load the objects associated with one end (we may need
Only the ID of the correlated object is required. In addition, if the correlated object uses an urgent left outer join query, multiple Outer Join tables will appear in the SELECT statement, if the number is large, the retrieval performance will be affected,
This is why hibernate uses the hibernate. max_fetch_depth attribute to control the depth of external connections. For an urgent left outer join query
The retrieval method does not apply. It uses an immediate retrieval policy.

You must select a search policy based on your needs. For instant retrieval and delayed retrieval, their advantages are as follows:
The SELECT statement is simple (one statement per table) and the query speed is fast. The disadvantage is that multiple select statements are required when you associate a table, increasing the frequency of database access. Therefore, you can select retrieval and latency check.
You can use a batch search policy to reduce the number of select statements (configure the batch-size attribute ). For left Outer Join search, the advantage is that select is small, but the disadvantage is
It increases the complexity of the SELECT statement, and the association between multiple tables is time-consuming. In addition, the configuration file is dead, but the program is live. You can display the specified retrieval policy in the program as needed (possibly
It is often necessary to display the specified urgent left Outer Join search in the program ). To understand how the search policy is configured, you can configure the show_ SQL attribute to view
SQL statement.

 

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.