No wasted 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
Class-Level retrieval policies
1. 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
2. Class-level retrieval policies can be set by the <class> element's Lazy property
3. If the program loads an object that is intended to access its properties, it can take immediate retrieval.
4. If the program loads a persisted object in order to obtain its reference only, a deferred retrieval can be used. Note that lazy loading exceptions appear!
5. Regardless of 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
6. If the lazy property of the <class> element is true or takes the default value, the Session's load () method does not execute the SELECT statement of the Query data table and returns only the instance of the proxy class object, which has the following characteristics:
(1) dynamically generated by Hibernate using the CGLIB tool at run time
(2) Hibernate only initializes its OID property when creating an instance of the proxy class
(3) Hibernate initializes the proxy class instance when the application accesses the non-OID attribute of the proxy class instance for the first time
One-to-many and many-to-many retrieval 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 attributes
(1) 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
(2) Fetch: When the value is "select" or "Subselect", determines the form of the query statement that initializes the orders; If the value is "join", the time when the Orders collection is initialized is determined
(3) If fetch is set to "join", the lazy property will be ignored
(4) The Batch-size attribute of the <set> element: 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.
<set> lazy and fetch attributes for elements
Delayed retrieval and enhanced deferred retrieval
Hibernate initializes the collection proxy class instance in the following cases when the deferred retrieval (lazy property value is true) collection property
(1) The first time the application accesses the collection properties: Iterator (), size (), IsEmpty (), contains () and other methods
(2) explicit initialization via Hibernate.initialize () static method
Enhanced deferred retrieval (the Lazy 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:
(1) When a program accesses the iterator () method of the Orders property for the first time, it causes the initialization of the Orders collection proxy class instance
(2) When the program first accesses the size (), contains (), and IsEmpty () methods of the Order property, Hibernate does not initialize an instance of the Orders collection class, only queries the necessary information through a specific SELECT statement and does not retrieve all Order Object
Full batch initialization of the Orders collection with the SELECT statement of the tape query (Fetch attribute "Subselect")
Fetch attribute of the <set> element: Determines the form of the query statement that initializes the orders when the value is "select" or "Subselect"; If the value is "join", it determines when the orders collection is initialized. The default value is select
When the fetch attribute is "subselect":
(1) Assuming that the Session cache has n orders collection proxy class instances not initialized, Hibernate is able to batch initialize N orders collection proxy class instances by using the SELECT statement of the tape query
(2) Batch-size property will be ignored
(3) The SELECT statement in the subquery is a SELECT statement that queries the CUSTOMERS table OID
Urgent left OUTER JOIN search (Fetch attribute value set to "join")
Fetch attribute of the <set> element: Determines the form of the query statement that initializes the orders when the value is "select" or "Subselect"; If the value is "join", it determines when the orders collection is initialized. The default value is select
When the fetch attribute is "join":
(1) When retrieving a Customer object, the policy to retrieve all associated Order objects is taken with an urgent left outer join (the object associated with retrieving the specified object is loaded through a left outer join)
(2) The lazy attribute is ignored
(3) the list () method of Query ignores the urgent left outer connection retrieval policy configured in the mapping file, and still uses the deferred load policy
Multiple-to-one and single-on-a-link retrieval strategies
Like <set>, the <many-to-one> element also has a lazy property and a fetch attribute.
If the fetch attribute is set to join, then the Lazy property is ignored
The advantage of an urgent left outer join retrieval strategy is that fewer SELECT statements are used than the immediate retrieval policy.
Agentless latency retrieval requires the enhancement of the byte code of the persisted class to achieve
The list method of Query ignores the urgent left outer connection retrieval policy of the mapping file configuration, and uses the deferred retrieval strategy
If you use lazy-load or immediate-load retrieval policies at the association level, you can set the size of the bulk retrieval to help improve the running performance of deferred or immediate retrieval.
Hibernate allows you to overwrite the retrieval policy set in the mapping file in your application.
Summarize
Class-level and association-level optional retrieval policies and default retrieval policies
The operating mechanism of 3 kinds of retrieval strategies
Several properties in the mapping file that are used to set the retrieval policy
Three search strategies for comparing Hibernate
Hibernate's retrieval strategy