Series Introduction
The analysis series of nhibernate3.0 fully reveals new features, applications and various applications of nhibernate3.0 in terms of configuration, mapping, query, session policy, and application.ProgramBased on nhibernte3.0. If you are not familiar with Nhibernate, you can quickly get started with the Nhibernate series Article navigation series. If you are already using Nhibernate, keep up with the nhibernate3.0 series.
- Nhib.pdf topics: http://kb.cnblogs.com/zt/nhibernate/
- Nhibernate site: http://nhforge.org/
- Nhib.pdf Reference document: http://nhforge.org/doc/nh/en/
- Get Nhibernate address: http://sourceforge.net/projects/nhibernate/
Enhanced query Overview
In addition to standard query operators, nhibloud. LINQ also provides two Enhanced query methods unique to nhibloud, namely, eagerfetching and querycacheable ).
Capture now (eagerfetching)
If we do not set lazy = "false" for object associations in the mapping file, the default value is delayed loading. nhibernate3.0 provides four extension methods. Fetch, thenfetch, fetchmany, and thenfetchmany. These methods are used to load associated objects immediately during query.
Instance analysis
By default, the join relationship is delayed. For example, if nhib.pdf. LINQ queries all the customers, the order set is delayed by default.
// Code snippets copyright http://lyj.cnblogs.com/VaRX = session. query <Customer> (). Tolist ();
Use fetch to immediately load associations, for example, loading all the order sets of customer objects immediately.
// Code snippets copyright http://lyj.cnblogs.com/VaRX = session. query <Customer> (). Fetch (C => C. Orders). tolist ();
Use a fetch object to immediately load multiple associations. If an object has multiple sets, we can use the following method to load multiple associations immediately. For example, if the employee object contains a subordinates and orders set, use the following method to immediately load the subordinates and orders sets of all the employee objects.
// Code snippets copyright http://lyj.cnblogs.com/VaRX = session. query <Employee> (). Fetch (E => E. subordinates). Fetch (E => E. Orders). tolist ();
Use fetch, thenfetch, fetchmany, and thenfetchmany to immediately load nested associations. For example, the customer object has an order set and the order set has multiple orderlines sets. You can use the following methods to load all of them immediately.
// Code snippets copyright http://lyj.cnblogs.com/VaRX = session. query <Customer> (). Fetchders (C => C. Orders). thenfetchmany (O => O. orderlines). tolist ();
Querycacheable)
Nhibernate3.0 provides three extension methods for querycacheable.
- Cacheable is used to enable query cache.
- Cachemode is used to set cache policies.
- Cacheregion is used to set the cache region.
The following nhib.pdf. the query cache is enabled for the LINQ query. When you execute this query, first query the query from the querycache to check whether the query exists. If the query does not exist, the query database is placed in the querycache. If the query exists, it is directly obtained from the querycache.
// Code snippets copyright http://lyj.cnblogs.com/VaRQ = session. query <Customer> (). Cacheable (). tolist ();
The following describes how to enable the query cache for the LINQ query and set the cache region and policy.
// Code snippets copyright http://lyj.cnblogs.com/VaRQ = session. query <Customer> (). Cacheable (). cacheregion ("Test"). Cachemode (Cachemode. Put). tolist ();
Instance analysis
The istatistics interface provides the queryexecutioncount, querycacheputcount, and querycachehitcount attributes to count the number of cache executions, put count, and hit count.
Note that the query cache is not enabled for Nhibernate by default. We need to configure it as follows:
// Code snippets copyright http://lyj.cnblogs.com/Cfg. setproperty (Environment. Usequerycache,"True");
For example, if the same query is executed twice, the number of verification queries is 1, the number of put statements is 1, and the number of hits is 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. shocould (). be. similar to (1); sessionfactory. statistics. querycacheputcount. shocould (). be. similar to (1); sessionfactory. statistics. querycachehitcount. shocould (). be. similar to (1 );}
If you use nhib1_profiler to monitor the test above, you can see that it executes a statement, and the second directly uses the query cache.
Conclusion
Based on the standard nhibloud. LINQ query, this article introduces two native. LINQ enhanced queries (eagerfetching) and query cache (querycacheable) that are unique to nhibloud ). Next, continue.
Additional reading
Mike hadlow: nhibw.linq eager fetching
Ayende: eagerly loading entity associations efficiently with Nhibernate
I hope this article will help you.
Tags: Nhib1_nhibernate3.0 nhib1_query nhib1_. LINQ