title: Is JPA2 CriteriaQuery with parameter thread safe to create query from?
body: <p>I want to have reusable <code>CriteriaQuery</code> similar to this:</p>
  
  <pre><code>    CriteriaBuilder b = cb();
      CriteriaQuery&lt;MyClass&gt; query = b.createQuery(MyClass.class);
      Root&lt;MyClass&gt; myClass= query.from(MyClass.class);
      where.add(b.equal(applicant.get(MyClass_.id),          b.parameter(Integer.class, "id")));
      query.where(where.toArray(new Predicate[where.size()]));
      return query;
  </code></pre>
  
  <p>Now in worker threads pool I want to perform following operation:</p>
  
  <pre><code>TypedQuery&lt;MyClass &gt; query = em.createQuery(myClassCriteria);
  query.setParameter("id", id);
  MyClass a = query.getSingleResult();
  em.detach(a);
  return a;
  </code></pre>
  
  <p>where <code>myClassCriteria</code> is <code>CriteriaQuery</code> from above. </p>
  
  <p>is that a thread safe apporach? I suspect, that setting parameter on <code>query</code> can internally modify given <code>CriteriaQuery</code> making it non-tread-safe.</p>
  
  <p>My suspicions came from the situation that occasionally app threw some SQL error saying that <code>parameter 2 is not set</code>. I dont even know what parameter 2 here is as I have only 1 paremeter in query, neverheless this makes me thing that resusing parametrized query in such way can be non-thread safe. </p>
  
  <p>Error are rahter not reproducable on demand, so It is hard to confirm the cause. </p>
  
  <p>My JPA provider is Hiberate</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
      &lt;version&gt;5.2.1.Final&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>