shh整合出现no session or session was closed

[b]项目使用hibernate3.1 + spring2.0 + struts1.3
[/b]set集合延迟加载出现的问题如下
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jessie.common.model.JyAttribute.codes, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)

[b]解决:[/b]
1.getHibernateTemplate().initialize 准备set集合
return getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery(hql.toString());
List<JyAttribute> attrs = q.list();
for(JyAttribute a : attrs){
getHibernateTemplate().initialize(a.getCodes());
}
return attrs;
}});
2.fetch捉取
//需在映射文件中添加 fetch="join"
return (List)getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery(
"select distinct p from JyAttribute p left join fetch p.codes" + hql.toString()
);
return q.list();
}
});
3.使用OpenSessionInViewFilter管理session,在web.xml中添加
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
[color=red][b]注:[/b][/color]openSessionViewInFilter未配置成功,还不知何缘由
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值