?<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%''
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配置
OpenSessionInViewFilter配置
??? 它有兩種配置方式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配置
- ?? ??
- < 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 > ???
<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配置
- ? ??
- < web-app > ? ??
- ...? ??
- < filter > ? ??
- < filter-name > hibernateFilter </ filter-name > ? ??
- < filter-class > ?org.springframework.orm.hibernate3.support.OpenSessionInViewFilter? </ filter-class > ? ??
- <!--?singleSession默認為true,若設為false則等于沒用OpenSessionInView?--> ? ??
- < init-param > ? ??
- < param-name > singleSession </ param-name > ? ??
- < param-value > true </ param-value > ? ??
- </ init-param > ? ??
- </ filter > ?...? < filter-mapping > ? ??
- < filter-name > hibernateFilter </ filter-name > ? ??
- < url-pattern > *.do </ url-pattern > ? ??
- </ filter-mapping > ?...? </ web-app > ???
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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