注册 登录
编程论坛 J2EE论坛

求助,关闭错误!

y0311 发布于 2007-05-12 10:43, 601 次点击
代码如下:

String sql = "from StudentInfo where studentId=1";
List list = template.find(sql);
if (list != null) {
for(int i=0;i<list.size();i++)
{

StudentInfo stus = (StudentInfo)list.get(i);
System.out.println(stus.getUsers().getUserName() + " "
+ stus.getRemark() + " "
+ stus.getPersonInfo().getNameSpell());
}
}

错误信息如下:
2007-05-12 10:11:59,268 [http8080-Processor25] ERROR org.hibernate.LazyInitializationException - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at com.yyaccp.edu.model.strive.Users$$EnhancerByCGLIB$$2c251792.getUserName(<generated>)
at com.yyaccp.edu.dataaccess.strive.StudentInfoImpl.findAllStudent(StudentInfoImpl.java:58)
at com.yyaccp.edu.business.strive.StudentInfoBizImpl.findAllStudent(StudentInfoBizImpl.java:21)

template已经配置好了,我在另一个方法里面使用了
58行就是上面我用红色标记出来的那行
6 回复
#2
y03112007-05-12 10:46
template是spring里面集合的hibernate,与hibernate里面的session差不多
呵呵,先谢谢各位了
#3
y03112007-05-12 11:21
补充说明以下
抛出异常的地方的代码如下:
public final void initialize()
throws HibernateException
{
if(!initialized)
{
if(session == null)
throw new LazyInitializationException("could not initialize proxy - no Session");
if(!session.isOpen())
throw new LazyInitializationException("could not initialize proxy - the owning Session was closed");



#4
y03112007-05-12 14:55
我另外写了个保存的方法,也是使用的template ,成功了,但这里却老是报错,各位大虾帮帮忙
#5
支离破碎2007-05-14 14:05

好像是延迟加载的问题,你的student是否和别的表有级联的关系,看下你这个student.xml的配置,是否有一个lazy的属性,确认一下这他属性的状态,把这个属性改成true试下?

#6
黄袖标2007-05-14 15:56
ls正解,就是延迟加载,你要的东西hib还没抓出来.
#7
黄袖标2007-05-14 15:57
可以放在一个session的transation里试试
不行就去改加载策略吧
1