nhibernate fetching entire tree
I need help fetching tree hierarchy from db using nhibernate 3.0
QueryOver.Of<Enterprise>(() => entAlias)
.JoinAlias(() => entAlias.ChildEntities, () => childEntityAllias, JoinType.LeftOuterJoin)
.TransformUsing(new Di开发者_JAVA百科stinctRootEntityResultTransformer())
I am getting only the two level of the graph (parent and its childrens) but not the childrens of children etc.
The same is if I try to fetch parents of the leaf. I get only the parent of the leaf, but not parent of the parent of the parent... of the leaf. where level = n.
Ho to do this type of query. no mather if Icriteria, linq, HQL or else.
You could use batch-size to fetch the children more efficiently.
<bag name="Childen" batch-size="20" ...>
Children are still loaded by separate queries (you shouldn't join them in the query anymore), but always 20 at once. To turns "N+1" to "N/20+1" which usually performs very good. The advantage of this solution is that you don't need to care about in your queries.
You could also load parents in batches:
<class name="Enterprise" batch-size="20">
It loads many-to-one relations to Enterprises in batches if possible.
If you need even more optimization, consider to add a reference to the root (the top most parent). Then you can load all the children of a root in one easy query. The disadvantage is that you need to care about this reference, it is a redundancy which could also be hard to maintain.
If you really want to fetch everything (although I'm not sure why you would do such a thing) then disable lazy loading (thus enabling eager loading) in NHibernate.
精彩评论