您的位置:程序门 -> java -> j2ee / ejb / jms



如何利用opensessioninviewfilter等来实现lazy="true"呢?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何利用opensessioninviewfilter等来实现lazy="true"呢?[已结贴,结贴人:lyb_abiandbel]
发表于:2007-05-23 10:53:54 楼主
customer-order
one-to-many

我在做many-to-one的时候,遇到了延迟加载的问题,
我自己查了资料,把class,set,many-to-one标签全部加上lazy= "false "后,
可以成功实现了.但这时没有实现延迟加载,

我想使用lazy= "true ",hibernate   3.2.2默认使用的是lazy= "true "吧?
这样可以提高性能,我又查到了资料,说只要在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>
就可以了,但是我加了以后还是不可以.

请问怎么样才能实现lazy= "true "不报错呢?
发表于:2007-05-23 10:56:451楼 得分:30
你的session怎么获得的?
要sessionfactory.getcurrentsession();
不要sessionfactory.opensession();
发表于:2007-05-23 10:58:062楼 得分:10
楼上的解释下这有什么区别?谢谢!
发表于:2007-05-23 11:01:133楼 得分:0
我是使用spring   +   hibernate+struts

我就想知道,web.xml怎么写,才能实现lazy= "true "?

我看资料上还说,要用ontextloaderservlet,这样是不是原来的web.xml里面的org.apache.struts.action.actionservlet就不需要用了呢?

applicationcontext.xml,struts-config.xml都不变吗?
发表于:2007-05-23 11:01:334楼 得分:30
默认就已经是lazy加载啊,从打印的sql语句应该可以看出在你没有getorders的时候应该没有访问到order表啊
发表于:2007-05-23 11:05:215楼 得分:0
是的,默认lazy= "true "

customer   customer=customerservice.getcustomer(new   long(1));
set   orders=customer.getorders();   //这句是延迟加载

我原来没有用opensessioninviewfilter,所以会抱错,
我想添上opensessioninviewfilter实现延迟加载,不知道要在web.xml里面怎么写.
发表于:2007-05-23 11:09:366楼 得分:0
我原来的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,但是不知道怎么写
发表于:2007-05-23 13:21:207楼 得分:30
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本身不太好的地方,非常郁闷
发表于:2007-05-23 15:53:138楼 得分:0
谢谢楼上.

原来我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> -->
发表于:2007-05-24 10:03:499楼 得分:0
实现延迟加载还有其他的解决方案吗?

我查了资料,看到还有另外一种方法:用hibernateinterceptor,
但是我没有实验成功,

有谁能提供一个hibernateinterceptor实现延迟加载的例子吗?


快速检索

最新资讯
热门点击