title: Hibernate criteria filter by related entity
body: <p>I have an entity which has a collection of related entities. </p>
  
  <pre><code>public class Student{
  
  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinColumn(name = "COURSE_STUDENT_ID" , insertable=false,updatable=false)
  private Set &lt;Course&gt; courses;
  </code></pre>
  
  <p>I want to filter students by course names and student class id. For now I have worked it out how to filter by class id but I have no idea how to filter by courseId given that Student entity has a set of courses and the tables are related. I have read some articles but no code matches the one I have already.</p>
  
  <pre><code>CriteriaBuilder criteriaBuilder = persistenceStore.createCriteriaBuilder();
  CriteriaQuery&lt;Object&gt; criteria = criteriaBuilder.createQuery();
  Root&lt;Student&gt; root = criteria.from(Student.class);
  List&lt;Predicate&gt; params = new ArrayList&lt;Predicate&gt;();
  
  params.add(criteriaBuilder.equal(root.get("classId"),classId));
  
  Predicate[] predicates = new Predicate[params.size()];
  params.toArray(predicates);
  criteria.select(root);
  criteria.where(criteriaBuilder.and(predicates));
  Query query = persistenceStore.createQuery(criteria);
  List&lt;Student&gt; resultList = query.getResultList();
  </code></pre>