Hibernate's Crawl Strategy

Source: Internet
Author: User

Retrieve Now: issue an SQL statement to query (get ()) immediately when executing a line of code
Deferred retrieval: when executing a line of code, SQL statements are not issued immediately for QUERYING. SQL statements are sent when the object is actually used (load ())

Class-level Retrieval and Association-level retrieval
Class-level retrieval:Configure lazy on the
Relevance-level search:<set>/<many-to-one> lazy above

A party that is associated with a party from one side (<set>)
Fetch: controlling the type of SQL statement
Join: an SQL query association object that sends an urgent left outer join. fetch= "join" then lazy is ignored.
Select: default value, which sends multiple SQL query Association Objects.
Subselect: sends subqueries to query the associated Object. (need to use query interface Test)

Lazy: controls whether the retrieval of the associated object takes a Delay.
True: default value, use deferred retrieval when querying associated objects
False: deferred retrieval is not used when querying the associated Object.
Extra: and Laziness.

  If fetch is a join, the lazy property ignores

On one side of a party (<many-to-one>)
Fetch: control SQL statement send format
Join: sends an urgent LEFT OUTER JOIN Query Association Object. fetch= "join", The Lay property is Ignored.
Select: sends multiple SQL to retrieve the associated Object.
Lazy: whether to use delay when correlating object retrieval
False: No delay
Proxy: Use the Agent. when retrieving the order amount, whether to retrieve the customer immediately is determined by the <class> lazy attribute in the mapping file of the customer Object.
No-proxy: do not use proxies (not researched)

Instance:

 /*   * Distinguish immediate retrieval and deferred retrieval   */ //  retrieve  Customer now Customer = (customer) session.get (customer. Class , 1 //  deferred retrieval:     The Persistence class fails if set to final deferred retrieval.  //  configuring lazy= "false" on the <class> tab in Customer.hbm.xml does not support deferred retrieval, it is retrieved immediately.  Customer customer = (customer) session.load (customer. Class , 1 
/*  *   */  public void  == session.begintransaction ();
= (customer) Session.get (customer. Class, 1); // send a query to the Client's sql. System.out.println (customer.getorders (). size ()); // and send a SQL to query the Customer's associated order   Tx.commit (); Session.close ();
}  
@Test/** <set> configuration fetch= "join" lazy will be ignored!!! * * Directly send urgent LEFT outer connection query two Tables.*/ public voidDemo3 () {session session=hibernateutils.opensession (); Transaction TX=session.begintransaction (); //send an urgent left outer connection directly    /** Select Customer0_.cid as cid0_1_, customer0_.cname as cname0_1_, orders1_.cno as cno0_3_, Orders1_.oid as oid3_, orders1_.oid as oid1_0_, orders1_.addr as addr1_0_, orders1_.cno as Cno1_0 _ from customer customer0_ left outer join orders orders1_ on custom         Er0_.cid=orders1_.cno where customer0_.cid=? */Customer Customer= (customer) Session.get (customer.class, 1);     System.out.println (customer.getorders (). Size ());    Tx.commit (); Session.close ();}
@Test/** Configuration on <set> set * * fetch= "select" lazy= "true" * * Lazy:true-use Deferred retrieval * * Send multiple sql, Query Association Object*/     public voidDemo4 () {session session=hibernateutils.opensession (); Transaction TX=session.begintransaction (); //send a query that queries only the Customer's SQLCustomer customer = (customer) Session.get (customer).class, 1); //when I use an order, I send a query to the SQL of the Customer's order.System.out.println (customer.getorders (). size ());        Tx.commit ();    Session.close (); }
@Test     /*      * configuration on <set> set     *     * fetch= "select" lazy= "false"     *     lazy:false: The retrieval of associated objects does not use delay      */     public void demo5 () {        = hibernateutils.opensession ();         = session.begintransaction ();              // send more than one SQL to query the associated object.        Customer customer = (customer) Session.get (customer).  Class, 1);        System.out.println (customer.getorders (). size ());        Tx.commit ();        Session.close ();    }
@Test/** Configuration on <set> set * * fetch= "select" lazy= "extra" * Lazy:extra extremely lazy. number of orders to order*/     public voidDemo6 () {session session=hibernateutils.opensession (); Transaction TX=session.begintransaction (); Customer Customer= (customer) Session.get (customer.class, 1); //Hibernate sends select Count (*) from orders where CNO =?;System.out.println (customer.getorders (). size ()); //SQL to send query orders         for(order order:customer.getOrders ()) {System.out.println (order);        } tx.commit ();    Session.close (); }
@Test/** configured on the <set> set * * fetch= "subselect" lazy= "true" * * Use the query interface when using Subselect to Test.     * * Query a customer for more than one customer.     * If there are multiple customers: * * SELECT * from orders where CNO in (all-in-one);     * If there is only one customer: * * select * FROM orders where CNO = 1; */     public voidDemo7 () {session session=hibernateutils.opensession (); Transaction TX=session.begintransaction (); List<Customer> list = Session.createquery ("from Customer"). List ();  for (Customer customer:list) {System.out.println (customer.getorders (). size ());        } tx.commit ();    Session.close (); }
 /*   * Not configured on <many-to-one> tab: * *     Send more than one SQL to Query.  */ public   void   Demo8 () {session session  = Hibernateut        Ils.opensession ();        Transaction TX  = session.begintransaction ();         //  Order order = (order) Session.get (order., 1 //  When the customer object of the order is used, a SQL query is sent to the customer that is associated with the order          System.out.println (order.getcustomer (). getcname ());        Tx.commit ();    Session.close (); }
 /*   * Configure on <many-to-one> tab: * * F Etch= "join" lazy= "ignored" * Send urgent LEFT outer connection  */ public  void   demo9 () {session session 
    = hibernateutils.opensession ();        Transaction TX  = session.begintransaction ();         //  Send an urgent left outer join. query the associated object.  Order order = (order) Session.get (order., 1
 /*   * Configure on <many-to-one> tab: * * F Etch= "select" lazy= "false" * * Send multiple SQL  */ public  void   demo10 () {Session SE        Ssion  = hibernateutils.opensession ();        Transaction TX  = session.begintransaction ();         //  Order order = (order) Session.get (order., 1
 /*   * Bulk CRAWL * configured on client side * <set& Gt; Configure batch-size= "3"  */ public  Span style= "color: #0000ff" >void   demo12 () {session session  = hibernateutils.opensession ();        Transaction TX  = session.begintransaction ();        List  <Customer> list = session.createquery ("from Customer"  for   (Customer customer:list) {for   (Order order:customer.getOrders ()) {            System.out.println (order.getaddr ());        }} Tx.commit ();    Session.close (); }
/*      * Bulk Crawl     *     customers can be crawled by order:     *         need to configure Batch-size         on the customer side <class> label * *  public void demo13 () {        = hibernateutils.opensession ();         = session.begintransaction ();        List<Order> list = session.createquery ("from Order"). list ();          for (Order order:list) {            System.out.println (order.getcustomer (). getcname ());        }        Tx.commit ();        Session.close ();    }

Hibernate's Crawl 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.