你的session怎么获得的?
要sessionFactory.getCurrentSession();
不要sessionFactory.openSession();

解决方案 »

  1.   

    我是使用Spring + Hibernate+Struts我就想知道,web.xml怎么写,才能实现lazy="true"?我看资料上还说,要用ontextLoaderServlet,这样是不是原来的web.xml里面的org.apache.struts.action.ActionServlet就不需要用了呢?applicationContext.xml,struts-config.xml都不变吗?
      

  2.   

    默认就已经是lazy加载啊,从打印的SQL语句应该可以看出在你没有getOrders的时候应该没有访问到order表啊
      

  3.   

    是的,默认lazy="true"Customer customer=customerService.getCustomer(new Long(1));
    Set orders=customer.getOrders(); //这句是延迟加载我原来没有用OpenSessionInViewFilter,所以会抱错,
    我想添上OpenSessionInViewFilter实现延迟加载,不知道要在web.xml里面怎么写.
      

  4.   

    我原来的web.xml是这样的:<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
    </context-param>
    <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
    </welcome-file-list> <jsp-config>
    <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
    </taglib>
    <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
    </taglib>
    <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
    </taglib>
    </jsp-config>
    </web-app>要加上哪些内容才能不报这个错误:
    could not initialize proxy - the owning Session was closed
    我查到资料要用到OpenSessionInViewFilter,但是不知道怎么写
      

  5.   

    could not initialize proxy - the owning Session was closed这是因为另外的原因造成的
    你通过session.load/get取出了customer对象了,然后由于页面请求完成了
    你手工或者通过filter调用了session.close()然后在下一次请求或者调用了session.close之后,你再调用customer.getOrders这样由于customer所关联的session被关闭了,它无法得到orders了你可以在准备getOrders之前重新加载一次order,就是order = session.get(Order.class, order.getId());不过我也觉得这个是Hibernate本身不太好的地方,非常郁闷
      

  6.   

    谢谢楼上.原来我strtus-config.xml还是用ContextLoaderPlugIn加载applocationContext.xml文件,注释掉以后,就能被org.springframework.web.context.ContextLoaderListener加载了,资料上说的不是很清楚.<!-- <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
    </plug-in>-->
      

  7.   

    实现延迟加载还有其他的解决方案吗?我查了资料,看到还有另外一种方法:用HibernateInterceptor,
    但是我没有实验成功,有谁能提供一个HibernateInterceptor实现延迟加载的例子吗?