object references an unsaved transient instance - save the transient instance be

5 篇文章 0 订阅
3 篇文章 0 订阅
今天在重构公司一个老项目的时候,遇到了这个异常,记录下:
很明显异常的意思是,引用了一个没保存的对象,而我的操作是点修改,即需要根据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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值