当前位置: 永利会员登录皇宫463 > 新闻资讯 > 正文

而get方法又是非延迟加载,但是我们只要的是

时间:2019-10-15 21:43来源:新闻资讯
  貌似景观下,Hibernate会暗中同意给大家设置延迟加载。lazy="true",这样会升级大家的种类天性,所以常常境况下,大家不会去 集合延迟加载最早化失败,无法起先化二个代理。便是集聚

 

貌似景观下,Hibernate会暗中同意给大家设置延迟加载。lazy="true" ,这样会升级大家的种类天性,所以常常境况下,大家不会去

集合延迟加载最早化失败,无法起先化二个代理。便是集聚在非一对一对象关系中,为了节省能源是默许延迟加载,而get方法又是非延迟加载,所以在奉行完一遍数据库查询后就实行session.close();关闭了session,而集合是延迟加载,在接纳集适合时宜再加载,此时session已经关闭,所以得不到代办。消除方法:能够在主表的hbm配置文件中,在<set>标签里安装lazy="false",集结就不延迟加载了,因而在实践get方法时,集结也获取到了,就不相会世延迟加载难题了。

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

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

那是大家不指望看见的结果。

  //dept.getEmp()获得子表的记录集结
  System.out.println(dept.getEmp());

<?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>

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

/**
 *
 * @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();  
 }
 
 
}

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)

HibernateTest.java

 

import java.util.Set;

运营结果:

    where
        products0_.category_id=?
ID:  1  name:java SE应用程序设计 price: 78.00
此地能够断定的告知大家,当大家要加载Set会集的时候,那时候才去加载,而地点的事例,表达的是大家不加载的时候

}

  System.out.println("id:"+category.getId()+"  ,name:"+category.getName()+", description:"+category.getDescription());
输出的是id,name和description属性值,别的的大家随意,所以Hibernate用了lazy loading(延迟加载),带来的益处就是我们不关心的

运行效果:

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

代码:

那边大家看看我们关怀的是id,name和description属性,

HibernateTest.java

import java.util.Set;

HIbernateTest.java

其次条select语句,对于大家的需假如没有必要的,他唯有贰个用场正是据有我们的程序试行时间。当然,

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

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_

Hibernate就延迟加载

import org.hibernate.Session;

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语句。不过大家会意识

/**
 *
 * @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();  
 }
 
 
}

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

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好啊

虽说有:  Set<Product> products=category.getProducts(); 代码,即:不处理会集对象。可是大家倘诺的是:

运转结果:

import java.util.Set;

代码:

/**
 *
 * @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();  
 }
 
 
}

代码:

import org.hibernate.Session;

测量试验代码二:

运行作效果果:

推迟加载在Hibernate中是默许延迟加载;

Category.hbm.xml

测量检验代码一:

代码:

import org.hibernate.Session;

测量检验代码:

收回延迟加载:

安装lazy="false",当然在特种的景况下,我们务须要打消延迟加载的时候,大家就把lazy="false",就可以了

编辑:新闻资讯 本文来源:而get方法又是非延迟加载,但是我们只要的是

关键词: