金沙国际唯一官网-奥门金沙手机娱乐网址

热门关键词: 金沙国际唯一官网,奥门金沙手机娱乐网址
金沙国际唯一官网 > 奥门金沙手机娱乐网址 > 集合就不延迟加载了,但是我们只要的是

原标题:集合就不延迟加载了,但是我们只要的是

浏览次数:200 时间:2019-10-14

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();关闭了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",就可以了

本文由金沙国际唯一官网发布于奥门金沙手机娱乐网址,转载请注明出处:集合就不延迟加载了,但是我们只要的是

关键词:

上一篇:没有了

下一篇:没有了