1. Search method
1 Search Now: Queries immediately, querying all data immediately when executing a query statement.
2 Deferred retrieval: Deferred query, after executing a query statement, when needed in the query. (Lazy Loading)
2. Check the policy
1 class-Level retrieval: Whether the property of the current class gets a delay.
2 Retrieval of association levels: whether the current class associates another class requires a delay.
3. Class-Level Search
1 Get: Retrieve now. The Get method executes, querying the data for all fields immediately.
2 Load: Deferred retrieval. By default, after the Load method executes, if only the value of the OID is not queried, the query is queried if you want to use a different property value. Customer.hbm.xml <class lazy= "true | False ">
The lazy default value of True, which indicates deferred retrieval, if set to False indicates immediate retrieval, is equivalent to the Get method.
Package Com.alice.hibernate02.lazy;import Org.hibernate.session;import Org.hibernate.transaction;import Org.junit.test;import Com.alice.hibernate02.util.hibernateutil;import Com.alice.hibernate02.vo.customer;public Class Demo1 {//classes lazy load//Load method//class lazy attribute//Default value: True when load is obtained, the proxy object is returned and the database is not queried. Query @testpublic void test01 () {Session Session = Hibernateutil.opensession (); Transaction tran = Session.begintransaction (); Customer cus = (customer) session.load (Customer.class, 6); System.out.println (Cus.getname ()); Tran.commit (); Session.close ();} Class-Level lazy load//load method//Class Lazy Property//Lazy:false The Load method executes to send the SQL statement. Consistent with the Get method. @Testpublic void test02 () {Session Session = Hibernateutil.opensession (); Transaction tran = Session.begintransaction (); Customer cus = (customer) session.load (Customer.class, 6); System.out.println (Cus.getname ()); Tran.commit (); Session.close ();}}
4. Relevance Level Search
When querying data about the associated relationship, whether the data on one side needs to be queried immediately by the other party.
Default: The data associated with me is not loaded when it is used.
Collection (one-to-many):
Set
Lazy: Whether lazy loading is used for set data
true: (default) for collection use only load
False: The collection will be loaded immediately
Extra: Extremely lazy, if the collection is used, it calls the size method to query the number, and Hibernate sends the Count statement, querying only the quantity. The data within the collection is not loaded.
Fetch: Determines the kind of SQL statement used by the Load collection
Select: (Default) General select query
Join: Table-Link statement query collection data
Subselect: Use subqueries to load order data for multiple customer at once
Fetch |
Lazy |
Conclusion |
Select |
True |
The default value, which is loaded when the collection is used, and the normal SELECT statement |
Select |
False |
Load collection data immediately using the SELECT statement |
Select |
Extra |
The normal SELECT statement is loaded when the collection is used, and the Count statement query length is sent if only the length of the collection is obtained |
Join |
True |
When you query a collection using a table link query, the collection data is loaded immediately |
Join |
False |
When you query a collection using a table link query, the collection data is loaded immediately |
Jojn |
Extra |
When you query a collection using a table link query, the collection data is loaded immediately |
Subselect |
True |
is loaded when the collection is used, the subquery statement |
Subselect |
False |
Use a subquery to load the customer's order data immediately when querying the user |
Subselect |
Extra |
When the collection is used, the subquery statement is loaded, and if only the length of the collection is obtained, the Count statement query length is sent. |
Code test:
Package Com.alice.hibernate02.lazy;import Java.util.list;import Org.hibernate.session;import Org.hibernate.transaction;import Org.junit.test;import Com.alice.hibernate02.util.hibernateutil;import Com.alice.hibernate02.vo.customer;import Com.alice.hibernate02.vo.order;public class Demo02 {//association level lazy Load//default: Data associated with me is loaded when it is used. @Testpublic void test01 () {Session session = Hibernateutil.opensession (); Transaction tran = Session.begintransaction (); Customer cus = (customer) Session.get (Customer.class, 6); for (Order o:cus.getorders ()) {System.out.println (O.getname () );} Tran.commit (); Session.close ();} Association level lazy Load//lazy:false@testpublic void test02 () {Session session = Hibernateutil.opensession (); Transaction tran = Session.begintransaction (); Customer cus = (customer) Session.get (Customer.class, 6); for (Order o:cus.getorders ()) {System.out.println (O.getname () );} Tran.commit (); Session.close ();} Association level lazy Load//lazy:false//fetch:subselect@testpublic void test03 () {Session session = Hibernateutil.opensession (); Transaction tran = Session.begintransaction (); Customer cus = (customer) Session.get (Customer.class, 6); for (Order o:cus.getorders ()) {System.out.println (O.getname () );} Tran.commit (); Session.close ();} Association level lazy Load//lazy:true/false//fetch:subselect@testpublic void test04 () {Session session = Hibernateutil.opensession (); Transaction tran = Session.begintransaction (); list<customer> list = Session.createquery ("from Customer"). List (), for (Customer c:list) {System.out.println ( C.getname () + "Number of orders" +c.getorders (). Size ()); Tran.commit (); Session.close ();} Association level lazy Load//lazy:extra//fetch:select@testpublic void test05 () {Session session = Hibernateutil.opensession (); Transaction tran = Session.begintransaction (); Customer cus = (customer) Session.get (Customer.class, 6);//query customer for order quantity System.out.println (Cus.getorders (). Size () ); Really use the data in the order for (Order O:cus.getorders ()) {System.out.println (O.getname ());} Tran.commit (); Session.close ();} Association level lazy Load//lazy:extra//fetch:subselect@testpublic void test06 () {Session session = Hibernateutil.opensession (); Transaction tran = Session.begintransaction (); @SuppressWarnings ("Unchecked") list<customer> List = Session.createquery ("from Customer"). List (), for (Customer c:list) {System.out.println (C.getname () + "Order Number:" + C.getorders (). Size ());} for (Customer c:list) {for (Order o:c.getorders ()) {System.out.println (C.getname () + "Order name:" +o.getname ()); }}tran.commit (); Session.close ();}}
Many-to-one:
<many-to-one>
Lazy
False: When loading an order, passengers are immediately added
Proxy: Look at the class loading strategy of the client object to decide
No-proxy: Do not do research.
Fetch
Select: (default) load with normal select
Join : Loading data using table links
Fetch |
Lazy |
Results |
Select |
False |
The customer data is loaded immediately when the order is loaded. The normal SELECT statement loads the customer. |
Select |
Proxy |
The class loading policy is: Lazy=false Ibid. Lazy=true the customer data is not loaded first when the order is loaded. Load when using customer data |
Join |
False |
Use the table link to inquire about orders and corresponding customer information. Invalid Lazy Property |
Join |
Proxy |
Use the table link to inquire about orders and corresponding customer information. Invalid Lazy Property |
PackageCom.alice.hibernate02.lazy;Importorg.hibernate.Session;Importorg.hibernate.Transaction;Importorg.junit.Test;ImportCom.alice.hibernate02.util.HibernateUtil;ImportCom.alice.hibernate02.vo.Order;//multiple-to-one retrieval strategy Public classDemo03 {//multiple-to-one retrieval strategy@Test//Fetch:select//Lazy:false Public voidtest01 () {Session session=hibernateutil.opensession (); Transaction Tran=session.begintransaction (); Order o= (order) session.get (order).class, 8); System.out.println (O.getcustomer (). GetName ()); Tran.commit (); Session.close (); } //Fetch:select//Lazy:proxy//Customer Lazy:false@Test Public voidtest02 () {Session session=hibernateutil.opensession (); Transaction Tran=session.begintransaction (); Order o= (order) session.get (order).class, 8); System.out.println (O.getcustomer (). GetName ()); Tran.commit (); Session.close (); } //Fetch:select//Lazy:proxy//Customer lazy:true@Test Public voidtest03 () {Session session=hibernateutil.opensession (); Transaction Tran=session.begintransaction (); Order o= (order) session.get (order).class, 8); System.out.println (O.getcustomer (). GetName ()); Tran.commit (); Session.close (); } //Fetch:join//Lazy:proxy|false@Test Public voidtest04 () {Session session=hibernateutil.opensession (); Transaction Tran=session.begintransaction (); Order o= (order) session.get (order).class, 8); System.out.println (O.getcustomer (). GetName ()); Tran.commit (); Session.close (); }}
5. Bulk Load
Set
Batch-size: Determines the collection data for loading several objects at a time. The in condition loads orders for multiple users.
PackageCom.alice.hibernate02.lazy;Importjava.util.List;Importorg.hibernate.Session;Importorg.hibernate.Transaction;Importorg.junit.Test;ImportCom.alice.hibernate02.util.HibernateUtil;ImportCom.alice.hibernate02.vo.Customer;ImportCom.alice.hibernate02.vo.Order;//Batch Policy Public classDemo04 {@Test//Find All Customers//traverse the customer and print the order information under the customer Public voidTest () {Session session=hibernateutil.opensession (); Transaction Tran=session.begintransaction (); List<Customer> list = Session.createquery ("From Customer"). List (); for(Customer c:list) {System.out.println (C.getorders (). Size ()); } tran.commit (); Session.close (); }}
Customer Get (int id)
Return session.load (Customer.class,id);
- Layz=false
- The service layer gets the attributes to be used on the page, and the service layer ensures that the data is
Hibernate Learning (6)--Loading strategy (optimization)