Hibernate lazy -- delayed Loading

Source: Internet
Author: User

The Hibernate lazy policy can be used in:
* <Class> label. Optional values: True/false. The default value is true for hibernate3 or a later version.
* <Property> label. Optional values: True/false. Class enhancement tools are required.
* <Set> <list> label. The value can be true/false/extra.
* <One-to-one> <allow-to-one> single-ended Association. Optional values: false/Proxy/no-proxy

Lazy concept: it is created only when this object is actually used. For hibernate, SQL is issued only when this object is actually used.

Hibernate supports the lazy policy only when the session is enabled.

1 <class> label:

Group. HBM. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping package = "com. Model">
   <Class name = "group" table = "group5" lazy = "true"> // lazy. The default value is true. Do not write.
       <ID name = "ID" column = "ID" type = "Java. Lang. Integer">
           <Generator class = "native"/>       
       </ID>
       <Property name = "name" column = "name" length = "50" type = "Java. Lang. String"/>
   </Class>
</Hibernate-mapping>

Test cases:

Public class lazytest extends testcase

{

Public void testload1 (){
  Session session = NULL;
  Transaction TA = NULL;
  Try {
   Session = hibernateutil. getsession ();
   Ta = session. begintransaction ();

   // The SQL statement has not been sent yet, and lazy has a delay effect. If lazy is set to false, the SQL statement is sent.

   Group g2 = (Group) Session. Load (group. Class, 1 );
   Group G3 = (Group) Session. Get (group. Class, 1 ); // Lazy is not supported
   System. Out. println ("group. ID =" + g2.getid ());   // No SQL has been issued,
   System. Out. println ("group. Name =" + g2.getname (); // issue an SQL
   Ta. Commit ();
  } Catch (exception e ){
   E. printstacktrace ();
   If (Ta! = NULL ){
    Ta. rollback ();
   }
  } Finally {
   // Close the session and change the user to a detached offline object
   Hibernateutil. closesession (session );
  }
  // System. Out. println ("group. Name =" + g2.getname ());
  // Hibernate supports the lazy policy only when the session is enabled, so this exception occurs.
}

}

<Class> the lazy feature on tags only applies to common attributes.

<Class> tag lazy does not affect the lazy feature of single-ended associations.

2. On the <set> <list> label, you can set the value to true/false/extra. The default value is true.

Classes. HBM. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping package = "com. ZD. Model">
   <Class name = "classes" table = "classes">
       <ID name = "ID" column = "ID" type = "Java. Lang. Integer">
           <Generator class = "native"/>
       </ID>
       <Property name = "name" column = "name" type = "Java. Lang. String"/>
       
<Set name = "Students" lazy = "true">
// Lazy is not supported, because the default value is true.
           <Key column = "class_id"/>
           <One-to-learn class = "com. ZD. model. Student"/>
       </Set>
   </Class>

</Hibernate-mapping>

Test cases:

Public void testload1 (){
  Session session = NULL;
  Transaction TA = NULL;
  Try {
   Session = hibernateutil. getsession ();
   Ta = session. begintransaction ();
   Classes C = (classes) Session. Load (classes. Class, new INTEGER (2 ));// No SQL
   System. Out. println ("class. Name =" + C. getname ());                  // Issue an SQL statement without querying the set
   Set stuset = C. getstudents ();                       

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.