hibernate延迟加载org,Hibernate延迟加载

作者: 编程技术  发布:2019-12-11

public static void main(String[] args) {
 
  DeptEntity dept = getDept("402882e762ae888d0162ae888e420000");

延迟加载在Hibernate中是暗许延迟加载;

  //dept.getEmp(卡塔尔(قطر‎获得子表的记录集结
  System.out.println(dept.getEmp());

测量试验代码风流潇洒:

}

HibernateTest.java

private static DeptEntity getDept(String did){
  Session session = sessionFactory.openSession();
  DeptEntity dept = (DeptEntity)session.get(DeptEntity.class, did);
  session.close();
  return dept;
}

代码:

 

/**
 *
 */
package com.b510.examples;

运维结果:

import java.util.Set;

Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.javakc.hibernate.onetomany.entity.DeptEntity.emp, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:326)
at java.lang.String.valueOf(String.java:2827)
at java.io.PrintStream.println(PrintStream.java:771)
at com.javakc.hibernate.onetomany.action.TestAction.main(TestAction.java:74)

import org.hibernate.Session;

 

/**
 *
 * @author XHW
 *
 * @date 2011-7-18
 *
 */
public class HibernateTest {
 public static void main(String[] args) {
  new HibernateTest().update();
 }
 public void update(){
  Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  Category category=(Category)session.get(Category.class, 1);
  System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());
  
  Set<Product> products=category.getProducts();
  
  session.getTransaction().commit();  
 }
 
 
}

聚焦延迟加载开首化战败,不能够先导化三个代理。就是集聚在非意气风发对意气风发对象关系中,为了省去能源是私下认可延迟加载,而get方法又是非延迟加载,所以在举行完一次数据库查询后就施行session.close(卡塔尔(英语:State of Qatar);关闭了session,而集结是延迟加载,在利用集适当时候再加载,那时session已经破产,所以得不到代办。消除方法:可以在主表的hbm配置文件中,在<set>标签里设置lazy="false",集结就不推迟加载了,因而在进行get方法时,集结也收获到了,就不会现身延迟加载问题了。

运转结果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
    select
        category0_.id as id1_0_,
        category0_.name as name1_0_,
        category0_.description as descript3_1_0_
    from
        users.category category0_
    where
        category0_.id=?
id:1  ,name:java, description:java好啊

此间我们看来大家关注的是id,name和description属性,

就算有:  Set<Product> products=category.getProducts(卡塔尔; 代码,即:不管理会集对象。可是大家要是的是:

  System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());
出口的是id,name和description属性值,其余的大家无论,所以Hibernate用了lazy loading(延迟加载卡塔尔国,带给的功利正是大家不关切的

数据,不用现在加载,当大家要用的时候,才去加载

测验代码二:

HibernateTest.java

代码:

/**
 *
 */
package com.b510.examples;

import java.util.Set;

import org.hibernate.Session;

/**
 *
 * @author XHW
 *
 * @date 2011-7-18
 *
 */
public class HibernateTest {
 public static void main(String[] args) {
  new HibernateTest().update();
 }
 public void update(){
  Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  Category category=(Category)session.get(Category.class, 1);
  System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());
  
  Set<Product> products=category.getProducts();
  for(Product product:products){
   System.out.println("ID:  "+product.getId()+"  name:"+product.getName()+" price: "+product.getPrice());
  }  
  session.getTransaction().commit();  
 }
 
 
}

运营效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
    select
        category0_.id as id1_0_,
        category0_.name as name1_0_,
        category0_.description as descript3_1_0_
    from
        users.category category0_
    where
        category0_.id=?
id:1  ,name:java, description:java好啊
Hibernate: 
    select
        products0_.category_id as category2_1_,
        products0_.id as id1_,
        products0_.id as id0_0_,
        products0_.category_id as category2_0_0_,
        products0_.name as name0_0_,
        products0_.price as price0_0_,
        products0_.descripton as descripton0_0_
    from
        users.product products0_

    where
        products0_.category_id=?
ID:  1  name:java SE应用程序设计 price: 78.00
这里能够显然的告诉我们,当大家要加载Set集结的时候,这个时候才去加载,而地方的例证,说明的是大家不加载的时候

Hibernate就滞缓加载

撤废延迟加载:

Category.hbm.xml

代码:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.b510.examples.Category" table="category" catalog="users">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="500" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="description" length="500" />
        </property>
        <set name="products" inverse="true" lazy="false">
            <key>
                <column name="category_id" />
            </key>
            <one-to-many class="com.b510.examples.Product" />
        </set>
    </class>
</hibernate-mapping>

测验代码:

HIbernateTest.java

代码:

/**
 *
 */
package com.b510.examples;

import java.util.Set;

import org.hibernate.Session;

/**
 *
 * @author XHW
 *
 * @date 2011-7-18
 *
 */
public class HibernateTest {
 public static void main(String[] args) {
  new HibernateTest().update();
 }
 public void update(){
  Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  Category category=(Category)session.get(Category.class, 1);
  System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());
  
  Set<Product> products=category.getProducts();
   
  session.getTransaction().commit();  
 }
 
 
}

运维效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
    select
        category0_.id as id1_0_,
        category0_.name as name1_0_,
        category0_.description as descript3_1_0_
    from
        users.category category0_
    where
        category0_.id=?
Hibernate:
    select
        products0_.category_id as category2_1_,
        products0_.id as id1_,
        products0_.id as id0_0_,
        products0_.category_id as category2_0_0_,
        products0_.name as name0_0_,
        products0_.price as price0_0_,
        products0_.descripton as descripton0_0_
    from
        users.product products0_
    where
        products0_.category_id=?
id:1  ,name:java, description:java好啊
和测验代码黄金时代的周转结果互相相比较,大家会意识,本次运转结果用了两条select语句。但是大家会发觉

其次条select语句,对于我们的供给是平昔不供给的,他唯有叁个用途就是据有我们的程序推行时间。当然,

那是大家不愿意寓指标结果。

日常情状下,Hibernate会私下认可给我们设置延迟加载。lazy="true" ,那样会升高大家的系统天性,所以日常情况下,大家不会去

设置lazy="false",当然在非常的图景下,我们一定要要裁撤延迟加载的时候,大家就把lazy="false",就足以了

本文由贝博体育app发布于编程技术,转载请注明出处:hibernate延迟加载org,Hibernate延迟加载

关键词: