title: How to construct a hierarchical query in hibernate?
body: <p>Hello I have the following data model (simplified):</p>
  
  <pre><code>User
     Long getDelegationId();
  
  Expedient
     Long getDelegationId();
  
  Delegation
      Long getId();
      Set&lt;Delegation&gt; getSubDelegations();
      Delegation getParentDelegation();
  </code></pre>
  
  <p>Now I need to create a filter so a User can only see the expedients that belong to his delegation or its subdelegations at 3 lvl depth maximum.</p>
  
  <p>How can I achieve this?</p>
  
  <p>Im trying the following query:</p>
  
  <pre><code>from Expedient aliasExpedient where aliasExpedient.delegation.ID in 
  ( select delegation4.ID from Delegation as delegation1
  join fetch delegation1.delegations as delegation2
  join fetch delegation2.delegations as delegation3
  join fetch delegation3.delegations as delegation4
  where delegation1.ID = ?)
  </code></pre>
  
  <p>With the fetch included I get:</p>
  
  <pre><code>org.hibernate.QueryException: fetch not allowed in subquery from-elements
  </code></pre>
  
  <p>and without it I get no results.</p>
  
  <p>Any hints about how to achieve my goal?</p>
  
  <p>Thanks,
  Gonzalo</p>