SSH架构中Illegal attempt to associate a collection with two open sessions 问题
2010-10-25 16:40
447 查看
多对多关系表的save保存动作
hbm配置:
BEAN保存部分代码:
web.xml 中Session配置部分
使用以上配置保存一个BsdUser对象时报错:
Illegal attempt to associate a collection with two open sessions
解决上面的问题,只需修改web.xml中OpenSession的属性配置
说明一下Open Session in View的作用,
如果是singleSession=true的话,
就是允许在每次的整个request的过程中使用同一个hibernate session,可以在这个request任何时期lazy loading数据。
如果是singleSession=false的话,就不会在每次的整个request的过程中使用同一个hibernate session,而是每个数据访问都会产生各自的seesion,等于没有Open Session in View。
hbm配置:
<set name="groups" table="MNG_GROUP_USER" inverse="false" lazy="true" cascade="save-update"> <key column="user_id"/> <many-to-many column="group_id" class="MngGroup" /> </set>
BEAN保存部分代码:
public void saveBsdUser(BsdUser user,String[] ids){ if(ids.length>0){ // 关联组 Set<MngGroup> groups = new HashSet(); for(int i=0;i<ids.length;i++){ MngGroup group = (MngGroup)getHibernateTemplate().find( "from " + MngGroup.class.getName()+ " where id=?", new Integer(ids[i])).get(0); groups.add(group); } user.setGroups(groups); } try { this.getHibernateTemplate().saveOrUpdate(user); this.getHibernateTemplate().flush(); } catch (DataAccessException e) { logger.error("Save error:" + e); throw e; // return null; } }
web.xml 中Session配置部分
<filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>false</param-value> </init-param> </filter>
使用以上配置保存一个BsdUser对象时报错:
Illegal attempt to associate a collection with two open sessions
解决上面的问题,只需修改web.xml中OpenSession的属性配置
<init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param>
说明一下Open Session in View的作用,
如果是singleSession=true的话,
就是允许在每次的整个request的过程中使用同一个hibernate session,可以在这个request任何时期lazy loading数据。
如果是singleSession=false的话,就不会在每次的整个request的过程中使用同一个hibernate session,而是每个数据访问都会产生各自的seesion,等于没有Open Session in View。
相关文章推荐
- org.hibernate.HibernateException: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;
- 解决HibernateException: 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 session问题
- 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 session问题
- HibernateException: Illegal attempt to associate a collection with two open sessions
- OpenSessionInViewFilter的两个异常:Illegal attempt to associate a collection with two open sessions
- Illegal attempt to associate a collection with two open sessions
- 异常描述:hibernate懒加载中,用OpenSessionInViewFilter解决之后,同时对一个collection创建两个session访问导致异常(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
- Hibernate Merge (Illegal attempt to associate a collection with two open sessions)
- Illegal attempt to associate a collection with two open sessions
- Hibernate 异常: Illegal attempt to associate a collection with two open sessions