今天在重构公司一个老项目的时候,遇到了这个异常,记录下:
很明显异常的意思是,引用了一个没保存的对象,而我的操作是点修改,即需要根据ID查询实体,那查询为什么会存在保存的操作呢?经仔细排查后得出结论
实体类如下:
原因如上代码注释部分。
解决方法:
1、在sping文件中中切面配置中修改所有的get方法为 read-only="true"
2、在web.xml配置文件中加入一个过滤器,拦截所有的.do或者.action请求,具体看你需要而定
具体原理,请参考下面一文:[url]http://blog.csdn.net/wgj85/article/details/2840747[/url]
很明显异常的意思是,引用了一个没保存的对象,而我的操作是点修改,即需要根据ID查询实体,那查询为什么会存在保存的操作呢?经仔细排查后得出结论
实体类如下:
package com.eclink.model;
public class ModuleInfo implements java.io.Serializable {
private Integer id;
private ModuleInfo parent;
public ModuleInfo getParent() {
// 这一步是防止,在编辑的时候,在页面上直接写moduleInfo.parent.id这种为空异常的情况,而正因为这一步,所以在查询实体的时候,就会出现如题的异常,引用了未保存的对象。
if(parent == null)
parent = new ModuleInfo();
return parent;
}
public void setParent(ModuleInfo parent) {
this.parent = parent;
}
}
原因如上代码注释部分。
解决方法:
1、在sping文件中中切面配置中修改所有的get方法为 read-only="true"
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
2、在web.xml配置文件中加入一个过滤器,拦截所有的.do或者.action请求,具体看你需要而定
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter>
具体原理,请参考下面一文:[url]http://blog.csdn.net/wgj85/article/details/2840747[/url]