Query entities by ID

Loading a specific identified entity from the database is a very common operation in the majority of NHibernate applications. Just querying for an entity by its Id or primary key may seem like the obvious thing to do, but as we'll see in this recipe, there's a bit more to it.

Getting ready

Complete the Getting Ready section at the beginning of this chapter.

How to do it…

  1. Add a new folder named QueryById to the project.
  2. Add a new class named Recipe to the folder, containing the following code:
    using NH4CookbookHelpers;
    using NH4CookbookHelpers.Queries.Model;
    using NHibernate;
    
    namespace QueryRecipes.QueryById
    {
        public class Recipe : QueryRecipe
        {
            protected override void Run(ISession session)
            {
                var product1 = session.Get<Product>(1);
    
                ShowNumberOfQueriesExecuted();
                ShowProduct(product1);
                ShowNumberOfQueriesExecuted();
    
                var product2 = session.Load<Product>(2);
    
                ShowProduct(product2);
                ShowNumberOfQueriesExecuted();
    
                var movie2 = session.Load<Movie>(2);
    
                ShowProduct(movie2);
                ShowNumberOfQueriesExecuted();
            }
        }
    }
  3. Run the application.
  4. Follow the instructions in the Query entities by ID recipe.

How it works…

NHibernate provides three ways to fetch an entity by its Id. The most obvious way is to simply use one of the query syntaxes to find an entity where the Id property is equal to a specified value. That method, however, should rarely be used, since is misses out on a lot of the functionality provided by NHibernate's caching mechanisms.

In this recipe, we will focus on the two methods Load and Get, which look similar but have very specific and different behaviors.

Session.Get

We retrieve our first entity using session.Get<Product>(1), where 1 is the Id of one of the Movie entities we have added to the database. On the next line we output the number of queries executed so far. As expected, one query has been executed:

SELECT
        product0_.Id as Id0_0_,
        product0_.Name as Name0_0_,
        product0_.Description as Descript4_0_0_,
        product0_.UnitPrice as UnitPrice0_0_,
        product0_.ISBN as ISBN0_0_,
        product0_.Author as Author0_0_,
        product0_.Director as Director0_0_,
        product0_.ProductType as ProductT2_0_0_ 
    FROM
        Product product0_ 
    WHERE
        product0_.Id=1

Session.Load

The next entity is retrieved used session.Load<Product>(2) and immediately we output the number of queries executed. Still only one? Yes, a call to Load will never trigger a query. Instead it returns a proxy object (in this case, ProductProxy) with only the value of the Id populated. The rest of the data isn't requested until we start accessing the other properties. In this case, that happens in the call to ShowProduct.

The session cache jumps in

We continue with session.Get<Movie>(2), which should have the same entity as the Product we just loaded. As we hoped, no extra query was executed. The session realizes that it has already loaded this entity and returns the same ProductProxy instance, as in the previous session.Load section.

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

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