title: How can I use Hibernate Criteria's to query nested tables?
body: <p>I've looked all over SO and Google but I guess I'm not using the right search terms or something.</p>
  
  <p>Anyway, say I have three tables:</p>
  
  <pre><code>Companies
  -----------------------------------------
  id
  name
  user_id
  
  Users
  -----------------------------------------
  id
  username
  usertype_id
  
  UserTypes
  -----------------------------------------
  id
  typeofuser
  </code></pre>
  
  <p>So <code>ACME</code> would be a company, it would have a user <code>Moe</code> and Moe would be a usertype of <code>Stooge</code>.</p>
  
  <p>In SQL, I would do something like:</p>
  
  <pre><code>select
      *
  from companies c
      join users u on (u.id = c.user_id)
      join usertypes ut on (ut.id = u.usertype_id)
  where
      ut.typeofuser = 'Stooge'
  </code></pre>
  
  <p>But I can't seem to figure out how to do that in a Criteria.  I have tried:</p>
  
  <pre><code>Criteria crit = io.getSession().createCriteria(Company.class);
  List&lt;Company&gt; list = 
      crit.createCriteria("users")
          .createCriteria("usertypes")
              .add(Restriction.eq("typeofuser", "Stooge").list();
  </code></pre>
  
  <p>But I get back way too many records. And the results don't even come close to being accurate.</p>
  
  <p>I've also tried:</p>
  
  <pre><code>Criteria crit = io.getSession().createCriteria(Company.class);
  List&lt;Company&gt; list = 
      crit.createAlias("users", "u")
          .createAlias("u.usertypes", "ut")
              .add(Restriction.eq("ut.typeofuser", "Stooge").list();
  </code></pre>
  
  <p>Seems to bring back the exact same result set.  I actually have read the user manual.  And when I nest only one level deep (ie, searching by users is fine) but when I get two layers deep, I can't quite get it.  And the manual is no help.  I just can't relate cats and kittens to business objects.  Maybe they should use cats, kittens and fleas?  :-/</p>
  
  <p>Thanks for any suggestions.</p>