Hibernate lazy loading FOR Connection

Hibernate Collection property of lazy loading policy  :

 In the collection properties, you can configure the lazy loading policy  , A value of  :true/false/extra

true: The default value, which means that only the collection gets inside the element object  , Before issuing the query statement, loading its  
      The collection of data elements  
false: Cancel the lazy load characteristics, i.e. while loading the object  , The second query statement to load data to the associated collection  
extra: A clever lazy loading policy, which is called the collection  size/contains When methods such as  ,hibernate
       And not to load the entire collection of data, and is issued a clever  SQL Statements, in order to get the required values  , Only in  
       The real need to use the collection element object data when going to issue a query statement to load all the data of the object  


The following to-many map-based way. On the side of the relevant settings set to lazy to test.

As follows:

package com.bjsxt.hibernate;

import java.util.Set;

public class Classes {
        
        private int id;
        
        private String name;
        
        private Set students;
        
        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public Set getStudents() {
                return students;
        }

        public void setStudents(Set students) {
                this.students = students;
        }
        
        
}


package com.bjsxt.hibernate;

public class Student {
        
        private int id;
        
        private String name;

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }
}


Classes.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
        <class name="com.bjsxt.hibernate.Classes" table="t_classes">
                <id name="id">
                        <generator/>
                </id>
                <property name="name"/>
                <!-- 
                        <set name="students" cascade="all" order-by="id">
                 -->
                 <set name="students" lazy="extra">
                        <key column="classid"/>
                        <one-to-many/>
                </set>
        </class>
</hibernate-mapping>


Student.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
        <class name="com.bjsxt.hibernate.Student" table="t_student">
                <id name="id">
                        <generator/>
                </id>
                <property name="name"/>
        </class>
</hibernate-mapping>


Configure-to-many mapping above.

As the next set of three property values in LAZY true / false / extra can set the following, we were tested.

1, true: the default value, it means that only the call to get this set of elements inside the object, when it issued the query, load the collection of data elements

package com.bjsxt.hibernate;

import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;

import junit.framework.TestCase;

/**
 *  Keep the default value for the lazy  
 * @author Administrator
 *
 */
public class CollectionLazyTest1 extends TestCase {
        
        public void testLoadClasses1() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        Classes classes = (Classes)session.load(Classes.class, 1);
                        
                        // Sends a query to load data into sql,  classes
                        System.out.println(" Class  :[" + classes.getName() + "]   Have students  :");
                        
                        // Never send query  sql
                        Set students = classes.getStudents();
                        
                        // Sends a query to load data into sql,  Students
                        for (Iterator iter = students.iterator(); iter.hasNext();) {
                                Student student = (Student)iter.next();
                                System.out.println(student.getName());
                        }
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        HibernateUtils.closeSession(session);
                }       
        }
        
        public void testLoadClasses2() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        Classes classes = (Classes)session.load(Classes.class, 1);
                        
                        // Sends a query to load data into sql,  classes
                        System.out.println(" Class  :[" + classes.getName() + "]   The total number of students  :");

                        // Never send query  sql     
                        Set students = classes.getStudents();
                        
                        // Will issue a query SQL, load all of the data to the  Students
                        System.out.println(students.size());
                        
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        HibernateUtils.closeSession(session);
                }       
        }
        
}

The implementation of the above results of two methods:

testLoadClasses1 Results:

Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
 Class  :[ Class  ]   Have students  :
Hibernate: select students0_.classid as classid0_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_ from t_student students0_ where students0_.classid=?
 Students  _6
 Students  _9
 Students  _2
 Students  _0
 Students  _4
 Students  _1
 Students  _8
 Students  _7
 Students  _5
 Students  _3


testLoadClasses2 Results:

Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
 Class  :[ Class  ]   The total number of students  :
Hibernate: select students0_.classid as classid0_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_ from t_student students0_ where students0_.classid=?
10


2, false: Cancel lazy loading features, that is, the object is loaded, while the second query on the issue of load its associated data collection

package com.bjsxt.hibernate;

import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;

import junit.framework.TestCase;

/**
 *  Sets the collection of  lazy=false
 * @author Administrator
 *
 */
public class CollectionLazyTest2 extends TestCase {
        
        public void testLoadClasses1() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        Classes classes = (Classes)session.load(Classes.class, 1);
                        
                        // Will issue two query SQL, loading data to the  classes And Students in  
                        System.out.println(" Class  :[" + classes.getName() + "]   Have students  :");
                        
                        // Never send query  sql
                        Set students = classes.getStudents();
                        
                        // Never send query  sql
                        for (Iterator iter = students.iterator(); iter.hasNext();) {
                                Student student = (Student)iter.next();
                                System.out.println(student.getName());
                        }
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        HibernateUtils.closeSession(session);
                }       
        }
        
        public void testLoadClasses2() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        Classes classes = (Classes)session.load(Classes.class, 1);
                        
                        // Will issue two query SQL, loading data to the  classes And Students in  
                        System.out.println(" Class  :[" + classes.getName() + "]   The total number of students  :");

                        // Never send query  sql     
                        Set students = classes.getStudents();
                        
                        // Never send query  sql     
                        System.out.println(students.size());
                        
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        HibernateUtils.closeSession(session);
                }       
        }
        
}

When you call here in testLoadClasses1 method classes.getName () when the note, at this time before the issue of two SQL, as follows:

Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
Hibernate: select students0_.classid as classid0_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_ from t_student students0_ where students0_.classid=?
 Class  :[ Class  ]   Have students  :
 Students  _3
 Students  _9
 Students  _4
 Students  _6
 Students  _5
 Students  _1
 Students  _8
 Students  _0
 Students  _2
 Students  _7

The testLoadClasses2 results are as follows: Note that both SQL is issued when the call classes.getName, followed by print students.size not reissued SQL. This also shows that: the object is loaded, while the second query on the issue of load its associated data collection

Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
Hibernate: select students0_.classid as classid0_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_ from t_student students0_ where students0_.classid=?
 Class  :[ Class  ]   The total number of students  :
10


3, extra: a more clever lazy loading strategy that calls the collection of size / contains methods such as time, hibernate
And not going to load the entire collection of data, but issues a smart of the SQL statements in order to obtain the values you only need to use these collections Real element object data while before trying to send Chaxunyuju load all objects data


package com.bjsxt.hibernate;

import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;

import junit.framework.TestCase;

/**
 *  Sets the collection of  lazy=extra
 * @author Administrator
 *
 */
public class CollectionLazyTest3 extends TestCase {
        
        public void testLoadClasses1() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        Classes classes = (Classes)session.load(Classes.class, 1);
                        
                        // Sends a query to load data into sql,  classes
                        System.out.println(" Class  :[" + classes.getName() + "]   Have students  :");
                        
                        // Never send query  sql
                        Set students = classes.getStudents();
                        
                        // Sends a query to load data into sql,  students
                        for (Iterator iter = students.iterator(); iter.hasNext();) {
                                Student student = (Student)iter.next();
                                System.out.println(student.getName());
                        }
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        HibernateUtils.closeSession(session);
                }       
        }
        
        public void testLoadClasses2() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        Classes classes = (Classes)session.load(Classes.class, 1);
                        
                        // Sends a query to load data into sql,  classes
                        System.out.println(" Class  :[" + classes.getName() + "]   The total number of students  :");

                        // Never send query  sql     
                        Set students = classes.getStudents();
                        
                        // Will issue a more intelligent query SQL, such as  :
                        //select count(id) from t_student where classid =?
                        System.out.println(students.size());
                        
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        HibernateUtils.closeSession(session);
                }       
        }
        
        public void testLoadClasses3() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        Classes classes = (Classes)session.load(Classes.class, 1);
                        
                        // Sends a query to load data into sql,  classes
                        System.out.println(" Class  :[" + classes.getName() + "]   The total number of students  :");

                        // Never send query  sql     
                        Set students = classes.getStudents();

                        // Iteration will issue  SQL
                        for (Iterator iter = students.iterator(); iter.hasNext();) {
                                Student student = (Student)iter.next();
                                System.out.println(student.getName());
                        }
                        
                        // Because the earlier iterations out all of the data, so the following does not issue  sql, And testLoadClasses2 of different printing is the total number of iterations before  .
                        System.out.println(students.size());
                        
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        HibernateUtils.closeSession(session);
                }       
        }
        
}


testLoadClasses1 Results:

Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
 Class  :[ Class  ]   Have students  :
Hibernate: select students0_.classid as classid0_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_ from t_student students0_ where students0_.classid=?
 Students  _2
 Students  _8
 Students  _3
 Students  _5
 Students  _4
 Students  _9
 Students  _0
 Students  _7
 Students  _1
 Students  _6

testLoadClasses2 Results:

Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from t_classes classes0_ where classes0_.id=?
 Class  :[ Class  ]   The total number of students  :
Hibernate: select count(id) from t_student where classid =?
10


Can see that when LAZY = "extra" time would be more intelligent, that students.size () when the calls select count (id) from t_student where classid =?
Total demand.

Comparison of three kinds of value to the difference and connection between, obviously the third method is more optimized
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Hibernate lazy loading FOR Connection

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries