title: Java hibernate Detached criteria, count / having, query
body: <p>can someone help me out with a query ?</p>
  
  <p>Here´s the deal:</p>
  
  <p>I have two tables</p>
  
  <p>1- Group of users</p>
  
  <p>2- Users</p>
  
  <p>One group has a lot of users, but the thing is, the table groups holds the number of users it has on table users. But it happens that sometimes this number is invalid, I want to find the casees where the number in the table group is less then the users in the table users.</p>
  
  <p>The SQL query would be like that:</p>
  
  <pre><code>select
   id_group,
   count(user)
  from
   user inner join user
  having 
   count(user) &lt; group.number_of_users
  </code></pre>
  
  <p>In hibernate I cant do that, so far I got into this</p>
  
  <pre><code>DetachedCriteria dc = DetachedCriteria.forClass(Group.class);
  dc.createAlias("userCollection", "uc");
  dc.setProjection(Projections.count("uc.idUser"));
  dc.add(Restrictions.lt("????????", "??????????");
  </code></pre>
  
  <p>Thanks in advance</p>