日期:2014-05-20  浏览次数:20700 次

关于hibernate 的session和缓存问题。
最近在用s2sh做一个小项目。
其中。hibernate中的一对多,多对1级查询出现延时加载的问题。
然后我配置了spring的openSessionInViewFilter。
然后新增不会出现问题。但是更新出现问题。
Java code
org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
    org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)


这个应该是缓存问题。没有好的解决方案。就改用了merge()方法。
但是做批量删除的时候
Java code
    String hql="from Roles where id in(";
        for(int i=0;i<roles.length-1;i++)
        {
            hql+=roles[i]+",";
        }
        hql+=roles[roles.length-1]+")";
        this.getHibernateTemplate().clear();
        this.getHibernateTemplate().flush();
        List<Roles> list=this.getHibernateTemplate().find(hql);
        this.getHibernateTemplate().deleteAll(list);

还是挂掉了。错误和上面一样。



web。xml
Java code
    <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>

    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>






------解决方案--------------------
Illegal attempt to associate a collection with two open sessions 
非法试图把一个集合用于两次开着的session 
有可能你自己开了个session.然后spring配置又默认给这个类启动了一个session.

<bean id="insertFamerAction" class="com.farmer.action.InsertFamerAction" scope="prototype">
<property name="businessService">
<ref bean="businessService"/>
</property>
 </bean>

hibernate中,是不允许出现同一主键对象有两个不同session同时关联的情况,如果出现这种情况hibernate会抛出"org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions"这个异常,这个时候怎么办呢?我们需要通过hibernate提供的merge()这个方法来解决这一问题。方法原型:
public Object merge(Object object)
使用merge方法是有如下情况
1、如果session中并不存在于参数object具有相同主见的对象,那么,首先需要根据主键从数据库中加载出该对象,然后将参数object中的属性拷贝到加载出来的那个对象上面,最后返回该持久化对象,同时需要注意,参数object并没有关联到session上。 2、如果session中存在与参数objcet具有相同主键属性的对象,那么直接将参数object中的属性拷贝到该持久化对象上,然后将该持久化对象返回,同时需要注意,参数object并没有关联到session上。
------解决方案--------------------
探讨
这个应该是缓存问题。没有好的解决方案。就改用了merge()方法。
但是做批量删除的时候
还是挂掉了