[Original]java Web Learning note 88:hibernate Learning path---Hibernate retrieval strategy (immediate retrieval, deferred retrieval, urgent left outer connection search)

Source: Internet
Author: User

The purpose of this blog: ① summary of their own learning process, equivalent to study notes ② to share their own experience to everyone, learn from each other, communication, not commercialcontent inevitably appear problems, welcome to correct, exchange, discussion, you can leave a message, can also be contacted by the following ways. I Internet technology enthusiasts, Internet technology enthusiastsWeibo: Ivan is in 0221qq:951226918

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------------------------

1. Overview

1) Problem Introduction:

① does not waste memory: When Hibernate loads the customer object from the database, if all the associated order objects are loaded at the same time, and the program actually only needs to access the customer object, the associated order objects waste a lot of memory.

② higher query efficiency: Send As few SQL statements as possible

    

  

2. Class-Level retrieval strategies

① class-level optional retrieval policies include immediate retrieval and deferred retrieval, default to deferred retrieval

> Retrieve now: Load the object specified by the retrieval method immediately

    > Deferred retrieval: Lazy loading retrieves the object specified by the method. When you use a specific property, the load

Class-level retrieval policies can be set by the <class> element's Lazy property

③ if the program loads an object to access its properties, it can take immediate retrieval.

④ if the program loads a persisted object for the purpose of obtaining its reference only, a deferred retrieval can be used. Note that lazy loading exceptions appear!

Whether the <class> element's Lazy property is true or false, the get () method of the Session and the list () method of the Query always use the immediate retrieval policy at the class level

If the lazy property of the <class> element is true or takes the default value , the load () method of the Session does not execute the SELECT statement of the Query data table and returns only instances of the proxy class object , the proxy class instance has the following characteristics:

> dynamically generated by Hibernate using the CGLIB tool at run time

> Hibernate When you create an instance of a proxy class, only its OID properties are initialized

> Hibernate initializes the proxy class instance when the application first accesses the non-OID property of the proxy class instance

3. One-to-many and many-to-many search strategies

① in the mapping file, the <set> element is used to configure a one-to-many association and many-to-many association relationships . <set> elements have lazy and fetch Properties

    > Lazy: The time when the Orders collection is initialized is primarily determined . Whether it is initialized when the Customer object is loaded, or when the program accesses the Orders collection; whether to use deferred retrieval

  

                                Delayed retrieval and enhanced deferred retrieval

1. In deferred retrieval (thevalue of the Lazy property is true
   
Iterator (), size (), IsEmpty (), contains () and other methods
hibernate.initialize () static method explicit initialization
2. Enhanced deferred retrieval (thelazy property is extra): Similar to lazy= "true". The main difference is that the enhanced deferred retrieval strategy can further delay the initialization time of the Orders collection agent instance of the Customer object:
The iterator () method of the Orders property causes initialization of the Orders collection proxy class instance
② when the program first accesses O
All Order objects are not retrieved

> Fetch: When a value of "select" or "subselect" is chosen, the form of the query statement that initializes the orders is determined, and if the value is "join", The O Rders the time when the collection was initialized

       

1. When the Fetch property is "Subselect",
Batch - The Size property is ignored >> The SELECT statement in the subquery is a SELECT statement that queries the CUSTOMERS table OID

2. When the fetch attribute is "join"
When the Customer object is retrieved, all associated Order objects are retrieved with an urgent left outer join (the object associated with retrieving the specified object is loaded through the left outer join) policy
The Lazy property is ignored
The list () method of Query ignores the urgent left-to-outer connection retrieval policy configured in the mapping file and still uses the deferred-loading strategy

> If fetch is set to "join", the lazy property will be ignored

> Batch-size Properties of the <set> element: sets the data for initializing the set collection at once . Used to set the number of bulk searches for a deferred retrieval policy or an immediate retrieval policy. Bulk retrieval can reduce the number of SELECT statements and improve the performance of deferred or immediate retrieval operations.

    

4. Multiple-to-one and single-to-a-link retrieval strategies

1) As with <set>, the <many-to-one> element also has a lazy property and a fetch attribute

  

2) If the Fetch attribute is set to join, then the Lazy property is ignored

3) The advantage of an urgent left outer connection retrieval strategy is that fewer SELECT statements are used than the immediate retrieval strategy.

4) agentless delay retrieval requires the enhancement of the persistence class's bytecode to achieve

5. Summary

1) class-level and association-level optional retrieval policy and default retrieval policy

 

2) operating mechanism of 3 kinds of retrieval strategies

 3) Compare Hibernate's three kinds of retrieval strategies

[Original]java Web Learning note 88:hibernate Learning path---Hibernate retrieval strategy (immediate retrieval, deferred retrieval, urgent left outer connection search)

Related Article

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.