我用了OpenSessionInViewInterceptor报错org.hibernate.HibernateException: collection is not associated with any session,然后又用OpenSessionInViewFilter还是报同样错误,请求终极解决方案,高手帮帮忙!
/** * we do a different flushmode than in the codebase * here */ protected Session getSession(org.hibernate.SessionFactory sessionFactory) throws org.springframework.dao.DataAccessResourceFailureException { Session session = SessionFactoryUtils.getSession(sessionFactory, true); session.setFlushMode(FlushMode.COMMIT); return session; } /** * we do an explicit flush here just in case * we do not have an automated flush */ protected void closeSession(Session session, SessionFactory factory) { session.flush(); super.closeSession(session, factory); } }用这个类替换你的OpenSessionInView,在web.xml中,如果用到了struts2,记得把OpenSessionInViewFilter的映射放到FilterDispatcher的前面
spring重新配置事务管理,将session与具体的事务相关联 怎么配.!
Spring具体事务的开启与关闭是通过spring的事务拦截器来实现的,具体的开启时间和关闭时间,是当 执行到这个方法和退出 这个方法的时候,你可以参考一下spring的官方文档。
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
/**
* we do a different flushmode than in the codebase
* here
*/
protected Session getSession(org.hibernate.SessionFactory sessionFactory) throws org.springframework.dao.DataAccessResourceFailureException {
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.COMMIT);
return session;
}
/**
* we do an explicit flush here just in case
* we do not have an automated flush
*/
protected void closeSession(Session session, SessionFactory factory) {
session.flush();
super.closeSession(session, factory);
}
}用这个类替换你的OpenSessionInView,在web.xml中,如果用到了struts2,记得把OpenSessionInViewFilter的映射放到FilterDispatcher的前面