Experience 14-hibernate Optimization 2-grab (fetch)

Source: Internet
Author: User
Tags emit ord

Use the crawl join query in the HQL statement to load data from the associated two entities at once from the database by writing a LEFT JOIN FETCH statement. This can improve query efficiency by reducing the number of SQL in specific cases (and also by using data from both entities). The query effects of the Get () and load () methods of the session are directly affected by configuring the crawl policy.

1. Single-end association <many-to-one><one-to-one> crawl strategy.

You can add a fetch attribute to a single end-associated mapping element. The Fetch property has 2 optional values.

Select: as the default, its policy is to send a separate SELECT statement to crawl the data of the associated object of the current object when it is necessary to use the data for the associated object. That is, deferred loading.

join: Its strategy is to use a connection in the same SELECT statement to obtain the data of the object and the data of its associated object, at which time the delayed loading of the associated object is invalidated.

2. Capture strategy on collection properties

You can add a fetch property on a mapped element of a collection property, which has 3 optional values.

Select: As the default, its policy is to send a separate SELECT statement to crawl the associated collection of the current object, which is deferred loading, when the data of the associated collection is needed.

join: Use a connection in the same SELECT statement to obtain an associated collection of each other. The lazy on the Association collection is now invalidated.

subselect: also sends a query statement (or subquery) to crawl the associated collection of all the entity objects that were previously queried. This strategy also works for HQL queries. When a fetch is joined, a left outer join is performed, at which point the customer's order value is loaded into the cache when the customer is loaded, and this strategy is a good choice if there is no large data in the order. When the fetch is subselect, it is valid for in and, if select, from the Customer where ID in (1,2,3), Hibernate takes out the ID and takes the value of the order individually, which is less efficient. This time the subselect efficiency is high, no matter how much data in the, in the query order is, will emit only one SQL statement.

setting the Batch-size in the <set> set to a more appropriate value is also equivalent to Subselect, and you can choose the number of times to emit the SQL statement based on the element of the project. HQL always ignores the pre-crawl policy set in the mapping file, which means that the FETCH keyword must be displayed when using the HQL in the pre-crawl. The difference, however, is that QBC does not ignore the prefetch strategy in the mapping file.

In the process of developing the project, we should not only choose the appropriate grasping strategy according to the actual situation, but also verify that the strategy is the most efficient through continuous testing.

Association level Retrieval Policy

3. Case study

mapping File:

<set name= "ord" table= "Orders" lazy= "false" batch-size= "2" >

<key column= "CID"/>

<one-to-many class= "Orders"/>

</set>

Test:

If you do not set fetch= "join", the default is Fetch= "select", that is, there will be two SQL statements, set the crawl will first crawl to the associated data, use the time directly, no longer from the database to get

@Test

publicvoid Testfetch () {

Session session = Hibernateutil.getsession ();

Customer cus = (customer) session.get (customer. Class, 3);

The following statement will also be exported after the SQL statement

System.out.println (Cus.getcname ());

set<orders> Orders = Cus.getord ();

System.err.println (Orders.size ());

Hibernateutil.close ();

}

Test fetch= "Subselect" and batch-size= "2";

@Test

publicvoid Testsubselect () {

Session session = Hibernateutil.getsession ();

list<customer> cus = Session.createquery ("from Customer C where c.id in (1,2,3)"). List ();

for (Customer C:cus) {

System.err.println (C.getcname ());

}

Hibernateutil.close ();

}

mapping File:

<many-to-one name= "cus" column= "CID" lazy= "false" fetch= "join"/>

Test:

@Test

publicvoid testhql () {

Session session = Hibernateutil.getsession ();

List<orders> ord = Session.createquery ("from Orders"). List ();

Hibernateutil.close ();

}

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.