Hibernate lazy loading single-ended correlation FOR

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 {

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

相关文章

  • java.util.Date.java.sql.Date and String 2010-03-18

    package date_transfor; import java.text.SimpleDateFormat; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; / ** * * @ Author henry.xiao * * / public class Date_transfor ( private static Log log = LogFactory.getLog

  • Using the json-lib to java object into json string 2010-03-31

    http://shuaigg-babysky.javaeye.com/blog/354516 Learn to use ajax in j2ee after, and sometimes returned to the client from the server data object, piece together their own manual json string error-prone, through Google know json-lib the library to fac

  • Using json-lib to java object into json string 2010-03-31

    http://shuaigg-babysky.javaeye.com/blog/354516 Learned to use ajax j2ee after, sometimes the client from the server to return data from an object, their hand piece json string error-prone, through Google that the json-lib the library to facilitate th

  • Mutual Convert Java objects and JSON string 2010-05-12

    Mutual Convert Java objects and JSON string Uncategorized , by admin. Now the products are functional and the front jsp page is to compare large data exchange, so consider using JSON for data exchange. Original use of the JSON lib , the truth is real

  • java object into json string 2010-11-02

    Using json-lib to java object into json string Learned to use ajax j2ee after, and sometimes returned to the client from the server the data is the object, its manual and error-prone patchwork json string by Google knows the json-lib the library to f

  • java中Date与String的相互转化 2015-03-31

    1:大体思路 [html] view plaincopy 这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new Date("2008-04-14"); 方法2: SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟 String dstr="2008-4-24"; j

  • gson (JSON string into an equivalent Java object) 2010-03-19

    Most of the gson also requires the use of java class in the use of java annotation, do not fully support POJO. Gson This Java class library can be converted into Java objects JSON, the JSON string can be converted into an equivalent Java object. Gson

  • java read txt text string interception 2010-04-02

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class TextConvertor (

  • 04. [Java programming] array and string 2010-12-17

    public class ExtractSubstrings { public static void main(String[] args) { String text = "To be or not to be"; // String to be segmented int count = 0; // Number of substrings char separator = ' '; // Substring separator // Determine the number o

  • Bash string manipulation (control and Java) - 4 string output 2011-09-08

    Bash string manipulation (control and Java) - 4 string output In Java Output to the standard output device (console, screen) System.out.println (s); Output to the standard error device (the console screen) System.err.println (s); Output to a file Pri

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

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