R – Nhibernate – Getting a list

listnhibernate

I am trying to fetch a list from my database fulfilling a given criterion. The statement I am using is :
var products = session
.CreateCriteria(typeof(Product))
.Add(Restrictions.Eq("Category", category))
.List();

Where, product is my Domain object
session is the current active session.

Whenever I use this statement, NHibernate queries the database everytime to fetch me the list instead of doing it just the 1st time and then returning me the result from the cache from 2nd time onwards. Is there anything I am doing incorrect?

Best Answer

It has to hit the database, but only to retrieve the PK values in the query results.

Demonstration: set a breakpoint on this line, execute it once, then pause before it executes again. Modify the database directly to change one of the object's values, then run the line again. Check the results. The entities returned should not reflect the changes you made to the database (i.e., they came from the session cache).