您的位置:首页 > 其它

Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly'

2010-07-19 16:50 561 查看
最近的一个SpringMVC+Hibernate的项目中,执行save操作的时候出现一下问题:

Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly'

搜索了很久终于找到一个好的解决办法。分享给大家。

在web.xml中:

<filter>
<filter-name>CloseSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OurOpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>


OurOpenSessionInViewFilter的代码如下:

package org.springframework.orm.hibernate3.support;

import org.hibernate.*;

/**
* @author <a href="mailto: mackjieson@gmail.com" mce_href="mailto: mackjieson@gmail.com">zhoujie</a>
*
*/
public class OurOpenSessionInViewFilter extends OpenSessionInViewFilter{

public OurOpenSessionInViewFilter() {
super.setFlushMode(FlushMode.AUTO);
}

protected void closeSession(Session session, SessionFactory sessionFactory) {
session.flush();

try {
session.getTransaction().commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block//e.printStackTrace();
}

super.closeSession(session, sessionFactory);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐