title: HQL with a collection in the WHERE clause
body: <p>I've been trying for the this whole a query who is officially giving me nightmares. The system is a user and contact management. So I have <code>UserAccount</code>, <code>Contact</code> and <code>Phone</code>.  </p>
  
  <p><code>UserAccount</code> has a bidirectional one-to-many relationship with <code>Contact</code> and an unidirectional one on phone all mapped by a <code>Set</code>:</p>
  
  <pre><code>//UserAccount mapping 
  @OneToMany(targetEntity=PhoneImpl.class, cascade= {CascadeType.ALL})
  @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  private Set&lt;Phone&gt; phones = new HashSet&lt;Phone&gt;();
  
  @OneToMany(targetEntity=ContactImpl.class, cascade={CascadeType.ALL}, mappedBy="userAccount")
  @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  private Set&lt;Contact&gt; contacts = new HashSet&lt;Contact&gt;();
  </code></pre>
  
  <p>Contact now has a one-to-many unidirectional with phones</p>
  
  <pre><code>@OneToMany(targetEntity=PhoneImpl.class, cascade={CascadeType.ALL})
  private Set&lt;Phone&gt; phones = new HashSet&lt;Phone&gt;();
  </code></pre>
  
  <p>I'm writing a method to check the existence of the same number for the same contact of a particular user by the email unique field.  </p>
  
  <p>I know I could have overridden the <code>equals</code> and <code>hashcode</code> for that but since phone in a entity mapped by set I don't know at this moment how to do that. So I wanted to provide a method to rather check for that uniqueness for me before each entry on the contact page  </p>
  
  <pre><code>public boolean checkForExistingPhone(String userEmail, String formatedNumber) {
      List&lt;Contact&gt; result = null;
      Session sess = getDBSession().getSession();
  
      String query = "select Contact ,cphones.formatedNumber from Contact c inner join    Contact.phones cphones where c.UserAccount.email = :email and cphones.formatedNumber= :number";
  //        try {
          result = (List&lt;Contact&gt;) sess.createQuery(query)
                  .setParameter("email", userEmail)
                  .setParameter("number", formatedNumber).list();
  //        } catch (HibernateException hibernateException) {
  //            logger.error("Error while fetching contacts of email " + userEmail + " Details:"     + hibernateException.getMessage());
  //        }
          if(result == null)
              return false;
          else
              return true;
  }
  </code></pre>
  
  <p>I keep on having this error:</p>
  
  <pre><code>org.hibernate.hql.ast.QuerySyntaxException: Contact is not mapped [select
  cphones.formatedNumber from Contact c inner join Contact.phones cphones where
  c.UserAccount.email = :email and cphones.formatedNumber= :number]. 
  </code></pre>
  
  <p>I can't really  figure out what happens and first i don't know how to treat collections in HSQ.thanks for reading </p>