spring+hibernate 延迟加载出错could not initialize proxy-the owning Session was closed

WoodLikeWater 2010-04-13 09:37:20
javabean中hibernate配置文件

UserInfo类

<hibernate-mapping>
<class name="org.project.oa.entity.UserInfo" table="UserInfo" schema="dbo" catalog="oaDB">
..............
<many-to-one name="purviewInfo" class="org.project.oa.entity.PurviewInfo" fetch="select" >
<column name="purviewId" not-null="true" />
</many-to-one>
................
</class>
</hibernate-mapping>


PurviewInfo类

<hibernate-mapping>
<class name="org.project.oa.entity.PurviewInfo" table="PurviewInfo" schema="dbo" catalog="oaDB">
.........................
<set name="userInfos" inverse="true">
<key>
<column name="purviewId" not-null="true" />
</key>
<one-to-many class="org.project.oa.entity.UserInfo" />
</set>
..........................
</class>
</hibernate-mapping>

biz层代码

public List search(int userId){
UserInfo userInfo=userInfoDao.get(userId);//正常加载
PurviewInfo purviewInfo=userInfo.getPurviewInfo();//出现延迟加载异常
return functionDao.search(purviewInfo.getPurviewStr());
}

UserInfo配置文件中加入lazy="false" 成功运行。治标不治本的配置。
<many-to-one name="purviewInfo" class="org.project.oa.entity.PurviewInfo" fetch="select" lazy="false">

默认不写的情况下是lazy="true",在web-inf.xml中加入OpenSessionInViewFilter和ContextLoaderListener的相关配置文件也是照样出现异常

...全文
228 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
AreFlying 2011-03-02
  • 打赏
  • 举报
回复
请问楼主的问题是怎样解决的?
lk_well 2010-11-19
  • 打赏
  • 举报
回复
看我博客:
解决Hibernate延迟加载出现session过期的问题 http://hi.baidu.com/lk_well/blog/item/da4b613ca95f4d10bba16746.html
只在web.xml中配置一下就OK了.
WoodLikeWater 2010-04-14
  • 打赏
  • 举报
回复
砖家们快来帮帮忙。。
zidasine 2010-04-13
  • 打赏
  • 举报
回复
dao怎么写的
事务哪里控制的
wanli209 2010-04-13
  • 打赏
  • 举报
回复
Dao怎么写的没贴出来啊?是不是你手动获取session然后在关闭了
但是你却使用的Spring 或什么来管理的事务呢
这样你2次操作事务 关闭了session
不知道会不会这样 自己YY的
qq262067995 2010-04-13
  • 打赏
  • 举报
回复
EJB 有很好的解决方法

不过

用hql吧 !

加油!
wg243964183 2010-04-13
  • 打赏
  • 举报
回复
<class name="org.project.oa.entity.PurviewInfo" table="PurviewInfo" schema="dbo" catalog="oaDB">
里面有一个lazy=false或者用Hibernate.init...(Object)手动初始化
岁月之梦 2010-04-13
  • 打赏
  • 举报
回复
这个是很烦的 有时候配置了lazy还好 有时候 ....
daokun66 2010-04-13
  • 打赏
  • 举报
回复

public PurviewInfo getByUserId(int userId) {
return (PurviewInfo)super.getHibernateTemplate().execute(
new HibernateCallback(){
public Object doInHibernate(Session s){
String hql=" from UserInfo u join fetch u.purviewInfo where u.id="+userId;
return ((UserInfo)(s.createQuery(hql).uniqueResult())).getPurviewInfo();
}
}
);
}



lz试试,告诉你个思路,是先根据userId得到userInfo ,然后再userInfo里getPurviewInfo
WoodLikeWater 2010-04-13
  • 打赏
  • 举报
回复
手动没用过,应该是自动关闭的吧。。#83
WoodLikeWater 2010-04-13
  • 打赏
  • 举报
回复
spring 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- data source -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
<!-- dao层 -->
<bean id="daoBase" class="org.project.oa.dao.impl.DaoBase">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="functionDao" class="org.project.oa.dao.impl.FunctionDaoImpl" parent="daoBase"/>
<bean id="userInfoDao" class="org.project.oa.dao.impl.UserInfoDaoImpl" parent="daoBase"/>
<bean id="purviewInfoDao" class="org.project.oa.dao.impl.PurviewInfoDaoImpl" parent="daoBase"/>
<!-- biz层 -->
<bean id="functionBiz" class="org.project.oa.biz.impl.FunctionBizImpl">
<property name="functionDao" ref="functionDao"/>
<property name="purviewInfoDao" ref="purviewInfoDao"></property>
<property name="userInfoDao" ref="userInfoDao"></property>
</bean>
<!-- action层 -->
<bean name="/treeView" class="org.project.oa.web.action.TreeViewAction">
<property name="functionBiz" ref="functionBiz"></property>
</bean>
</beans>


biz层

public class FunctionBizImpl implements FunctionBiz{
private FunctionDao functionDao=null;
private PurviewInfoDao purviewInfoDao=null;
private UserInfoDao userInfoDao=null;
public List search(int userId){
UserInfo userInfo=userInfoDao.get(userId);//正常获取
PurviewInfo purviewInfo=userInfo.getPurviewInfo();//出现延迟加载异常
return functionDao.search(purviewInfo.getPurviewStr());
}
//getter&setter
...................

zidasine 2010-04-13
  • 打赏
  • 举报
回复
原因是你在dao中就把session关闭了(手动还是spring)
看下spring的配置
WoodLikeWater 2010-04-13
  • 打赏
  • 举报
回复
1楼你能看清楚再回答吗?感觉你说的对我一点帮助都没有。
3楼能能帮我写个hql语句吗?
http://topic.csdn.net/u/20100413/09/0d5eb316-8be4-461a-bf99-869a08ec9675.html?88981
daokun66 2010-04-13
  • 打赏
  • 举报
回复
这个的确不好办,hibernate的延迟加载很容易出错,但是它的关系属性还都默认是延迟加载的,我们在项目中一般会使用hql直接查询,要是结果要用到关系属性会用join fetch 显示填充。
NOKIA5320XM 2010-04-13
  • 打赏
  • 举报
回复
传说中的沙发
OpenSessionInViewFilter把事务交给SPRING应该是不会出错的。
长公子冰 2010-04-13
  • 打赏
  • 举报
回复
解决方法:
将UserInfo.hbm.xml映射文件中的 fetch="select"去掉 换成 lazy="false"
如果还没解决就配置一下Spring的OpenSessionInView监听器。不过这个一般都要配置的,否则很容易出现这种异常,这是Spring提供的解决方案。
WoodLikeWater 2010-04-13
  • 打赏
  • 举报
回复
UserInfoDaoImpl 类

public class UserInfoDaoImpl extends DaoBase implements UserInfoDao{
/**
* 通过id获取用户信息
* @param id
* @return
*/
public UserInfo get(int id) {
return (UserInfo)super.get(UserInfo.class, id);
}

}
DaoBase类

public class DaoBase extends HibernateDaoSupport {
public Object get(Class clz, int id) {
return super.getHibernateTemplate().get(clz, id);
}
}


67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧