Introduction to hibernate in Fetch and lazy

Source: Internet
Author: User

Fetch, which specifies how the associated object is crawled, you can set fetch = "SELECT" and fetch = "join". The Select method first queries the principal object (list) to be queried, and then according to the associated foreign key ID, each object sends a select query, obtains the associated object, forms the N+1 query , and joins the method, The principal object and the associated object are queried at the same time by the SQL associated with a foreign key, and no multiple queries are formed.

The crawl strategy defined in the mapping document will have the following effects:

(1) Obtain data through the get () or load () method.

(2) data is implicitly acquired (deferred fetch) only when navigating between associations.

(3) Explicitly declaring a connection fetch as a crawl strategy in the mapping document does not affect subsequent HQL queries.

Typically, we do not use mapping documents to customize the crawl strategy. More than that, keep its default value, and then, in a particular transaction, overload it with the left join fetch of HQL, which is used by the right-side fetching. This informs hibernate to use an external association (outer join) in the first query to get its associated data directly. In the conditional query API, you should call the Setfetchmode (fetchmode.join) statement.

Use a one-to-many example:

Fetch = "Select" is the query at the time of the first one end of the entity, and then the query based on one end of the entity, will produce 1+n SQL statement;

Fetch = "Join" is the query when the use of outer joins to query, will not be inferior 1+n phenomenon.

Lazy everyone should be familiar with, that is, lazy loading, you can set the lazy = "true", lazy = "false" lazy = "proxy".

The Join method set in the configuration file in the HQL query does not work (and is valid for all other query methods such as GET, criteria, or re-association fetching, etc.), unless you specify a join fetch in HQL with a related object.

Use lazy and fetch together:

1, when lazy= "true" fetch = "Select" Time, this time is the use of the delay strategy, began to query only one side of the entity, multi-terminal will not query, only when the use of the time will be issued SQL statements to query;

2, when lazy= "false" fetch = "Select" Time, this time is to make no delay policy, while querying out one end and multi-terminal, while generating 1+n SQL.

3, when lazy= "true"/lazy= "false" fetch = "join" time, I think this time delay is no use, because the use of the external connection query, at the same time the end and multi-terminal query out, the delay does not work.

Introduction to hibernate in Fetch and lazy

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.