Using ICriteria

In the previous chapter you learned how the Hibernate Query Language (HQL) works and how to implement most of the APIs that make up the library. Most important, you learned how to combine queries so that fewer round-trips can be made to the database, thereby improving the program's performance. This chapter covers using ICriteria, which is a more method-based query API versus the SQL-like queries of IQuery. In this chapter, you learn about the following:

  • CreateCriteria, CreateMultiCriteria, DetachedCriteria, QueryOver, and Futures
  • Implementing paging
  • Lambda expressions
  • FetchMode, Restrictions, and data transfer objects (DTOs)


The ICriteria interface is a feature-rich, method-based query API. When I first started using NHibernate, I chose to use ICriteria. As a programmer, the implementation of ICritera seemed more programmatic than HQL — meaning that instead of using dot (.) notation in a string, ICriteria provides methods, classes, and properties for creating the query. A large number of ICriteria methods are accessible via the Session to execute a query against the database, and pulling the methods and properties together seems almost intuitive.

Figure 3-1 provides a graphical representation of the methods and their corresponding execution methods for using the ICriteria interface, which implements the same functionality described in the previous chapter for IQuery. This chapter describes these methods and provides examples demonstrating how ...

Get Working with NHibernate 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.