//把fetch改成join就能解決couldnotinitializeproxy-theowningSessionwasclosed這個問題innerjoin(內連接)leftouterjoin(左外連接)rightouterjoin(右" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

解決hibernate中的lazy的問題

系統 2192 0
?<many-to-one name="TCustomerComeCategory" class="com.hnyxsm.modules.customer.TCustomerComeCategory" fetch="join">
//把fetch改成join就能解決 could not initialize proxy - the owning Session was closed這個問題
inner join (內連接)

left outer join (左外連接)

right outer join (右外連接)

full join (全連接,并不常用)

語句inner join , left outer join 以及 right outer join 可以簡寫。
from Cat as cat???? join cat.mate as mate??? left join cat.kittens as kitten

通過HQL的with關鍵字,你可以提供額外的 join 條件。
from Cat as cat???? left join cat.kittens as kitten???????? with kitten.bodyWeight > 10.0

還有,一個" fetch "連接允許僅僅使用一個選擇語句就將相關聯的對象或一組值的集合隨著他們的父對象的初始化而被初始化,這種方法在使用到集合的情況下尤其有用, 對于關聯和集合來說,它有效的代替了映射文件中的外聯接 與延遲聲明(lazy declarations).
from Cat as cat???? inner join fetch cat.mate??? left join fetch cat.kittens

一個 fetch 連接通常不需要被指定別名, 因為相關聯的對象不應當被用在 where 子句 (或其它任何子句)中。同時,相關聯的對象 并不在查詢的結果中直接返回,但可以通過他們的父對象來訪問到他們。
from Cat as cat???? inner join fetch cat.mate??? left join fetch cat.kittens child??? left join fetch child.kittens

假若使用iterate()來調用查詢,請注意 fetch 構造是不能使用的(scroll() 可以使用)。 fetch 也不應該與setMaxResults() 或setFirstResult()共用,這是因為這些操作是基于結果集的,而在預先抓取集合類時可能包含重復的數據,也就是說無法預先知道精確的行數。 fetch 還不能與獨立的 with條件一起使用。通過在一次查詢中 fetch 多個集合,可以制造出笛卡爾積,因此請多加注意。對bag映射來說,同時 join fetch 多個集合角色可能在某些情況下給出并非預期的結果,也請小心。最后注意,使用full join fetch 與 right join fetch 是沒有意義的。

如果你使用屬性級別的延遲獲取(lazy fetching)(這是通過重新編寫字節碼實現的),可以使用 fetch all properties 來強制Hibernate立即取得那些原本需要延遲加載的屬性(在第一個查詢中)。
from Document fetch all properties order by name
from Document doc fetch all properties where lower(doc.name) like ''%cats%''
通常情況下,我們并不使用映射文檔進行抓取策略的定制。更多的是,保持其默認值,然后在特定的事務中, 使用HQL的左連接抓取(left join fetch ) 對其進行重載。這將通知 Hibernate在第一次查詢中使用外部關聯(outer join),直接得到其關聯數據。 在條件查詢 API中,應該調用 setFetchMode(FetchMode.JOIN)語句。
?在沒有使用Spring提供的Open Session In View情況下,因需要在service(or Dao)層里把session關閉,所以lazy loading 為true的話,要在應用層內把關系集合都初始化,如 company.getEmployees(),否則Hibernate拋session already closed Exception;??? Open Session In View提供了一種簡便的方法,較好地解決了lazy loading問題.????
??? 它有兩種配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具體參看SpringSide),功能相同,只是一個在web.xml配置,另一個在application.xml配置而已。????
???? Open Session In View在request把session綁定到當前thread期間一直保持hibernate session在open狀態,使session在request的整個期間都可以使用,如在View層里PO也可以lazy loading數據,如 ${ company.employees }。當View 層邏輯完成后,才會通過Filter的doFilter方法或Interceptor的postHandle方法自動關閉session。
???? OpenSessionInViewInterceptor配置
Xml代碼 復制代碼
  1. ?? ??
  2. < beans > ? ??
  3. < bean ? name = "openSessionInViewInterceptor" ? class = "org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor" > ? ??
  4. < property ? name = "sessionFactory" > ? ??
  5. < ref ? bean = "sessionFactory" /> ? ??
  6. </ property > ? ??
  7. </ bean > ? ??
  8. < bean ? id = "urlMapping" ? class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > ? ??
  9. < property ? name = "interceptors" > ? ??
  10. < list > ? ??
  11. < ref ? bean = "openSessionInViewInterceptor" /> ? ??
  12. </ list > ? ??
  13. </ property > ? ??
  14. < property ? name = "mappings" > ? ??
  15. ...? ??
  16. </ property > ? ??
  17. </ bean > ?...? </ beans > ???
        
<beans> 
<bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> 
<property name="sessionFactory"> 
<ref bean="sessionFactory"/> 
</property> 
</bean> 
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
<property name="interceptors"> 
<list> 
<ref bean="openSessionInViewInterceptor"/> 
</list> 
</property> 
<property name="mappings"> 
... 
</property> 
</bean> ... </beans> 

    

OpenSessionInViewFilter配置
Xml代碼 復制代碼
  1. ? ??
  2. < web-app > ? ??
  3. ...? ??
  4. < filter > ? ??
  5. < filter-name > hibernateFilter </ filter-name > ? ??
  6. < filter-class > ?org.springframework.orm.hibernate3.support.OpenSessionInViewFilter? </ filter-class > ? ??
  7. <!--?singleSession默認為true,若設為false則等于沒用OpenSessionInView?--> ? ??
  8. < init-param > ? ??
  9. < param-name > singleSession </ param-name > ? ??
  10. < param-value > true </ param-value > ? ??
  11. </ init-param > ? ??
  12. </ filter > ?...? < filter-mapping > ? ??
  13. < filter-name > hibernateFilter </ filter-name > ? ??
  14. < url-pattern > *.do </ url-pattern > ? ??
  15. </ filter-mapping > ?...? </ web-app > ???

解決hibernate中的lazy的問題


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 晋中市| 博罗县| 洞口县| 郧西县| 仙游县| 曲松县| 延边| 信丰县| 宁津县| 淮滨县| 高要市| 邵阳县| 浠水县| 呼玛县| 育儿| 伊川县| 开江县| 阿城市| 顺义区| 滨州市| 嵩明县| 崇礼县| 岳阳市| 丹江口市| 太谷县| 宁国市| 荆门市| 金平| 汉寿县| 浦江县| 周口市| 林口县| 依兰县| 桐乡市| 仙居县| 台湾省| 宣威市| 鄂托克旗| 太仆寺旗| 昌宁县| 信阳市|