Hibernate lazy loading single-ended correlation FOR

sponsored links
Hibernate To load policy-Terminal associated lazy  : That is, in  <one-to-one>/<many-to-one> You can configure on the label  
 Lazy loading policy  .

 Can a value  :false/proxy/no-proxy
false: Cancel the lazy loading policy, that is, while loading the object  , Issuing the query statement, loading its associated object  
proxy: It's hibernate on Singel associated default lazy loading policy  , That is, only to its associated object method  
       It issues a query statement to query the object data, its associated object is a proxy class  
no-proxy: This kind of lazy load characteristics need to be enhanced, use class  no-proxy, Its associated object is not a proxy class  

 Note  : In the class label configuration  lazy The attribute does not affect to the associated object  !!!


To many to one as an example the following cases:

package com.bjsxt.hibernate;

public class Group {
        
        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;
        }
        
        
}


package com.bjsxt.hibernate;

import java.util.Date;

public class User {
        
        private int id;
        
        private String name;
        
        private Group group;

        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 Group getGroup() {
                return group;
        }

        public void setGroup(Group group) {
                this.group = group;
        }
        
}


Group.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.Group" table="t_group">
                <id name="id">
                        <generator/>
                </id>
                <property name="name"/>
        </class>
</hibernate-mapping>      


User.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.User" table="t_user" >
                <id name="id">
                        <generator/>
                </id>
                <property name="name"/>
                <!--
                <many-to-one name="group" column="relatedGroup" />
                 -->
                
                <many-to-one name="group" column="relatedGroup"/> 
        </class>
</hibernate-mapping>      


Generated data:

package com.bjsxt.hibernate;

import org.hibernate.Session;

public class InitData {

        public static void main(String[] args) {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        session.beginTransaction();
                        Group group = new Group();
                        group.setName("java Development Group  ");
                        
                        // The group is converted to a persistent object   
                        session.save(group);
                        
                        for (int i = 0; i < 10 ; i++) {
                                User user = new User();
                                user.setName("User_" + i);
                                
                                // Establish relationships between objects  
                                user.setGroup(group);
                                
                                // Because the group is a persistent object  , So you can successfully save  
                                session.save(user);
                        }
                        session.getTransaction().commit();
                }catch(Exception e) {
                        e.printStackTrace();
                        session.getTransaction().rollback();
                }finally {
                        HibernateUtils.closeSession(session);
                }
        }

}


Test Class:

package com.bjsxt.hibernate;

import org.hibernate.Session;

import junit.framework.TestCase;

/**
 *  All the property default lazy  
 * @author Administrator
 *
 */
public class SingleEndTest1 extends TestCase {

        public void testLoadUser() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        User user = (User)session.load(User.class, 2);
                        
                        // Will issue a query SQL, reprint data to  User
                        System.out.println("user name=" + user.getName());
                        
                        // Never send query  sql
                        Group group = user.getGroup();
                        
                        // Will issue a query SQL, reprint data to  Group
                        System.out.println("related group=" + group.getName());
                        
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        
                }
        }
}


The lazy <class> tag is set to false

package com.bjsxt.hibernate;

import org.hibernate.Session;

import junit.framework.TestCase;

/**
 *  Will  <class> The lazy in the label is set to  false
 * @author Administrator
 *
 */
public class SingleEndTest2 extends TestCase {

        public void testLoadUser() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Will issue a query SQL, reprint data to  User
                        User user = (User)session.load(User.class, 2);
                        
                        // Never send query  sql
                        System.out.println("user name=" + user.getName());
                        
                        // Never send query  sql
                        Group group = user.getGroup();
                        
                        // Will issue a query SQL, reprint data to  Group
                        System.out.println("related group=" + group.getName());
                        
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        
                }
        }
}


<class> Label lazy to keep the default settings, will <many-to-one> labels to add lazy = false

package com.bjsxt.hibernate;

import org.hibernate.Session;

import junit.framework.TestCase;

/**
 *  Keep  <class> Label the lazy default settings in the  
 *  Will  <many-to-one> Label added  lazy=false
 * @author Administrator
 *
 */
public class SingleEndTest3 extends TestCase {

        public void testLoadUser() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Does not issue a query  sql
                        User user = (User)session.load(User.class, 2);
                        
                        // Will issue a query SQL, here because  many-to-one Setting the lazy = false so there will be  group Also loads over here will issue two  SQL
                        //Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.relatedGroup as relatedG3_0_0_ from t_user user0_ where user0_.id=?
                        //Hibernate: select group0_.id as id1_0_, group0_.name as name1_0_ from t_group group0_ where group0_.id=?

                        System.out.println("user name=" + user.getName());
                        
                        // Never send query  sql
                        Group group = user.getGroup();
                        
                        // The previous single out data, is no longer issuing the query  SQL
                        System.out.println("related group=" + group.getName());
                        
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        
                }
        }
}
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Hibernate lazy loading single-ended correlation FOR

  • 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 ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

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