failed to lazily initialize a collection of role問題

來源:互聯網
上載者:User

今天遇到了這樣一個問題

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
在網上找了好久終於找到了一個較全的解決方案。具體如下:

  "org.hibernate.LazyInitializationException: could not initialize proxy"延遲抓取出的錯,hb3對many-to-one的預設處理是lazy = "proxy",把所有many-to-one,one-to-one都加上lazy="false"...

這個方法理論上沒錯,但是會讓人產生誤解,使人以為只是修改<set>中的lazy值,照做後依然會報錯,看看下面的文章也許就明白了(注意最後一行紅色標記)!

  lazy initialization

  集合(不包括數組)是可以延遲初始化的,意思是僅僅當應用程式需要訪問時,才載入他們的值。

s = sessions.openSession();
User u = (User) s.find("from User u where u.name=?", userName,
Hibernate.STRING).get(0);
Map permissions = u.getPermissions();
s.connection().commit();
s.close();
Integer accessLevel = (Integer) permissions.get("accounts"); //Error!

  因為在commit之前,permissions沒有被用到,permission沒有被初始化,而session被close了,導致permissions永遠都無法load data.

  解決辦法:吧最後一行一道session被提交之前.

  設定:

<set name="names" table="NAMES" lazy="true" order-by="name asc">
<key column="group_id"/>
<element column="NAME" type="string"/>
</set>

  ==================================

  在做Spring1.2 + Hibernate 3 + struts demo時候遇到一個問題,編輯使用者資料時候

  用com.jeedev.service.dao.hibernate.UserHibernateDao 中 public TSysuser getUser(int userid) { } 方法,總是不能正常顯示編輯資料。具體表現為:

  參考 com.jeedev.service.dao.hibernate.UserHibernateDao 第32-45行

  java代碼:

public TSysuser getUser(int userid) {
  if (this.getHibernateTemplate() == null) {
System.out.println("error at there");
return null;
}
  TSysuser edituser= (TSysuser) getHibernateTemplate().load(TSysuser.class, new Integer(userid));
  System.out.println(edituser.getUsername());
System.out.println(edituser.getDeptno());
  return edituser;
}

  如果刪除上面的

  java代碼:

System.out.println(edituser.getUsername());
System.out.println(edituser.getDeptno());

  在點擊編號,修改時候就會出現錯誤:

  2005-10-27 15:35:59,245 ERROR [org.hibernate.LazyInitializationException] - could not initialize proxy - the owning Session was closed

  org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed

  經過在javaeye論壇大家一致認為 是hibernate lazy的原因

  在WEB層級應用時候,會出現意想不到的lazy錯誤,解決方案就是 設定 lazy="false" 例:

  <hibernate-mapping>

  <class name="com.jeedev.hb.TSysuser" table="t_sysuser" 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>

 

原文作者:My Java

原文地址:http://blog.csdn.net/yoyooop/article/details/5698555

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.