Unwanted tree loading in NHibernate

I have a problem loading the tree, this is my case, I have an object associated with itself (hierarchical) with n levels; the question is, can I look forward to loading the whole tree using ICriteria or HQL?

Thanks in advance for any help. Ariel

+4
source share
1 answer

Yes ... just set the correct fetchmode.


I will turn on the example in a minute.


Example: 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 whether you want to load child elements of the child too =>

 .SetFetchMode("Kittens.BornOn", FetchMode.Eager) 

If you are using Linq for NHibernate, use the Expand => method

 var feedItemQuery = from ad in session.Linq<FeedItem>().Expand("Ads") where ad.Id == Id select ad; 

And I would recommend using a helper method that creates a string from the lambda passed in the expression.


It is likely that you can specify Criteria for loading the entire tree. But I don’t know about it, and I prefer to indicate what I really need (it seems dangerous to download everything).


Does it help?

+1
source

Source: https://habr.com/ru/post/1299820/


All Articles