I am having a problem getting associated entities to be cached unless they are specified (Not.LazyLoad() in mapping). If I build up a query in Criteria or Linq the Main Entity is cached, but not associated entities.
IList<NewsItem> news;
using (var session = factory.OpenSession())
{
Console.WriteLine("First Query");
news = session.CreateCriteria(typeof(NewsItem))
.SetCacheable(true)
.SetFetchMode("Author", FetchMode.Eager) // associated entity eager loaded
.List<NewsItem>();
}
foreach (var item in news)
{
Console.WriteLine("Author: " + item.Author.Name); //works fine first time
}
using (var session = factory.OpenSession())
{
Console.WriteLine("");
Console.WriteLine("Second Query");
news = session.CreateCriteria(typeof(NewsItem))
.SetCacheable(true)
.SetFetchMode("Author", FetchMode.Eager)
.List<NewsItem>();
}
foreach (var item in news)
{
Console.WriteLine("Author: " + item.Author.Name); //NHibernate.LazyInitializationException
}
I want to avoid having to eager load the associations via the mapping files. Anyone else having a similar problem.
Any feedback appreciated.