title: Hibernate Criteria - Multiple joins on the same table
body: <p>I'm attempting to recreate the following piece of SQL in hibernate criteria.</p>
  
  <pre><code>select id from A join B b1 on b1.a_id = a.id and b1.name = "Some name"
                   join B b2 on b2.a_id = a.id and b2.name = "Some other name"
                   ...join B bN on bN.a_id = a.id and bN.name = "...."
  </code></pre>
  
  <p>The goal of which is to find only ids from A where it has all of the sought after names in table B.</p>
  
  <p>I tried to use multiple aliases, but it complains about a duplicate association path on the collection.</p>
  
  <p>I'm using criteria because I need this to be extensible out to N (where N is some small integer) and I've enjoyed using it in the past but I'm just completely stuck on how to get this to behave. Everything I've tried generates a single join - which doesn't produce proper results. </p>
  
  <p>I'm also open to other suggestions of how to get this to work.. but if I can get away with criteria that'd be helpful.</p>
  
  <p>I ended up solving this problem through the use of detached criteria and subqueries. It's not extremely efficient, but it is very very flexible.</p>
  
  <p>The problem was that I had to find ids from A where some subset of its related tables B C D's had field values equal to some contains search list. The solution does 1 query for every new point where there would have been a join. The end result looks something like</p>
  
  <pre><code>Select id from A where A.id in (Select id from B where B.val in (1,2,3)) or A.id in (Select id from C where c.val in (1,2,3,4))...
  </code></pre>