Hibernate lazy loading FOR Class

lazy Policy can be used in  :
 * <class> The label  : Can the value  true/false
 * <property> The label, you can value  true/false, This feature requires class enhancement  
 * <set>/<list> And so the collection, you can evaluate to  true/false/extra
 * <one-to-one>/<many-to-one> And so on, can value labels  false/proxy/no-proxy
 
  Important concepts  :
 1.lazy Concept, refers to the time issued  sql
 2.lazy Policies that are only opened during the session are valid  
 
  Note  :
 Hibernate At the class level of the lazy loading policy  : You can control when the loading of these ordinary object properties  
 
  Test units  :
  Please run the first program InitData, each unit test  
 


We are here for the time being only the circumstances test lazy for class

POJO:

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;
        }
        
        
}


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>      


Generated data class:

package com.bjsxt.hibernate;
import org.hibernate.Session;

import com.bjsxt.hibernate.Group;
import com.bjsxt.hibernate.HibernateUtils;


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  ");
                        session.save(group);
                        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;

/**
 *  Run the unit tests of prerequisites  :
 *  Set  <class> The label's lazy = "true", or keep the default  ( That is, do not configure lazy property  )
 * 
 * @author Administrator
 *
 */
public class ClassLazyTest extends TestCase {

        public void testLoadGroup1() {
                Session session = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        Group group = (Group)session.load(Group.class, 1);
                        
                        // Never send query  sql
                        System.out.println("group" + group.getId());
                        
                        // Issue the sql query, add data to the  Group
                        System.out.println("group name=" + group.getName());
                        
                        // Never send query  sql
                        System.out.println("group name=" + group.getName());
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        HibernateUtils.closeSession(session);
                }       
        }

        public void testLoadGroup2() {
                Session session = null;
                Group group = null;
                try {
                        session = HibernateUtils.getSession();
                        
                        // Never send query  sql
                        group = (Group)session.load(Group.class, 1);
                        
                }catch(Exception e) {
                        e.printStackTrace();
                }finally {
                        HibernateUtils.closeSession(session);
                }
                // Not the normal output, throw a   LazyInitializationException, Because the session has been closed  
                // Note  :Hibernate Support Lazy policy  , Only valid session open           
                System.out.println("group name=" + group.getName());
                
        }
        
        
}
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Hibernate lazy loading FOR Class

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

  • 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