failed to lazily initialize a collection of role:

 

環境 spring+hibernate3+struts

今天又碰到錯誤

failed to lazily initialize a collection of role: no session or session was closed

試驗了一下發現了幾個解決方法:

1、是把對應一對多的那兩個列lazy=true改爲lazy=false即可

2、對於查詢中如果用的是xxx.load(class,id)則改爲xxx,get(class,id)
3在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>false</param-value>
        </init-param> 

<!--這個--  <init-param>一定要加不然很可能會報錯:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
>
 </filter>

 <filter-mapping>
  <filter-name>hibernateFilter</filter-name>
  <url-pattern>*.mmg</url-pattern>
 </filter-mapping>

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章