Illegal attempt to associate a collection with two open session
来源:互联网 发布:周润发耍大牌知乎 编辑:程序博客网 时间:2024/05/18 03:27
本文转载自:
http://whq0417.blog.163.com/blog/static/12795144120098150238663/?NI5TH7NZFE=yiFfuCpBty
Hibernate交给spring 管理了...
我的出现上述问题了..代码如下:
/** 修改 **/
public void update(Object entity) {
super.getHibernateTemplate().update(entity);
}
/** 根据Id查找* */
public Restype queryById(Object entityid) {
String sql = "from Restype where typeid = " + entityid;
Restype restype = null;
Query query = super.getSession().createQuery(sql);
List type = query.list();
if (type.size() > 0) {
restype = (Restype) type.get(0);
}
return restype;
}
只查询或者只修改都没有问题,问题是2者一起用,就报上面的错..原因就是开了2个seesion:
搞了2天终于解决了..
将查找方法修改为:
/** 根据Id查找* */
public Restype queryById(Object entityid) {
String sql = "from Restype where typeid = " + entityid;
Restype restype = null;
List list = super.getHibernateTemplate().find(sql);
if (list.size() > 0) {
restype = (Restype) list.get(0);
}
return restype;
}
就好了..
大家可以参考:http://thiger.blog.hexun.com/22094206_d.html
后来又遇到了这个问题:
就把更新方法中的update 换成了 merge 解决了问题
大家可以参考:http://www.jiehoo.com/hibernate%E5%92%8Cspring%E9%85%8D%E5%90%88%E4%BD%BF%E7%94%A8%E7%9A%84%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98.htm
或者:http://xiaandong.spaces.live.com/blog/cns!C7C9E9B51C60807!162.entry
merge(Object entity)
Copy the state of the given object onto the persistent object with the same identifier.
update(Object entity)
Update the given persistent instance, associating it with the current Hibernate Session.
参考文档:如果你确定当前session没有包含与之具有相同持久化标识的持久实例,使用update()。 如果想随时合并你的的改动而不考虑session的状态,使用merge()。
我的理解:update只能在保证在session中只有一个持久化标示时,可以使用update,如果你在从数据库或者其他地方得到了另一个持久化对象,标示跟以前的某个对象一样,这个时候,只能用merge
错误情况:使用update有时候会出现Illegal attempt to associate a collection with two open sessions的异常,但是在取得对象后使用session.close()也不行,会出现另外的一个异常session is not open or is closed,原来就是getHibernateTemplate().update(object),修改为getHibernateTemplate().merge(object)问题解决。
merge
Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".
The semantics of this method are defined by JSR-220.
Parameters:
object - a detached instance with state to be copied
Returns:
an updated persistent instance
Throws:
HibernateException
有几种情况:
1.如果idertifier(以下简称为id)为空或不存在,则进行inert动作
2.如果id存在,则进行update动作
至于会话层的就如文档上所说,不再多说。
- Illegal attempt to associate a collection with two open session
- 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.
- net.sf.hibernate.HibernateException: Illegal attempt to associate a collection with two open session
- Illegal attempt to associate a collection with two open session问题
- Hibernate 异常: Illegal attempt to associate a collection with two open sessions
- SSH架构中Illegal attempt to associate a collection with two open sessions 问题
- Hibernate 错误: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
- OpenSessionInViewFilter的两个异常:Illegal attempt to associate a collection with two open sessions
- Hibernate 异常:Illegal attempt to associate a collection with two open sessions
- HibernateException: 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)
- ORACLE 10G创建表空间和创建用户
- 将Excel数据导入Access数据库
- 推荐六种简单方法让网站获得巨大流量
- c# 数组排序
- 话务量单位erl (爱尔兰)
- Illegal attempt to associate a collection with two open session
- Dreamhost Winscp
- 2009-11-6一点杂乱的知识
- 使用yum下载而不安装rpm包
- 平面设计中常用的尺寸
- Window API 创建窗体
- [知识竞赛策划方案][图]何用PPT制作知识竞赛所需要的题库?作为一个普通的单位,由于不具备电视台专用的比赛平台,如果要搞一场极致专业的知识竞赛?同时花钱最少?
- 开放平台规划攻略
- 区分中英文的正则表达式