NHibernate Data Load Criteria load

Source: Internet
Author: User
Tags abstract expression
Load | Data The criteria is to load data through a set of conditional expressions (Expression), which returns a collection of objects that meet the criteria.

The primary interface is Icriteria, implemented as a Criteriaimpl class, and this class is internal decorated so that it cannot be displayed outside the assembly.
NHibernate provides us with a method Createcriteria in the session object that returns the Icriteria interface.

Here is a list of some of the Icriteria interface's methods:
Setmaxresults: Sets the maximum number of results returned, which can be used for paging;
Setfirstresult: Sets the position of the first object to be returned, which can be used for paging;
SetTimeout: Sets the time-out value for the operation, which is passed to the IDbCommand object;
Add: Adds a conditional expression (expression object) that can be invoked multiple times to combine multiple conditions;
AddOrder: Join the Sorted field (order object);
List: Returns a collection of objects that meet the criteria.

The key to the criteria data load is on the expression object, which makes up the where part of the query statement.

Expression is an abstract (abstract) class that implements the factory method (factory methods) pattern by means of a static method that returns a subclass of the expression class, which is listed below for some common:

Eq: Return to Eqexpression, which is an expression of equal judgment;
Like: Return to Likeexpression, which is an expression of the same as judgment;
Gt: Returns Gtexpression, which is an expression larger than the judgment;
And: Returns Andexpression, which is the expression after the two expression and operation;
Or: Returns Orexpression, which is an expression after two expressions or operations;

For more expression please refer to the relevant documentation or source code.

Here are a few examples to illustrate the use of the criteria data load:

1. Get user name (username) as Billy's user object:

??? Expression ex = Expression.eq ("Username", "Billy");
??? IList users = Session. Createcriteria (typeof (User)). ADD (ex). List ();

2. Get User name (username) for Billy, user object with password 123456

??? Expression ex = Expression.and (Expression.eq ("Username", "Billy"),
??? Expression.eq ("Password", "123456"));
??? IList users = Session. Createcriteria (Type (User)). ADD (ex). List ();

3. Obtain the 第20-40 user object in the data.

??? IList users = Session. Createcriteria (typeof (User))
???????????????????????????? . Setfirstresult (20). Setmaxresults (40)
???????????????????????????? . List ();

For SQL Server, the data is positioned to roll forward to the Firstresult, and then maxresults the IDataReader record.

Insufficient load for criteria data:
1. When using Setfirstresult and setmaxresults to limit the number of objects returned, it is not possible to know the total number of objects;
2. The current version does not support associative queries (hibernate seems to be possible);




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.