hibernate--Retrieval Strategy

Source: Internet
Author: User

One, hibernate's retrieval strategy is essentially to optimize hibernate performance.

Hibernate retrieval strategies include class-level retrieval strategies, and association-level retrieval strategies (<set> elements)

Three, class-level retrieval strategies

1. Immediate retrieval, delayed retrieval

2. Control by the lazy attribute of the <class> node. The default is true, which is deferred retrieval.

3. The load () method for session only takes effect.

By default, the object obtained through the load () method is a proxy object, and Hibernate only initializes the OID when it creates the proxy object.

Hibernate initializes the proxy class instance the first time it accesses a non-OID property.

4. Testing

(1) <class> The Lazy property is True

  @Test  public  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) <class> The Lazy property is False

@Test  Public 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. Retrieval strategy of association level

1. Refers to the many-to-one and many-to-many relationships that are configured with the <set> element.

2. Mainly refers to the <set> elements of three properties: lazy, Fetch, batch-size.

3. Lazy Property (default is True, deferred retrieval policy is used)

(1) The timing of the initialization of the set is determined.

(2) When the value is True

    • Hibernate initializes the collection proxy class instance in the following cases
      • When accessing methods such as collection properties, iterator (), size (), IsEmpty (), contains ()
      • Initialization via Hibernate.initialize () static method display

(3) When the value is False

(4) When the value is extra (enhanced deferred retrieval policy), the time to initialize the collection is delayed as much as possible. Such as:

@Test  Public void Testsetlazy () {    = (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         =?2

When the size () method of the collection is called, it is queried by count ().

The collection is initialized when the iterator () method of the collection is called.

4. Fetch attribute (default = "select")

(1) When a value of "select" or "Subselect" is chosen, the form of the SET query statement is initialized.

(2) The value is "join", then the time to initialize the collection is determined. The "lazy" attribute is ignored.

(3) test

<1> value is "select"

@Test  Public 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_, or        ders0_.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_, O     rders0_.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_, O     rders0_.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_, O     rders0_.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> value is "Subselect", the Batch-size property is ignored.

 hibernate:select category0_.category_id as Category1_1_, category0_. Category_name as category2_1_ from Hibernate.category category0_hibernate:select items0_.category_i  D 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 joins Hibernate.item item1_ on items0_.item_id  =item1_.item_id where items0_.category_id in ( /span>?,? )  
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 the way of subqueries, through in.

<3> value is "join"

    • All associated objects are initialized with a policy of pressing LEFT outer links (querying with left outer links and initializing collection properties).
    • The lazy property will be ignored
    • The list () method of Query ignores this value and does not ignore the lazy property.
    • HQL will ignore the value of "join" value
@Test  Public 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 joins        hibernate.order orders1_ on             customer0_. customer_id=orders1_.customer_id     where        customer0_.customer_id=?

The 5.batch-size property sets the number of collections to be retrieved in bulk.

(1) By default

  @Test  public  void   Testsetlazy () {List  <Category> categories = Session.createquery (" From    Category "". List ();  for   (Category category:categories) {Sys    Tem.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_, ite ms0_.item_id as item2_0_1_, item1_.item_id as item1_3_0_, item1_.item_name as item2_3_0_ from Hib Ernate.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_ inne R join Hibernate.item item1_ on items0_.item_id=item1_.item_id where items0_.category_id=?2

(2) Set batch-size= "2"

@Test  Public 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

V. Lazy and FETCH attributes for <many-to-one> elements

1.lazy Properties

(1) Proxy

Adopt a deferred retrieval strategy.

(2) False

Adopt an immediate retrieval strategy.

2.fetch Properties

(1) Select (default)

(2) Join

Represents an object that initializes one end of an association of N with an urgent left outer link in the way of 1.

Similarly, the HQL query ignores fetch values as joins, in which case the Batch-size attribute can be added to the class node at one end of the 1 if you want to bulk initialize a proxy object at one end of 1.

Vi. Summary

This paper introduces the retrieval strategy of Hibernate, including the retrieval strategy of exhaustion level, and the Retrieval strategy of association level. And a brief description of the lazy and fetch attributes of the <many-to-one> element.

hibernate--Retrieval Strategy

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.