Hibernate3中could not initialize proxy - no Session的解決辦法


如果在對象中存在外鍵關聯的引用,在Javabean中體現為list對象,hibernate在加載時只默認加載單個屬性:

例如:class User{String name;String password;List Address}

那么在查詢時hibernate只默認加載出name和password,當需要使用到address這個List的時候才會去從新查詢。

這是hibernate的延遲加載機制,但是在這個地方到查處name和password后hibernate的session就會關閉,當再去查找時就會拋出no session的exception

在此處有兩中解決辦法:

1、為<set> 和<one-many> 增加屬性 lazy=false

2、在web.xml中使用OpenSessionInViewFilte

		<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>/*</url-pattern>
	</filter-mapping>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM