Chapter 6. Let's Retrieve Some Data from the Database

So far we have covered the most important building blocks of NHibernate. This chapter will cover the last important building block – retrieving data. I find this topic most interesting for two reasons. One, of the total time you spend on working with NHibernate, you will spend about 90 percent time writing code to retrieve data and 10 percent on working with other parts of NHibernate. Second, you are spoilt for choices when it comes to retrieving data. I know that I have been saying the same for most features of NHibernate. But with data retrieval we really are offered number of different options. On top of that, NHibernate offers features such as lazy loading, polymorphic queries, implicit joins, fetching strategies, and so on, which makes data retrieval a very interesting concept to learn. I do not want to throw jargon at you and confuse you more. We are going to learn all these features in this chapter. Let me give you a brief overview of what we are going to learn.

We will begin with a short discussion of something I call querying workflow in which we will talk about how NHibernate queries work. We will then take a quick tour of different querying methods available in NHibernate. At this point, you should be able to write simple queries involving a single entity. Next, we will look at how to join multiple entities in a query. We will then spend some time talking about things such as ordering, pagination, and aggregation. Discussion of concepts such as lazy loading, polymorphic queries, and fetching strategies would follow at appropriate times. Before we actually dive into the details, let's take a moment to recall the code from the previous chapters that we have used to load entities from database:

using (var transaction = session.BeginTransaction())
{
  var benefit = Session.Get<Benefit>(id);
  Assert.That(benefit.Name, Is.EqualTo("Benefit 1"));
  transaction.Commit();
}

So far we have used the ISession.Get<T> method to load an entity from database using the identifier value. Towards the end of the chapter we will see how powerful this method is. But you cannot use it much because the queries you will end up writing most of the times would not be based on identifiers of the entities but some other business criteria. That is why NHibernate offers different querying mechanisms.

Querying the workflow

NHibernate supports different ways of retrieving data from database. The basic semantics of writing a NHibernate query is no different than that of a standard SQL query which consists of the following three main parts:

  • You specify which tables are involved in query
  • You specify an optional filter using WHERE statement
  • You specify which columns you want to return using SELECT statement

There are other optional things you can add to a SQL query such as ordering, grouping, pagination, and so on, which is also supported by NHibernate. Queries written in NHibernate fundamentally differ from standard SQL queries on one aspect – composability. A query is represented by a query object. You can compose a query object by incrementally adding constructs such as joins with more tables, ordering, the where clauses, and so on. When you have finished building the query object, you call one of the terminal method on it, such as List<T> or First<T> which will tell NHibernate to go and build SQL from the query object, execute it, and return the results in specified type. Following image may make it easier to understand how this works:

Querying the workflow

Note that the different ISession methods referred in the preceding workflow refer to different querying mechanisms available in NHibernate which we are going to look into next. No matter which querying method you use, the workflow remains more or less the same.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset