项目中使用了Spring的OpenSessionInViewFilter,使得Hibernate的延时加载很方便,同时我也把DWR影射进取了,代码如下。
<filter></filter>
<filter-name></filter-name>hibernateFilter
<filter-class></filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
<init-param></init-param>
<param-name></param-name>
singleSession
<param-value></param-value>
true
<filter-mapping></filter-mapping>
<filter-name></filter-name>securityFilter
<url-pattern></url-pattern>/*
这样DWR就不会抱延时加载的错误了,但是它在把javaBean转换成JS的时候,调用了所有的Bean中的Get方法,这样Hibernate就把所有和这关联的属性都加载进来了,这样效率自然变低了,尤其是在加载Set的时候,一对多的时候,很慢,经常出错。,。。。。。。。。。
以上是我猜想的错误,就这样我想到修改源代码,个人适用的DWR版本是
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_07-87 ("Apple Computer, Inc.")
把其中的org.directwebremoting.convert.CollectionConverter文件下这个方法修改为
这样可以不加载Set了,其他属性还是加载,因为平时需要用到其他属性。
完成。。。。OK
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException
{
// Where we collect out converted children
Map ovs = new TreeMap();
// We need to do this before collecing the children to save recurrsion
ObjectOutboundVariable ov = new ObjectOutboundVariable(outctx);
outctx.put(data, ov);
try
{
Map properties = getPropertyMapFromObject(data, true, false);
for (Iterator it = properties.entrySet().iterator(); it.hasNext();)
{
Map.Entry entry = (Map.Entry) it.next();
String name = (String) entry.getKey();
Property property = (Property) entry.getValue();
Object value = property.getValue(data);
//如果为Set跳过------------------------------------------------
if(value!=null&&value.getClass()==org.hibernate.collection.PersistentSet.class){
continue;
}OutboundVariable nested = getConverterManager().convertOutbound(value, outctx);
ovs.put(name, nested);
}
}
catch (MarshallException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new MarshallException(data.getClass(), ex);
}
ov.init(ovs, getJavascript());
return ov;
}
分享到:
- 2007-06-28 16:57
- 浏览 2513
- 评论(1)
- 论坛回复 / 浏览 (1 / 2988)
- 查看更多
相关推荐
1. **引入dwr.jar** - 将DWR的jar包添加到项目的lib目录下,这是使用DWR的基础,因为该jar包包含了DWR的所有核心类和函数。 2. **创建dwr.xml** - 在WEB-INF目录下创建dwr.xml配置文件。这个文件用于配置DWR的行为...
### Struts1.x、Spring2.x、Hibernate3.x 和 DWR2.x 整合知识点解析 #### 一、Struts1.x与Spring2.x的整合 **1.1 在web.xml中进行Struts和Spring的配置** 为了实现Struts1.x与Spring2.x的无缝集成,首先需要在...
本文档旨在提供一套详细的配置指南,用于整合Struts1.x、Spring2.x、Hibernate3.x以及DWR2.x等技术栈。通过本指南,开发者可以更好地理解如何将这些框架有效结合,以构建稳定、高效的Java Web应用。 #### 文档约定 ...
为了让Hibernate在请求处理过程中自动打开和关闭Session,可以使用`OpenSessionInViewFilter`过滤器。 ```xml <!--Hibernate Session自动开启--> <filter-name>lazy <filter-class>org.springframework.orm....
`web.xml`是Servlet容器的配置文件,除了Struts2和Spring的配置外,还包含了`OpenSessionInViewFilter`,这个过滤器使得在HTTP请求的整个生命周期中,Hibernate的Session都保持打开状态,从而方便进行事务管理和数据...