Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

          使用 Spring 整合 Hibernate, 在懶加載的情況下, 有時候需要在 JSP/View 層顯示數據, 這時候就要用到Spring內置的: OpenSessionInViewFilter, 一般來說配置如下(web.xml):

          <filter>
          <filter-name>hibernateFilter</filter-name>
          <
          filter-class>
          org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
          </filter-class>
          <
          init-param>
          <
          param-name>singleSession</param-name>
          <param-value>true</param-value>
          </
          init-param><!-- 和 spring 中的sesssionfactory ID 一致 -->
          </filter>
          <
          filter-mapping><filter-name>hibernateFilter</filter-name><url-pattern>*.do</url-pattern><!-- *.jsp, *.do--></filter-mapping>
          不過, 這時候又會導致更新數據時拋出如下異常:
          Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

          這時候再去網上找解決方案, 會有人說: 把參數 singleSession改為false, 就行了. 不過, 改完后, 估計不久就會遇到另一個郁悶的異常:

          org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

          這下完了, 兩個方案都不行, 到底怎么辦? 還好, 在http://xuliangyong.javaeye.com/blog/144818的主頁上, 給了一個方案, 就是改寫 OpenSessionInViewFilter 的代碼, 非常感謝, 下面給出的就是最終方案:

          web.xml

          < filter-name >hibernateFilter</filter-name>

          < filter-class > org.springframework.orm.hibernate3.support.OurOpenSessionInViewFilter </filter-class>

          OurOpenSessionInViewFilter.java 代碼:

          package org.springframework.orm.hibernate3.support;



          import org.hibernate.*;



          /** * 單session模式下, 默認會發生無法提交的錯誤: * Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. * 需要設置FlushMode并刷新session. * 參考: http://xuliangyong.javaeye.com/blog/144818 * @author 劉長炯 */publicclass OurOpenSessionInViewFilter extends OpenSessionInViewFilter {



          public OurOpenSessionInViewFilter() {

          super.setFlushMode(FlushMode.AUTO);

          }



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

          }

          }

          如果各位有更好的解決方案, 歡迎討論哦!!!

          題外話:

          感覺 Spring + Hibernate 的健壯性還是不夠啊! 容易拋異常, 這是事實, 也許這是開源軟件的通病吧.

          posted on 2009-10-29 22:23 seal 閱讀(1567) 評論(0)  編輯  收藏 所屬分類: SpringHibernate
          主站蜘蛛池模板: 东城区| 北辰区| 雷山县| 和林格尔县| 靖边县| 贡觉县| 溧阳市| 阳江市| 临夏市| 化德县| 惠水县| 柞水县| 繁昌县| 永兴县| 岳阳县| 遂平县| 桐梓县| 新龙县| 渭源县| 南丹县| 临朐县| 汽车| 申扎县| 兴文县| 周口市| 苏尼特左旗| 阿鲁科尔沁旗| 晋中市| 白玉县| 盘山县| 石首市| 永靖县| 杨浦区| 罗江县| 泊头市| 保定市| 抚州市| 北海市| 齐河县| 金昌市| 佛坪县|