title: How to set a Collection/List to a named parameter of a JPA criteria query?
body: <p>A single named parameter can be set to a JPA criteria query something like the following. The parameter is of the type <code>Long</code> in this case.</p>
  
  <pre><code>public StateTable find(Long id)
  {
      CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
      CriteriaQuery&lt;StateTable&gt; criteriaQuery = criteriaBuilder.createQuery(StateTable.class);
      Metamodel metamodel=entityManager.getMetamodel();
      EntityType&lt;StateTable&gt; entityType = metamodel.entity(StateTable.class);
      Root&lt;StateTable&gt; root = criteriaQuery.from(entityType);
  
      ParameterExpression&lt;Long&gt; parameterExpression=criteriaBuilder.parameter(Long.class);
      criteriaQuery.where(criteriaBuilder.equal(root.get(StateTable_.stateId), parameterExpression));
  
      TypedQuery&lt;StateTable&gt; typedQuery = entityManager.createQuery(criteriaQuery);
      return typedQuery.setParameter(parameterExpression, id).getSingleResult();
  }
  </code></pre>
  
  <p>This query inside the method returns a single object of the <code>StateTable</code> (just say state) entity which I'm dealing with and corresponds to the following JPQL query.</p>
  
  <pre><code>entityManager.createQuery("select s from StateTable s where s.stateId=:id")
               .setParameter("id", id)
               .getSingleResult();
  </code></pre>
  
  <hr>
  
  <p>I need to find more than one row that corresponds to a list of ids supplied via <code>java.util.List&lt;Long&gt;</code>. The following is the incomplete version of the criteria query.</p>
  
  <pre><code>public List&lt;StateTable&gt; find(List&lt;Long&gt; ids)
  {
      CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
      CriteriaQuery&lt;StateTable&gt; criteriaQuery=criteriaBuilder.createQuery(StateTable.class);
      Metamodel metamodel=entityManager.getMetamodel();
      EntityType&lt;StateTable&gt; entityType = metamodel.entity(StateTable.class);
      Root&lt;StateTable&gt; root = criteriaQuery.from(entityType);
  
      ParameterExpression&lt;Long&gt; parameterExpression = criteriaBuilder.parameter(Long.class);
      criteriaQuery.where(criteriaBuilder.in(root.get(StateTable_.stateId)).value(parameterExpression));
  
      TypedQuery&lt;StateTable&gt; typedQuery = entityManager.createQuery(criteriaQuery);
      return typedQuery.setParameter(parameterExpression, 1L).getResultList();
  }
  </code></pre>
  
  <p>It uses an <code>in()</code> query but I made it return only a single row, since I don't know whether it is possible to set a list of ids to <code>ParameterExpression</code> or not.</p>
  
  <p>In short, this criteria query should correspond to the following JPQL query.</p>
  
  <pre><code>entityManager.createQuery("from StateTable where stateId in(:id)")
               .setParameter("id", ids)
               .getResultList();
  </code></pre>
  
  <p>Is there a way to set a <code>List&lt;Long&gt;</code> to <code>ParameterExpression</code> as specified?</p>