Hibernate 异常: Illegal attempt to associate a collection with two open sessions
来源:互联网 发布:p2p软件 编辑:程序博客网 时间:2024/05/18 02:54
今天在做项目发现的问题:
在做任务定时发送的时候发现此异常,于是我百度啊,找谷老师啊。
1.如以下找到的资料之一:
错误信息:
org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions;
错误原因:
1. 一个Action在两个线程中同时运行,造成并发。
解决方法:
1. 阻止重复提交。可以利用Struts Token机制。
但是还是不能解决我的问题。
2.经常debug 测试 终于发现了一个问题:
原来属性重名了
我在Action中已经注入了实体类
private Mission mission;
在下面某个内部类方法中也重名的定义
Mission mission = misList.get(0);
并且他们都被调用了同一方法
missionBussiness.addData(mis);
处理结果:改掉其中的一个属性名,错误仍然存在。
3.找到资料 :
在pring配置文件中的action bean中添加属性 scope="prototype"
问题依然存在。
4.最后终于找到一个可以解决我问题的方案:
使用hibenate的merge方法代替save、update等方法。
merge和saveOrUpdate方法区别在于:merge方法是把我们提供的对象转变为托管状态的对象;而saveOrUpdate则是把我们提供的对象变成一个持久化对象;说的通俗一点就是:saveOrUpdate后的对象会纳入session的管理,对象的状态会跟数据库同步,再次查询该对象会直接从session中取,merge后的对 象不会纳入session的管理,再次查询该对象还是会从数据库中取。所以一般建议用merge
将MissionDaoImpl类中的 this.getHibernateTemplate().save(data); save方法改为了merge方法
将此方法:public boolean addData(Object data) throws Exception {
this.getHibernateTemplate().save(data);
return true;
}
改为:
public boolean addData(Object data) throws Exception {
this.getHibernateTemplate().merge(data);
return true;
}
将MissionDaoImpl类中的 this.getHibernateTemplate().update(data); update方法改为了merge方法
将此方法:public void update(Object data) throws Exception {
super.getHibernateTemplate().update(data);
}
改为:public void update(Object data) throws Exception {
super.getHibernateTemplate().merge(data);
}
在做任务定时发送的时候发现此异常,于是我百度啊,找谷老师啊。
1.如以下找到的资料之一:
错误信息:
org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions;
错误原因:
1. 一个Action在两个线程中同时运行,造成并发。
解决方法:
1. 阻止重复提交。可以利用Struts Token机制。
但是还是不能解决我的问题。
2.经常debug 测试 终于发现了一个问题:
原来属性重名了
我在Action中已经注入了实体类
private Mission mission;
在下面某个内部类方法中也重名的定义
Mission mission = misList.get(0);
并且他们都被调用了同一方法
missionBussiness.addData(mis);
处理结果:改掉其中的一个属性名,错误仍然存在。
3.找到资料 :
在pring配置文件中的action bean中添加属性 scope="prototype"
问题依然存在。
4.最后终于找到一个可以解决我问题的方案:
使用hibenate的merge方法代替save、update等方法。
merge和saveOrUpdate方法区别在于:merge方法是把我们提供的对象转变为托管状态的对象;而saveOrUpdate则是把我们提供的对象变成一个持久化对象;说的通俗一点就是:saveOrUpdate后的对象会纳入session的管理,对象的状态会跟数据库同步,再次查询该对象会直接从session中取,merge后的对 象不会纳入session的管理,再次查询该对象还是会从数据库中取。所以一般建议用merge
将MissionDaoImpl类中的 this.getHibernateTemplate().save(data); save方法改为了merge方法
将此方法:public boolean addData(Object data) throws Exception {
this.getHibernateTemplate().save(data);
return true;
}
改为:
public boolean addData(Object data) throws Exception {
this.getHibernateTemplate().merge(data);
return true;
}
将MissionDaoImpl类中的 this.getHibernateTemplate().update(data); update方法改为了merge方法
将此方法:public void update(Object data) throws Exception {
super.getHibernateTemplate().update(data);
}
改为:public void update(Object data) throws Exception {
super.getHibernateTemplate().merge(data);
}
0 0
- Hibernate 异常: Illegal attempt to associate a collection with two open sessions
- Hibernate 异常:Illegal attempt to associate a collection with two open sessions
- Hibernate 异常: Illegal attempt to associate a collection with two open sessions
- Illegal attempt to associate a collection with two open sessions
- Illegal attempt to associate a collection with two open sessions;
- Illegal attempt to associate a collection with two open sessions
- Illegal attempt to associate a collection with two open sessions.
- OpenSessionInViewFilter的两个异常:Illegal attempt to associate a collection with two open sessions
- Hibernate 错误:Illegal attempt to associate a collection with two open sessions
- Hibernate Merge (Illegal attempt to associate a collection with two open sessions)
- SSH架构中Illegal attempt to associate a collection with two open sessions 问题
- 解决HibernateException: Illegal attempt to associate a collection with two open sessions
- 错误:Illegal attempt to associate a collection with two open sessions
- 解决HibernateException: Illegal attempt to associate a collection with two open sessions
- HibernateException: Illegal attempt to associate a collection with two open sessions
- SSH架构中Illegal attempt to associate a collection with two open sessions 问题
- Illegal attempt to associate a collection with two open session
- net.sf.hibernate.HibernateException: Illegal attempt to associate a collection with two open session
- 你应当如何学习C++(以及编程)(rev#1)
- HDU 1002 之常见WA(C++描述)
- ALSA声卡驱动中的DAPM详解之一:kcontrol
- jQuery中attr和prop方法的区别
- Part3 使用JavaScript显示地图
- Hibernate 异常: Illegal attempt to associate a collection with two open sessions
- hdu 1019
- libgdx 进度条
- ALSA声卡驱动中的DAPM详解之三:如何定义各种widget
- 初识设计模式
- Cocos2d-x 3.0 编译出错 解决 error: expected ';' at end of member declaration
- Java 日期时间 Date类型,long类型,String类型表现形式的转换
- 函数热身(1)
- RESTful Webservice 和 SOAP Webserivce 对比及区别