We use NHibernate 4 in our asp.net mvc 4 (.net 4) application. As far as I can tell the behaviour of NHibernate 4 has changed a bit when it comes to second level caching.
The following behaviour seems to have changed (please correct me if I am wrong):
- There is no longer a transaction needed when using second level cache
- When I execute a query like (Hibsession.Query().Where(x => x.Name == "x").ToList()), then it will query for the entiry entity. In earlier versions of NHibernate - if i remeber correctly - only the id's of the entity would have been retrieved.
It seems to me that second level only applies in the following cases:
using (var hibSession = SessionFactory.OpenSession())
{
// Second level cache working
var entity = hibSession.Get<ChachedEntity>(7); // second level cache working
var parent = entity.ParentElement; // second level cache working because n:1
// Probably working (not tested)
var elements = hibSession.Query<ChachedEntity>().Cacheable().Take(30).ToList(); // guessed behaviour: query-cache selects id's and then then uses second level cache
// second level cache NOT Working
var children = entity.ChildCollectionWithCachableEntities; // second level cache NOT working because 1:n (!!)
}
My questions are now:
- Where is the behaviour of the NHibernate 4 second level cache described (or at least changes to version 3 documented)
- Is it possible to use the second level cache for lazy loading child elements? (or at least ensure that only the id's are loaded and then have the second level cache materialize the entities)
Thanks in advance