標籤:img alt 多個 eth 關係 stat 介面 sans ati
系列引入
NHibernate3.0剖析系列分別從Configuration篇、Mapping篇、Query篇、Session策略篇、應用篇等方面全面揭示NHibernate3.0新特性和應用及其各種應用程式的整合,基於NHibernte3.0版本號碼。
假設你還不熟悉NHibernate。能夠高速閱讀NHibernate之旅系列文章導航系列入門。假設你已經在用NHibernate了。那麼請跟上NHibernate3.0剖析系列吧。
- NHibernate專題:http://kb.cnblogs.com/zt/nhibernate/
- NHibernate官方網站:http://nhforge.org/
- NHibernate參考文檔:http://nhforge.org/doc/nh/en/
- 擷取NHibernate地址:http://sourceforge.net/projects/nhibernate/
增強查詢概述
NHibernate.Linq除了提供標準查詢運算子外。NHibernate.Linq還專門提供了NHibernate特有的兩個增強查詢方法,各自是馬上抓取(EagerFetching)和查詢快取(QueryCacheable)。
馬上抓取(EagerFetching)
假設我們不在Mapping檔案裡對對象關聯關係設定Lazy="false",預設是延遲載入的。NHibernate3.0提供了四種擴充方法。分別為Fetch及ThenFetch,FetchMany及ThenFetchMany。在查詢時。使用這些方法用於馬上傳入出關聯對象。
執行個體分析
關聯關係預設是延遲載入的。比如以下NHibernate.Linq查詢查詢出全部Customer。其Order集合預設是延遲載入的。
//Code Snippets Copyright http://lyj.cnblogs.com/var x = session.Query<Customer>().ToList();
使用Fetch馬上傳入關聯關係。比如馬上傳入全部Customer對象Order集合。
//Code Snippets Copyright http://lyj.cnblogs.com/var x = session.Query<Customer>().Fetch(c => c.Orders).ToList();
使用Fetch對象馬上傳入多個關聯關係。假設一個對象有多種集合,我們能夠使用以下方法馬上傳入多個關聯關係。比如Employee對象有Subordinates及Orders集合。使用以下方法馬上傳入出全部Employee對象的Subordinates和Orders集合。
//Code Snippets Copyright http://lyj.cnblogs.com/var x = session.Query<Employee>() .Fetch(e => e.Subordinates) .Fetch(e => e.Orders).ToList();
使用Fetch及ThenFetch,FetchMany及ThenFetchMany馬上傳入嵌套關聯,比如Customer對象有Order集合,Order集合也有多個OrderLines集合。能夠使用以下方法所有馬上傳入出來。
//Code Snippets Copyright http://lyj.cnblogs.com/var x = session.Query<Customer>() .FetchMany(c => c.Orders) .ThenFetchMany(o => o.OrderLines).ToList();
查詢快取(QueryCacheable)
NHibernate3.0提供了三種擴充方法對查詢快取(QueryCacheable)的支援。
- Cacheable用於開啟查詢快取。
- CacheMode用於設定緩衝策略。
- CacheRegion用於設定快取區域。
以下NHibernate.Linq查詢開啟查詢快取。當運行這句查詢時。首先從QueryCache裡面查詢。看看是否存在了,不存在則查詢資料庫後放入QueryCache,存在則直接從QueryCache中擷取。
//Code Snippets Copyright http://lyj.cnblogs.com/var q = session.Query<Customer>().Cacheable().ToList();
以下Linq查詢開啟查詢快取。設定快取區域和策略。
//Code Snippets Copyright http://lyj.cnblogs.com/var q = session.Query<Customer>() .Cacheable().CacheRegion("Test") .CacheMode(CacheMode.Put).ToList();執行個體分析
IStatistics介面提供QueryExecutionCount、QueryCachePutCount、QueryCacheHitCount三個屬性用來統計查詢快取運行數目、Put數目、擊中數目。
注意NHibernate預設不啟用查詢快取,我們須要額為配置:
//Code Snippets Copyright http://lyj.cnblogs.com/cfg.SetProperty(Environment.UseQueryCache, "true");
比如以下範例:運行兩次同樣的查詢,驗證查詢運行數目為1,Put數目為1。擊中數目為1。
//Code Snippets Copyright http://lyj.cnblogs.com/[Test]public void QueryCacheable(){ SessionFactory.Statistics.Clear(); SessionFactoryImplementor.QueryCache.Clear(); var session = SessionFactory.OpenSession(); //Execution and Put Query var q = session.Query<Customer>().Cacheable().ToList(); //Get Results from QueryCache var q2 = session.Query<Customer>().Cacheable().ToList(); SessionFactory.Statistics.QueryExecutionCount.Should().Be.EqualTo(1); SessionFactory.Statistics.QueryCachePutCount.Should().Be.EqualTo(1); SessionFactory.Statistics.QueryCacheHitCount.Should().Be.EqualTo(1);}
假設使用NHibernate Profiler監視上面的測試,能夠看到其運行了一條語句,第二條直接使用查詢快取。
結語
這篇在NHibernate.Linq標準查詢的基礎上,介紹了NHibernate特有的兩個NHibernate.Linq增強查詢馬上抓取(EagerFetching)和查詢快取(QueryCacheable)。
下篇繼續。
NHibernate3剖析:Query篇之NHibernate.Linq增強查詢