Hibernate-retrieval policy and hibernate retrieval Policy

Source: Internet
Author: User

Hibernate-retrieval policy and hibernate retrieval Policy

I. The search policy of Hibernate is designed to optimize the performance of Hibernate.

2. Hibernate search policies include class-level search policies and association-level retrieval policies (<set> element)

Iii. Category-level search policies

1. Instant retrieval and delayed Retrieval

2. control by using the lazy attribute of the <class> node. The default value is true, that is, delayed retrieval.

3. Only the load () method for the session takes effect.

By default, the object obtained through the load () method is a proxy object. When Hibernate creates a proxy object, only the OID is initialized.

When you access non-OID attributes for the first time, Hibernate initializes the proxy instance.

4. Test

(1) The lazy attribute of <class> is true.

@Testpublic void testStrategyClassLevel() {    Customer customer = (Customer) session.load(Customer.class, 5);    System.out.println(customer.getClass());    System.out.println(customer.getCustomerId());    System.out.println("--------------");    System.out.println(customer.getCustomerName());}
class com.nucsoft.strategy.many2one.Customer_$$_javassist_05--------------Hibernate:     select        customer0_.customer_id as customer1_2_0_,        customer0_.customer_name as customer2_2_0_     from        hibernate.customer customer0_     where        customer0_.customer_id=?bb

(2) The lazy attribute of <class> is false.

@Testpublic void testStrategyClassLevel() {    session.load(Customer.class, 5);}
Hibernate:     select        customer0_.customer_id as customer1_2_0_,        customer0_.customer_name as customer2_2_0_     from        hibernate.customer customer0_     where        customer0_.customer_id=?

Iv. Association-level search policies

1. It refers to the many-to-one and many-to-many associations configured with the <set> element.

2. It mainly refers to three attributes of the <set> element: lazy, fetch, and batch-size.

3. lazy attribute (the default value is true, that is, the latency search policy is used)

(1) determines the time when the set is initialized.

(2) When the value is true

  • Hibernate will initialize a set proxy instance in the following circumstances
    • When you access collection attributes, iterator (), size (), isEmpty (), and contains (),
    • Initialize the display using the Hibernate. initialize () Static Method

(3) When the value is false

(4) When the value is extra (enhanced latency retrieval policy), the set initialization time will be delayed as much as possible. For example:

@Testpublic void testSetLazy() {    Category category = (Category) session.get(Category.class, 5);    System.out.println(category.getItems().size());}
Hibernate:     select        category0_.category_id as category1_1_0_,        category0_.category_name as category2_1_0_     from        hibernate.category category0_     where        category0_.category_id=?Hibernate:     select        count(item_id)     from        hibernate.categories_items     where        category_id =?2

When the size () method of the set is called, it is queried through count.

When the iterator () method of the set is called, the set is initialized.

4. fetch attribute (default: "select ")

(1) When the value is "select" or "subselect", the form of the query statement for the initialization set is determined.

(2) If the value is "join", the time when the set is initialized is determined. The "lazy" attribute is ignored.

(3) test

<1> set the value to "select"

@Testpublic void testSetLazy() {    List<Category> categories = session.createQuery("from Category").list();    for(Category category : categories) {        System.out.println(category.getItems().size());    }}
Hibernate:     select        customer0_.customer_id as customer1_2_,        customer0_.customer_name as customer2_2_     from        hibernate.customer customer0_Hibernate:     select        orders0_.customer_id as customer3_2_1_,        orders0_.order_id as order1_4_1_,        orders0_.order_id as order1_4_0_,        orders0_.order_name as order2_4_0_,        orders0_.customer_id as customer3_4_0_     from        hibernate.order orders0_     where        orders0_.customer_id=?2Hibernate:     select        orders0_.customer_id as customer3_2_1_,        orders0_.order_id as order1_4_1_,        orders0_.order_id as order1_4_0_,        orders0_.order_name as order2_4_0_,        orders0_.customer_id as customer3_4_0_     from        hibernate.order orders0_     where        orders0_.customer_id=?2Hibernate:     select        orders0_.customer_id as customer3_2_1_,        orders0_.order_id as order1_4_1_,        orders0_.order_id as order1_4_0_,        orders0_.order_name as order2_4_0_,        orders0_.customer_id as customer3_4_0_     from        hibernate.order orders0_     where        orders0_.customer_id=?2Hibernate:     select        orders0_.customer_id as customer3_2_1_,        orders0_.order_id as order1_4_1_,        orders0_.order_id as order1_4_0_,        orders0_.order_name as order2_4_0_,        orders0_.customer_id as customer3_4_0_     from        hibernate.order orders0_     where        orders0_.customer_id=?2

<2> If the value is "subselect", the batch-size attribute is ignored.

Hibernate:     select        category0_.category_id as category1_1_,        category0_.category_name as category2_1_     from        hibernate.category category0_Hibernate:     select        items0_.category_id as category1_1_1_,        items0_.item_id as item2_0_1_,        item1_.item_id as item1_3_0_,        item1_.item_name as item2_3_0_     from        hibernate.categories_items items0_     inner join        hibernate.item item1_             on items0_.item_id=item1_.item_id     where        items0_.category_id in (            ?, ?        )22
Hibernate:     select        customer0_.customer_id as customer1_2_,        customer0_.customer_name as customer2_2_     from        hibernate.customer customer0_Hibernate:     select        orders0_.customer_id as customer3_2_1_,        orders0_.order_id as order1_4_1_,        orders0_.order_id as order1_4_0_,        orders0_.order_name as order2_4_0_,        orders0_.customer_id as customer3_4_0_     from        hibernate.order orders0_     where        orders0_.customer_id in (            select                customer0_.customer_id             from                hibernate.customer customer0_        )2222

Through subquery and in.

<3> value: "join"

  • Uses the urgent left outer link (use the left outer link for query, and initialize the set attribute) Policy to initialize all associated objects.
  • The lazy property will be ignored.
  • The value is ignored in the list () method of Query, and the lazy attribute is not ignored.
  • HQL ignores the value of join.
@Testpublic void testFetch() {    session.get(Customer.class, 5);}
Hibernate:     select        customer0_.customer_id as customer1_2_1_,        customer0_.customer_name as customer2_2_1_,        orders1_.customer_id as customer3_2_3_,        orders1_.order_id as order1_4_3_,        orders1_.order_id as order1_4_0_,        orders1_.order_name as order2_4_0_,        orders1_.customer_id as customer3_4_0_     from        hibernate.customer customer0_     left outer join        hibernate.order orders1_             on customer0_.customer_id=orders1_.customer_id     where        customer0_.customer_id=?

5. Set the batch-size attribute to the number of batch search sets.

(1) by default

@Testpublic void testSetLazy() {    List<Category> categories = session.createQuery("from Category").list();    for(Category category : categories) {        System.out.println(category.getItems().size());    }}
Hibernate:     select        category0_.category_id as category1_1_,        category0_.category_name as category2_1_     from        hibernate.category category0_Hibernate:     select        items0_.category_id as category1_1_1_,        items0_.item_id as item2_0_1_,        item1_.item_id as item1_3_0_,        item1_.item_name as item2_3_0_     from        hibernate.categories_items items0_     inner join        hibernate.item item1_             on items0_.item_id=item1_.item_id     where        items0_.category_id=?2Hibernate:     select        items0_.category_id as category1_1_1_,        items0_.item_id as item2_0_1_,        item1_.item_id as item1_3_0_,        item1_.item_name as item2_3_0_     from        hibernate.categories_items items0_     inner join        hibernate.item item1_             on items0_.item_id=item1_.item_id     where        items0_.category_id=?2

(2) Set batch-size = "2"

@Testpublic void testSetLazy() {    List<Category> categories = session.createQuery("from Category").list();    for(Category category : categories) {        System.out.println(category.getItems().size());    }}
Hibernate:     select        category0_.category_id as category1_1_,        category0_.category_name as category2_1_     from        hibernate.category category0_Hibernate:     select        items0_.category_id as category1_1_1_,        items0_.item_id as item2_0_1_,        item1_.item_id as item1_3_0_,        item1_.item_name as item2_3_0_     from        hibernate.categories_items items0_     inner join        hibernate.item item1_             on items0_.item_id=item1_.item_id     where        items0_.category_id in (            ?, ?        )22

5. lazy and fetch attributes of the <role-to-one> element

1. lazy attributes

(1) proxy

Uses a latency retrieval policy.

(2) false

Use an immediate search policy.

2. fetch attributes

(1) select (default)

(2) join

Initialize objects at one end of 1 associated with one end of n by pressing the left outer link.

Similarly, HQL queries ignore the case where the fetch value is join. In this case, if you want to initialize proxy objects at one end of 1 in batches, you can add the batch-size attribute to the class node at one end.

Vi. Summary

This article introduces Hibernate's retrieval policies, including tired-level retrieval policies and associated-level retrieval policies. This section briefly introduces the lazy and fetch attributes of the <role-to-one> element.

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.