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);
        }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。