Retrieving entities by identifiers

If you recall the unit tests that we wrote in the previous chapters, we commonly used a method named Get<T> on ISession to retrieve a persistent instance of an entity by its id. Following is relevant part of that code:

using (var transaction = session.BeginTransaction())
{
  var employee = session.Get<Employee>(id);
  transaction.Commit();
}

You might be wondering what the reason behind providing this method is. All we are doing is querying an entity by its primary key. Why not use criteria query, QueryOver, or LINQ to query entities by primary key? Well, you can use those methods when you are not querying by identifier, but if you are querying by identifier then Get<T> has some optimizations built in that make it ...

Get Learning NHibernate 4 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.