Fetch and hibernatefetch of Hibernate

Source: Internet
Author: User

Fetch (conversion) and hibernatefetch of Hibernate

Fetch, you can set fetch = "select" and fetch = "join"

Use one-to-multiple examples:
Fetch = "select" is to first query the objects at one end during the query, and then query the objects at multiple ends based on one end, one or more SQL statements are generated;
Fetch = "join" refers to the use of external connections for queries during queries, without the occurrence of 1 + n.

Lazy should be familiar to everyone, that is, delayed loading. You can set lazy = "true", lazy = "false" lazy = "proxy ".
The main problem I want to talk about here is that lazy and fetch are used in combination:
1. When lazy = "true" fetch = "select", a latency policy is used to query only one object at a time, but not multiple objects, an SQL statement is issued for query only when it is used;
2. When lazy = "false" fetch = "select", the latency policy is not used, and one or more SQL statements are generated at the same time.
3. When lazy = "true"/lazy = "false" fetch = "join", I think the latency is useless, because the query of external connections is used, and one end and multiple ends are queried at the same time, the delay does not work.

Some analysis of fetch = "join" and fetch = "select" in Hibernate 

The fetch parameter specifies whether the associated object can be crawled by select or join queries. When the select method is used, the system first queries and returns the subject object (list) to be queried, and then based on the associated foreign key id, each object sends a select query to obtain the associated object, forming n + 1 query;
In the join mode, the subject object and associated object are queried at the same time using an SQL statement associated with a foreign key, and no query is performed multiple times.
If your associated object is delayed, it certainly does not query the associated object.
In addition, the join method set in the configuration file in the hql query does not work (and all other query methods, such as get, criteria, and re-Association, are valid ), the select method is used unless you specify an associated object of joinfetch in hql.

 

The fetch policy defines how to obtain non-lazy objects/sets when get/load an object. These parameters are invalid in Query.

The fetch policy defines how to obtain non-lazy objects/sets when get/load an object. These parameters are invalid in Query.

 

Query crawling (default) is not good in the case of N + 1 queries, so we may need to define connection crawling in the ing document:

 

<Set name = "permissions"
Fetch = "join">
<Key column = "userId"/>
<One-to-manyclass = "Permission"/>
</Set
<Role-to-one name = "mother" class = "Cat" fetch = "join"/>
The capture policy defined in the ing document will have the following impact:

Get data using get () or load () methods.

Data is implicitly acquired (delayed capturing) only when the association is navigated ).

Conditional Query

Explicit declaration of connection capturing as a crawling policy in the ing document does not affect subsequent HQL queries.

Generally, we do not use ing documents to customize crawling policies. More importantly, keep its default value, and then use the left join fetch of HQL to reload it in a specific transaction. This will notify Hibernate to use external join in the first query to directly obtain the associated data. In the conditional query API, call the setFetchMode (FetchMode. JOIN) statement.

In fact, this does not mean that hql can be crawled according to the join set in the configuration file. At this time, the level 2: topic --> forum capture is actually irrelevant to hql, this is because another select method has been generated before.
It is the association of objects. If the topic is set to delayed loading when the message is queried, the message is obtained later. topic, such as topic. if forum does not delay loading, topic --> forum captures the configured join mode, which is obviously irrelevant to hql queries.

 

Address: http://qinhanbin.iteye.com/blog/520038

Http://sun-gufeng-126-com.iteye.com/blog/629261

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.