views:

40

answers:

1

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?

+1  A: 

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).

Jon Seigel
To follow my demonstration, you have to _modify_ an existing record. A record that got added would not be in the cache after the first query.
Jon Seigel
Thanks for the quick response!
Saurabh Lalwani
Do you know the reason why does it have to retrieve the PK values from the Database?Also, do you know any good link for WPF + MVVM + NHibernate together?Thanks!
Saurabh Lalwani
For a query, NHibernate has to query something, so it uses the database. As I understand it, it then grabs the PK values from the query results; if an entity that has the PK value is in the cache, the cached entity is returned, otherwise the entity is queried from the database. I have no experience with WPF, sorry.
Jon Seigel