﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Ayende Rahien commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>No, you should not.
  
Post your question along with mapping &amp; code to the nh users list
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment19</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment19</guid><pubDate>Tue, 15 Sep 2009 20:05:15 GMT</pubDate></item><item><title>Andrew Bullock commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Should i expect to see NH issuing extra selects when I enumerate the child collection of the leaf nodes in the tree? I'm observing this happening and wondering if im doing something wrong :s
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment18</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment18</guid><pubDate>Tue, 15 Sep 2009 13:16:51 GMT</pubDate></item><item><title>Matthew commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Could you turn off lazy loading and then retrieve the first employee. Nhibernate will then try and populate the children property and hence retrieve the full tree.
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment17</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment17</guid><pubDate>Fri, 04 Sep 2009 03:46:48 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Zihotki,
  
If you need to order the collection itself, you need an ordered collection. NHibernate doesn't allow to change sort order for collections on the fly without using filters.
  
  
For the leaf ones, you use left joins, yes.
  
I am not sure how you got the leafs to have a reference to themselves, please post the results to nh users
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment16</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment16</guid><pubDate>Wed, 02 Sep 2009 11:01:30 GMT</pubDate></item><item><title>zihotki commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Ayende,
  
This snippet doesn't work as well as I supposed. Yes, all tree will be loaded at once, but there is one "small" problem. During traversal through this tree (using several nesting cycles or recursion) NH will generate N additional queries, N equals to the count of leafs (employees who have no managed employees, parent without childs).
  
  
In order to make it working, I mean to remove these additional queries for leafs, the query should be changed to "select e from Employee e left join fetch e.ManagedEmployees". But using this query you'll get other errors - all leafs will be in the root of the result and each leaf will have reference to self (I added "public virtual Employee Manager {get; set;}")
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment15</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment15</guid><pubDate>Wed, 02 Sep 2009 01:06:20 GMT</pubDate></item><item><title>zihotki commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>This snippet is very cool. Thanks a lot, I have to rewrite some code in my project :). 
  
Is there any easy way to filter entire result's tree to contain only employees that have height (Employee table/entity contains Height column/property) more than 180cm ordering by height? E.g. only employees with height more than 180cm will be loaded from DB and the result tree will not contain any other employee. And employees in ManagedEmployees will be ordered by Height.
  
Neither "select e from Employee e join fetch e.ManagedEmployees where e.Height&gt;180 order by e.Height" nor "select e from Employee e join fetch e.ManagedEmployees as empl where e.Height&gt;180 and empl.Height &gt; 180 order by e.Height, empl.Height" are not working because "A fetch join does not usually need to assign an alias, because the associated objects should not be used in the where clause (or any other clause). Also, the associated objects are not returned directly in the query results. Instead, they may be accessed via the parent object.".
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment14</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment14</guid><pubDate>Tue, 01 Sep 2009 18:11:08 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Not with the 1.0 version, we will have those in the 2.0 one
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment13</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment13</guid><pubDate>Tue, 01 Sep 2009 09:12:07 GMT</pubDate></item><item><title>Nathan Palmer commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>That's great. Is there a corresponding way to do this using the Linq provider?
  
  
Nathan
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment12</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment12</guid><pubDate>Mon, 31 Aug 2009 13:37:31 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>ulu,
  
That presume that we can agree on the terms on the left side, though.
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment11</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment11</guid><pubDate>Sun, 30 Aug 2009 06:58:18 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Dmitriy,
  
The difference is that mine will also pre-load all the ManagedEmployees.
  
  
As for the difference in the DB, it is simple
  
  
create Employee(Id, ManagedBy, ...)
  
create OrganizationHierarchy(EmpId, ManagedBy, Level)
  
  
Now you have a way to refer, using standard set notation, to the entire hierarchy.
  
Think about it as denormalizing that
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment10</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment10</guid><pubDate>Sun, 30 Aug 2009 06:57:44 GMT</pubDate></item><item><title>ulu commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Just another thought. Your posts on mappings are fantastic, but sometimes a noob like me needs a simple table showing which language constructs can be mapped with which mapping elements, so that I don't have to read all mapping chapter and your posts to figure out what I need in this particular case. 
  
  
Sort of,
  
simple property =&gt; property
  
entity-valued property =&gt; one-to-many (or is it many-to-one?)
  
IDictionary =&gt; map
  
etc etc
  
  
Would be great if some of you NHibernate Masters could put such a table in a wiki.
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment9</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment9</guid><pubDate>Fri, 28 Aug 2009 14:32:21 GMT</pubDate></item><item><title>ulu commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>The whole idea of delayed posts is.. refreshing. I was faced with the task to load a tree yesterday, and I had a choice: wait till thi post appears, or go to sleep.
  
  
Seriously, could you please post the mapping as well?
  
  
Another question is, suppose I want only descendants from a single entity. Can't I just set the children to eager loading and fetch this entity?
  
  
Thanks a lot for your posts about NH. I'm just learning it, and the official docs are a bit hard to grasp.
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment8</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment8</guid><pubDate>Fri, 28 Aug 2009 12:24:55 GMT</pubDate></item><item><title>liviu commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>If only for this abstraction, i will use nhibernate ;)
  
this is excellent!
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment7</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment7</guid><pubDate>Fri, 28 Aug 2009 07:50:32 GMT</pubDate></item><item><title>Dmitriy Nagirnyak commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Oren,
  
  
What exactly do you mean saying "relation between a parent and all its children (indirect and direct) is preserved in the DB."?
  
  
It would be interesting to see how you use NHibernate to effectively load the whole tree (maybe not in a single query:) ) as I saw it has been asked many times in the NH User Groups.
  
  
Additionally what is the difference between your HQL (with distinct root transformer) and simply this?
  
"from Employee"
  
  
they seem to return equals lists (maybe except of order).
  
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment6</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment6</guid><pubDate>Fri, 28 Aug 2009 05:54:02 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Dmitriy,
  
You are correct, I haven't shown the calling code, which loaded all the employees by calling GetHierarchyRecursive.
  
The problem that you have here is that SQL has no good way of specifying hierarchies. Therefor, you would have to write DB specific tree code, and feed that to NH if you wanted to 
  
load an entire tree in a single shot using N level parent.
  
A much easier task is to change the hierarchy so the relation between a parent and all its children (indirect and direct) is preserved in the DB.
  
That is a MUCH better proposition than doing hierarchical queries
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment5</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment5</guid><pubDate>Fri, 28 Aug 2009 04:24:12 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>Rob,
  
DistinctRootEntityResultTransformer will filter the output in memory so the list of entities that you get will have no duplicates.
  
There are going to be duplicates in the result set if you don't do that because there is a join i the query
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment4</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment4</guid><pubDate>Fri, 28 Aug 2009 04:17:53 GMT</pubDate></item><item><title>Dmitriy Nagirnyak commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>The original code (with all its cons) retrieves the hierarchy for a given root only.
  
The HQL example you provided retrieves ALL the hierarchies with all the roots.
  
  
This seems to be totally different scenario comparing to the original code.
  
Right?
  
  
Also how would you efficiently retrieve the tree hierarchy starting from a specific root?
  
  
Cheers.
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment3</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment3</guid><pubDate>Fri, 28 Aug 2009 04:08:13 GMT</pubDate></item><item><title>Jaime commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>This may answer your question:
  
[nhforge.org/.../...esults-from-joined-queries.aspx](http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries.aspx)</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment2</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment2</guid><pubDate>Fri, 28 Aug 2009 03:18:12 GMT</pubDate></item><item><title>Rob commented on NHibernate tips &amp; tricks: Efficiently selecting a tree</title><description>What is the significance of DistinctRootEntityResultTransformer?  Why do I need that?
</description><link>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment1</link><guid>http://ayende.com/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree#comment1</guid><pubDate>Fri, 28 Aug 2009 02:51:56 GMT</pubDate></item></channel></rss>