關於Hibernate懶加載----Spring filter

處理方法:在web.xml中,配置(攔截器須要在sturts2以前)web

<!-- Spring 懶加載 攔截器 -->
	<filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>

1、關於懶加載
spring

    所謂懶加載(lazy)就是延時加載,延遲加載。緩存

    何時用懶加載呢,我只能回答要用懶加載的時候就用懶加載。併發

    至於爲何要用懶加載呢,就是當咱們要訪問的數據量過大時,明顯用緩存不太合適,app

    由於內存容量有限 ,爲了減小併發量,減小系統資源的消耗,url

    咱們讓數據在須要的時候才進行加載,這時咱們就用到了懶加載。spa

    好比部門ENTITY和員工ENTITY,部門與員工1對多,若是lazy設置爲 false,那麼只要加載了一個部門的po,就會根據一對多配置的關係把全部員工的po也加載出來。可是實際上有時候只是須要用到部門的信息,不須要用到 員工的信息,這時員工po的加載就等於浪費資源。若是lazy設置爲true,那麼只有當你訪問部門po的員工信息時候纔回去加載員工的po的信息。hibernate

2、關於  org.hibernate.LazyInitializationException   ----Could not initialize proxy - the owning Session was closed!code

       錯誤出現緣由,orm

    解決方法:

相關文章
相關標籤/搜索