Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate 3 Linq query caching

I've just started using LINQ with NHibernate in NHibernate 3, after previously using ICriteria.

Here's an example query:

ISession session = NHibernateSessionManager.Instance.GetSession();

var results = from project in session.Query<Project>()
              where project.ProjectState == ProjectState.Archive
              orderby project.ProjectNumber
              select project;

return results.ToList();

How do I set that to cache? I've had a look around and other questions seem to use a different (perhaps outdated?) syntax, or perhaps I'm doing it wrong...

like image 563
RodH257 Avatar asked Jan 27 '11 06:01

RodH257


People also ask

How do I clear NHibernate cache?

NHibernate will evict associated entities automatically if the association is mapped with cascade="all" or cascade="all-delete-orphan". The ISession also provides a Contains() method to determine if an instance belongs to the session cache. To completely evict all objects from the session cache, call ISession. Clear().

What is fetch in NHibernate?

A fetching strategy is the strategy NHibernate will use for retrieving associated objects if the application needs to navigate the association. Fetch strategies may be declared in the O/R mapping metadata, or overridden by a particular HQL or Criteria query.

What is QueryOver?

QueryOver is a strongly-typed version of Criteria, and is more NHibernate specific. Pretty much anything you can do in ICriteria can be done with QueryOver.

What is NHibernate session?

The NHibernate session encapsulates a unit of work as specified by the unit of work pattern.


1 Answers

Use the Cacheable() extension method on your Queryable before calling ToList().

like image 152
Diego Mijelshon Avatar answered Sep 22 '22 15:09

Diego Mijelshon