I have a problem trying to load a tree, this is my cas开发者_运维知识库e, I have an entity associated with itself (Hierarchic) with n levels; the question is, Can I load eagerly the entire tree using ICriteria or HQL?
Thanks in advance for any help. Ariel
Yes... just set correct fetchmode.
i'll include example in a minute.
Example taken from here =>
IList cats = sess.CreateCriteria(typeof(Cat))
.Add( Expression.Like("Name", "Fritz%") )
.SetFetchMode("Mate", FetchMode.Eager)
.SetFetchMode("Kittens", FetchMode.Eager)
.List();
You can specify to eager load child of child too =>
.SetFetchMode("Kittens.BornOn", FetchMode.Eager)
In case you are using Linq to NHibernate, use Expand method =>
var feedItemQuery = from ad in session.Linq<FeedItem>().Expand("Ads")
where ad.Id == Id
select ad;
And i would recommend to use helper method that creates string from passed in lambda expression.
Quite likely that it's possible to tell Criteria to load whole tree. But i'm not aware about that and i prefer specifying what exactly i need (seems dangerous to load everything).
Does this helps?
精彩评论