澳门至尊网站-首页

您的位置:澳门至尊网站 > 技术教程 > hibernate延迟加载org,Hibernate延迟加载

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

2019-10-16 15:29

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",就足以了

本文由澳门至尊网站发布于技术教程,转载请注明出处:hibernate延迟加载org,Hibernate延迟加载

关键词:

  • 上一篇:没有了
  • 下一篇:没有了