Hibernate lazy loading FOR Class

2010-05-09  来源:本站原创  分类:Java  人气:185 

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

        }

}
相关文章
  • hibernate lazy loading principles reproduced great ~ ~ 2008-11-12

    hibernate lazy loading of the principle of When using the load method of query session records in the database, we return a proxy object, rather than the needs of that object; for example, there is a Student table in the database, we have cn.binyulan

  • Hibernate Lazy loading not fully realized due to OO 2010-06-02

    Transfer from: http://www.sulong.info/archives/335 Object and relational database fundamental difference exists between, ORM (object / relational mapping) tried to object mapping to relational database table structure, which simplifies storage object

  • Discussion hibernate lazy loading 2010-10-01

    Hibernate lazy initialization error is the use of development projects in the most common mistakes. If the configuration of a class or collection of search strategy of delay, it must be when the agent or agent class instance is a collection of persis

  • json-lib hibernate lazy loading problem 2010-10-07

    json-lib hibernate lazy loading problem ------ Question: Using json-lib to hibernate lazy loading of objects into a json when session closed packet error ------ Address: Can use json-lib's JsonConfig filter out unwanted properties, using the apply me

  • hibernate lazy loading issue a temporary solution 2011-05-06

    hibernate lazy loading problems often plague us today, hibernate itself found to provide some solutions Hibernate.initialize (company.getUsers ()); This collection is loaded users But does not support recursive loading it is not guaranteed that the e

  • Hibernate lazy loading mechanism 2009-09-18

    Lazy loading: Lazy loading mechanism is in order to avoid unnecessary performance overhead and put forward the so-called lazy loading is that when data in the real needs when we come to the real implementation of the data load operation. Provided in

  • hibernate lazy loading failed 2010-02-03

    Concerned about the javaeye has been more than a year, but had no opportunity to write a log. Today, they wanted big cow jokes are not afraid of you wrote a log. Want to beginners strtus, hibernate, spring who have some help. The lazy loading, I no l

  • BlazeDS & Hibernate lazy loading in n-tier arhitecture 2010-04-07

    Original link: http://forum.springsource.org/showthread.php?t=71622 Hi, We have faced an issue on lazy loading Domain Objects with Hibernate in n-tier architecture with Spring BlazeDS. Our architecture is just a basic Spring architecture model: - Fle

  • Hibernate lazy loading FOR Class 2010-05-09

    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/fa

  • Hibernate lazy loading FOR Connection 2010-05-10

    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

  • Hibernate lazy loading single-ended correlation FOR 2010-05-10

    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

  • Hibernate lazy loading 2010-05-10

    What is hibernate lazy load? The so-called lazy loading (lazy) is to delay loading, lazy loading. When to use lazy loading it? Can only be answered when to use lazy loading with lazy loading. Why should lazy load it? When we want to access the data t

  • Hibernate lazy loading in the load method and the principle of 2010-08-29

    When using the session method in load record in the database query, we return a proxy object, not really need that object; for example, there is a Student table in the database, we have cn.binyulan.doman.Student areas object, if the query Id is "2006

  • Detailed hibernate lazy loading problem 2010-10-03

    Lazy loading: Lazy loading mechanism is to avoid unnecessary performance overhead of their argument, the so-called lazy loading is when the really need the data before it will actually perform the data loading operation. Provided in the Hibernate obj

  • Hibernate lazy loading simple solution 2010-10-03

    Recently doing a project with a piece of data persistence layer hibernate. After the previous study used no how, has been in use IBATIS. When the integration of spring security encountered "no session, or session was closed", so look for good so

  • spring + hibernate lazy loading errors integrated solution 2010-11-25

    org.springframework.dao.InvalidDataAccessApiUsageException error org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER / MANUAL): Turn your Session into FlushMode.COMMIT / AUTO

  • The hibernate lazy loading (Lazy Loading) 2011-01-07

    Avoid some cases, association brings unnecessary overhead, Hibernate introduced the concept of lazy loading; Hibernate3, can be recorded for the delay: Entity object Collection (Collection) Lazy property Through the Load method, the target entity obj

  • Spring + Hibernate lazy loading test base class 2009-08-13

    package cn.com.oneslife; import junit.framework.TestCase; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.AfterClass; import org.springframework.context.ApplicationContext; import org.springframework.context.suppor

  • hibernate lazy loading Principle and Realization 2010-03-29

    Can read: http://superleo.javaeye.com/blog/243322

  • hibernate in the pessimistic and optimistic locking lock and lazy loading 2009-04-02

    hibernate frequently used when many individuals of the same data at the same time when changes will happen dirty data, resulting in data inconsistency, the solution can be adopted by the pessimistic and optimistic locking to achieve lock. Pessimistic