Using HQL

In the previous chapter, you learned about a few of NHibernate's capabilities, such as lazy loading, Interceptors, logging, and Events. Most important, you learned how to install and configure NHibernate so that it can be used to retrieve data from a database. This chapter addresses using Hibernate Query Language (HQL), which is a fully object-oriented query language similar in appearance to SQL. This chapter covers the following topics:

  • CreateQuery, CreateMultiQuery, GetNamedQuery, DetachedQuery, DetachedNamedQuery, and Futures
  • Implementing paging
  • Creating a round-trip database counter


The IQuery API is used for executing HQL queries on a database. HQL is my personal preference when I don't have or need a specific class to store the results of a query. As shown later in this chapter, some NHibernate methods require using a strongly typed class to retrieve data from the database.

Many HQL methods are made available via the Session interface to execute a query against the database. Figure 2-1 shows a graphical representation of the methods, such as CreateQuery and CreateNamedQuery, and their corresponding execution methods, such as List<T>() or List(), for using the IQuery API. This chapter describes each of the methods and provides examples demonstrating how they can be used to fulfill most of the GuitarStore project requirements defined in Chapter 1.


Figure 2-2 shows the IQuery, AbstractQueryImpl, and QueryImpl class diagram. IQuery is ...

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.