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 a preferred choice over any other querying method. Even other querying methods will use Get<T>
internally if they need to load an entity by identifier.
Get<T>
will first check whether an entity is present in the identity...