NHibernate3 analysis: Query Text nhibernate.linq enhanced queries

Source: Internet
Author: User

Series Introduction

NHibernate3.0 Analysis Series from configuration, mapping, query, session strategy, Applications and other aspects of the comprehensive disclosure of NHibernate3.0 new features and applications and the integration of various applications, based on the NHibernte3.0 version number.

Let's say you're not familiar with Nhibernate. Get started with the high-speed Reading NHibernate series article navigation Series. Let's say You're already using Nhibernate. Then please keep up with the NHibernate3.0 Anatomy series .

    • NHibernate Special Topic: http://kb.cnblogs.com/zt/nhibernate/
    • NHibernate official Website: http://nhforge.org/
    • NHibernate documentation: http://nhforge.org/doc/nh/en/
    • Get NHibernate address: http://sourceforge.net/projects/nhibernate/
Enhanced Query Overview

NHIBERNATE.LINQ except for standard query Operators. NHIBERNATE.LINQ also specifically provides the two enhanced query methods that are unique to NHibernate, which are immediately crawled (eagerfetching) and query cache (querycacheable).

Crawl now (eagerfetching)

Let's say we don't set lazy= "false" for object affinity in the mapping file, which is delayed loading by default. The NHibernate3.0 provides four extension methods. Fetch and Thenfetch,fetchmany and Thenfetchmany are respectively. At Query Time. Use these methods to load the associated object immediately.

Example analysis

The association relationship is delayed loading by default. For example, the following NHIBERNATE.LINQ query queries out all the Customer. Its order collection is delayed-loaded by Default.

//Code Snippets Copyright http://lyj.cnblogs.com/x = session.Query<Customer>().ToList();

Use FETCH to load the association relationship IMMEDIATELY. For example, load all the Customer object order collections Immediately.

//Code Snippets Copyright http://lyj.cnblogs.com/x = session.Query<Customer>().Fetch(c => c.Orders).ToList();

Use the Fetch object to load multiple associations immediately. Assuming that an object has multiple collections, we can load multiple associations immediately using the following METHODS. For example, the employee object has subordinates and orders Set. The subordinates and orders collections of all employee objects are loaded immediately using the following method.

//Code Snippets Copyright http://lyj.cnblogs.com/x = session.Query<Employee>()    .Fetch(e => e.Subordinates)    .Fetch(e => e.Orders).ToList();

Use Fetch and Thenfetch,fetchmany and thenfetchmany to load nested associations immediately, such as the customer object has an order collection, and the order collection has multiple OrderLines collections. Be able to load them all at once using the following METHODS.

//Code Snippets Copyright http://lyj.cnblogs.com/x = session.Query<Customer>()    .FetchMany(c => c.Orders)    .ThenFetchMany(o => o.OrderLines).ToList();
Query Cache (querycacheable)

NHIBERNATE3.0 provides support for query caching (querycacheable) in three extension methods.

    • Cacheable is used to turn on query caching.
    • The Cachemode is used to set the cache Policy.
    • The cacheregion is used to set the cache Area.

The following NHIBERNATE.LINQ query opens the query Cache. When you run this query. First from the Querycache inside Query. See if it exists, does not exist, then queries the database and puts it into querycache, which is obtained directly from the Querycache.

//Code Snippets Copyright http://lyj.cnblogs.com/q = session.Query<Customer>().Cacheable().ToList();

The following LINQ query opens the query Cache. Set the cache area and Policy.

//Code Snippets Copyright http://lyj.cnblogs.com/q = session.Query<Customer>()    .Cacheable().CacheRegion("Test")    .CacheMode(CacheMode.Put).ToList();
Example analysis

The Istatistics interface provides queryexecutioncount, querycacheputcount, and Querycachehitcount three properties to count the number of query cache runs, put number, hit Count.

Note NHibernate does not enable query caching by default, we need the amount to be configured:

//Code Snippets Copyright http://lyj.cnblogs.com/cfg.SetProperty(Environment"true");

For example: run the same query two times, and verify that the number of queries run is 1,put 1. Hit number is 1.

//code Snippets Copyright http://lyj.cnblogs.com/[Test]public voidQuerycacheable () {SessionFactory.Statistics.Clear (); SessionFactoryImplementor.QueryCache.Clear ();varSession = Sessionfactory.opensession ();//execution and Put QueryvarQ = Session. query<Customer> (). Cacheable (). ToList ();//get Results from QuerycachevarQ2 = 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);}

Suppose you use NHibernate Profiler to monitor the test above, you can see that it runs a statement, and the second one uses the query cache directly.

Conclusion

Based on the NHIBERNATE.LINQ standard query, This paper introduces the nhibernate-specific two nhibernate.linq enhanced query crawl (eagerfetching) and query cache (querycacheable).

The next article Continues.

NHibernate3 analysis: Query Text nhibernate.linq enhanced queries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.