Hibernate retrieval Policy

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 the hibernate ing file, you can configure the lazy attribute on <class> to determine the retrieval policy. For the session retrieval method, the Class-level retrieval policy applies only to the load method. That is to say, for get and qurey retrieval, persistent objects will be loaded immediately, regardless of whether lazy is false or true. in general, the object we retrieve 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; because the lazy attribute in <class> also affects multiple-to-one and one-to-one retrieval policies, 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: lazy = false, outer-join = true at this time, this search policy is only applicable to the ID retrieval method (load, get ), it is not applicable to query set retrieval (it uses an immediate retrieval policy ). Compared with instant retrieval, this retrieval policy reduces one SQL statement, but in hibernate, only one <set> can be configured as outer-join = true.

Multiple-to-one and one-to-one retrieval policies are generally configured using <allow-to-one> and <one-to-one>. The attribute to be configured in <operator-to-one> is outer-join, at the same time, you also need to configure the lazy attribute of the <class> associated with one end (not the lazy attribute in <role-to-one>). Their 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.

We can see that, by default (outer-join = auto, lazy = false), an urgent left Outer Join search is used for the associated one-side object hibernate. In my opinion, in many cases, we do not need to load the objects associated with one end (most likely we need only the ID of the associated object). In addition, if the associated object uses an urgent left outer join query, multiple Outer Join tables will appear in the SELECT statement. If the number is large, the query performance will be affected. This is why hibernate uses hibernate. max_fetch_depth attribute to control the depth of external connections. For an urgent left outer join query, the query set retrieval is not applicable. It uses an immediate retrieval policy.

You must select a search policy based on your needs. For instant retrieval and delayed retrieval, the SELECT statement is simple (one statement per table) and the query speed is fast. The disadvantage is that multiple select statements are required for table Association, increased the frequency of accessing the database. Therefore, you can use a batch search policy to reduce the number of select statements (configure the batch-size attribute) when selecting retrieval and delayed retrieval ). For left Outer Join search, the advantage is that the SELECT statement is relatively small, but the disadvantage is that the complexity of the SELECT statement is improved, and the association between multiple tables is a very time-consuming operation. In addition, the configuration file is dead, but the Program is active, you can display the specified search policy in the program as needed (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 the SQL statements executed by hibernate when the program is running.

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.