title: JPA / Hibernate: CriteriaBuilder - How to create query using relationship object?
body: <p>I have the following four tables:</p>
  
  <p><strong>SCHEDULE_REQUEST TABLE:</strong>
  ID, 
  APPLICATION_ID (FK)</p>
  
  <p><strong>APPLICATION TABLE:</strong>
  ID, 
  CODE</p>
  
  <p><strong>USER_APPLICATION TABLE:</strong>
  APPLICATION_ID (FK), 
  USER_ID (FK)</p>
  
  <p><strong>USER TABLE:</strong>
  ID, 
  NAME</p>
  
  <p>Now I wanted to create a <code>CriteriaBuilder</code> where condition is to select <code>ScheduleRequests</code> for specified user Ids. </p>
  
  <p>I have the following codes:</p>
  
  <pre><code>List&lt;User&gt; usersList = getSelectedUsers(); // userList contains users I wanted to select
  
  CriteriaBuilder builder = getJpaTemplate().getEntityManagerFactory().getCriteriaBuilder();
  CriteriaQuery&lt;ScheduleRequest&gt; criteria = builder.createQuery(ScheduleRequest.class);
  Root&lt;ScheduleRequest&gt; scheduleRequest = criteria.from(ScheduleRequest.class);
  criteria = criteria.select(scheduleRequest);
  
  ParameterExpression&lt;User&gt; usersIdsParam = null;
  if (usersList != null) {
      usersIdsParam = builder.parameter(User.class);
      params.add(builder.equal(scheduleRequest.get("application.userApplications.user"), usersIdsParam));
  }
  
  criteria = criteria.where(params.toArray(new Predicate[0]));
  
  TypedQuery&lt;ScheduleRequest&gt; query = getJpaTemplate().getEntityManagerFactory().createEntityManager().createQuery(criteria);
  
  // Compile Time Error here:
  // The method setParameter(Parameter&lt;T&gt;, T) in the type TypedQuery&lt;ScheduleRequest&gt; is not 
  // applicable for the arguments (ParameterExpression&lt;User&gt;, List&lt;User&gt;)
  query.setParameter(usersIdsParam, usersList);
  
  return query.getResultList();
  </code></pre>
  
  <p>Can you please help me how to pass query filter to a relationship object?
  I think what I did in <em>"application.userApplications.user"</em> is wrong?
  Please really need help.</p>
  
  <p>Thank you in advance!</p>