菜单

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

2019年11月4日 - 4166am金沙下载

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

延迟加载在Hibernate中是默认延迟加载;

SQL Error: 17008, SQLState: null
每次程序运行一段时间以后,就报此错
08-08-17 09:55:29 WARN JDBCExceptionReporter:71 – SQL Error: 17008,
SQLState: null
08-08-17 09:55:29 ERROR JDBCExceptionReporter:72 – 关闭的连接
08-08-17 09:55:29 WARN JDBCExceptionReporter:71 – SQL Error: 17008,
SQLState: null
08-08-17 09:55:29 ERROR JDBCExceptionReporter:72 – 关闭的连接
org.hibernate.exception.GenericJDBCException: could not execute query
Caused by: java.sql.SQLException: 关闭的连接

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

测试代码一:

我的的struts2 +hibernate3

}

HibernateTest.java

dao是
public Userinfo checkUserinfo(String name, String pwd) {

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

代码:

 Session session = HibernateSessionFactory.getSession(); Transaction ta = session.beginTransaction(); Userinfo userinfo =  session.createCriteria(Userinfo.class) .add(Expression.eq("userinfoName", name)).add( Expression.eq("userinfoPassword", pwd)).uniqueResult(); ta.commit(); HibernateSessionFactory.closeSession(); return userinfo;}

 

4166m金沙,/**
 *
 */
package com.b510.examples;

[b]问题补充:[/b]
package com.shop.hibernate.util;

运行结果:

import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

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属性,

[b]问题补充:[/b]

Caused by: java.sql.SQLException: 关闭的连接
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)
at
oracle.jdbc.driver.OracleConnection.privatePrepareStatement(OracleConnection.java:895)
at
oracle.jdbc.driver.OracleConnection.prepareStatement(OracleConnection.java:802)
at
org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:396)
at
org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:334)
at
org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:88)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1162)
at org.hibernate.loader.Loader.doQuery(Loader.java:390)
at
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)
… 122 more

虽然有:  Set<Product>
products=category.getProducts();
代码,即:不处理集合对象。但是我们只要的是:

  System.out.println(“id:”+category.getId()+” 
,name:”+category.getName()+”,
description:”+category.getDescription());
输出的是id,name和description属性值,其他的我们不管,所以Hibernate用了lazy
loading(延迟加载),带来的好处就是我们不关心的

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

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图