hibernate懒加载异常处理

1、实体类中使用lazy="false"

2、lazy="ture"   在web.xml中加入

      程序代码
    <filter> 
     <filter-name>hibernateFilter</filter-name> 
     <filter-class> 
     org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 
     </filter-class> 
   </filter 
   <filter-mapping> 
     <filter-name>hibernateFilter</filter-name> 
     <url-pattern>*.do</url-pattern> 
    </filter-mapping>
 
    此方式容易遇到Write operations are not allowed in read-only mode问题,解决方法:重写OpenSessionInViewFilter的2个方法
     public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
 

     
        /**
         * we do a different flushmode than in the codebase
         * here
         */
        protected Session getSession(SessionFactory sessionFactory) throws 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);
        }
}

原文地址:https://www.cnblogs.com/lengzhijun/p/4261670.html