开发者

Eager loading a tree in NHibernate

开发者 https://www.devze.com 2022-12-19 03:20 出处:网络
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

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号