title: HQL Query on Set Property
body: <p>Suppose that I have the following HBM mapping:</p>
  
  <pre><code>&lt;class name="Student" table="student"&gt;
      &lt;set name="classes" table="student_classes" cascade="none"&gt;
          &lt;key column="studentId" /&gt;
          &lt;many-to-many column="classId" class="org.myCompany.myClass" /&gt;
      &lt;/set&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>In my student POJO, I have the following:</p>
  
  <pre><code>private Set&lt;myClass&gt; classes = new HashSet&lt;myClass&gt;();
  public Set&lt;myClass&gt; getClasses() { return classes; }
  public void setClasses(Set&lt;myClass&gt; classes) { this.classes = classes; }
  </code></pre>
  
  <p>I want to run the following HQL query:</p>
  
  <pre><code>select count(*) from Student where classes.className = :myClassName
  </code></pre>
  
  <p>However, hibernate throws the following exception:</p>
  
  <pre><code>ERROR [service-j2ee-4] PARSER.reportError(33) |  Invalid path: 'classes.className'
  ERROR [service-j2ee-4] PARSER.reportError(33) | &lt;AST&gt;:0:0: unexpected end of subtree
  ERROR [service-j2ee-4] PARSER.reportError(33) |  left-hand operand of a binary operator was null
  org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'classes.className' [select count(*) from Student where classes.className = :myClassName  and  1=1]
  </code></pre>
  
  <p>Is it possible to run a hibernate query which returns results based on properties of a Set? In the above example, we might want to query for all students who are taking 'Algebra I' or another course?</p>
  
  <p><strong>Edit:</strong> I enabled the appropriate debug mode to get Hibernate to output it's actual SQL query and here is the query that it generates:</p>
  
  <pre><code>select count(*) as col_0_0_ 
  from student student0_, student_classes student1_, classes student2_
  
  where student0_.studentId=student1_.studentId and student1_.classId=student2_.classId and student2_.className LIKE 'algebra' and 1=1;
  </code></pre>