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());

        }

}
分类:Java 时间:2010-05-09 人气:180
分享到:
blog comments powered by Disqus

相关文章

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp

  • DWR initial return to the list of string & object & SPRING integration and processing, with examples (CTO) 2010-09-30

    DWR (full name: Direct Web Remoting) is a model of an AJAX JavaEE framework, through the configuration and specify the callback JavaScript function and directly call the service back-end business method, WEB and get a remote call framework. Using DWR

  • 浅析C#中的Main(String[] args)参数输入问题 2014-07-20

    本篇文章主要是对C#中的Main(String[] args)参数输入问题进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 指定相关的测试代码 首先,写一个用于测试的关于Main(String[] args)参数输入有关的代码类,如下: using System; public class Hello { public static void Main(String[] args) { switch (args[0]) { case "老板": Console.Write

  • Why public static void main (String args []){} 2010-11-06

    main () is a Java program entry, program execution is the beginning of this entry. Class is a class member, main () method must be public members. So that it can be called in the execution environment. main () method does not produce the object can b

  • C Programming Language (Second Edition) 4-7 Write a function ungets (), press return to enter the entire string in 2010-07-20

    #include<stdio.h> #include<string.h> #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; int getch(void); void unGetch(int); /* Getch: get a ( possibly pushed back) character. */ int getch(void) { return (bufp > 0) ? buf[--bufp]: getchar()

  • 一个完整的LMOS驱动框架 2013-09-17

    /**************************************************************** 主桥驱动程序文件hostbrideg.c ***************************************************************** 彭东 @ 2013.05.21.17.10 ****************************************************************/ /*一个完整的LM

  • java data type conversion Integer String Long Float Double Date 2010-07-24

    1 How to convert integer string String int? A. There are two ways: 1). Int i = Integer.parseInt ([String]); or i = Integer.parseInt ([String], [int radix]); 2). Int i = Integer.valueOf (my_str). IntValue (); Note: The string converted to Double, Floa

  • JAVA中int.String的类型转换 2012-04-08

    int -> String int i=12345; String s=""; 第一种方法:s=i+""; 第二种方法:s=String.valueOf(i); 这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢? String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法:i=Integer.valueOf(s).intValue(); 这两

  • String与int之间的转换(java) 2012-08-18

    int -> String int i=12345; String s=""; 第一种方法:s=i+""; 第二种方法:s=String.valueOf(i); 这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢? String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法:i=Integer.valueOf(s).intValue(); 这两

  • Java, byte hexadecimal string with 16 interchangeable 2009-11-28

    Java, said the occupation of byte binary 8-bit, and we know that each of 16 hexadecimal characters need to use 4-bit binary bits to represent the (23 + 22 + 21 + 20 = 15), so we can convert each byte two corresponding 16 hexadecimal characters, that

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 1.092 (s). 12 q(s)