title: JPA subquery syntax error
body: <p>I have the problem with a subquery with JPQL, same error occurs with Criteria. The query return a syntax error:</p>
  
  <pre><code>Encountered "," javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement.
  </code></pre>
  
  <p><strong>JPQL</strong></p>
  
  <pre><code>StringBuilder query = new StringBuilder("SELECT d FROM Document d WHERE NOT EXISTS (SELECT ds FROM Documents ds WHERE d = ds.key.document)"); 
  TypedQuery&lt;Documento&gt; tQuery = entityManager.createQuery(query.toString());
  List&lt;Document&gt; documents = tQuery.getResultList();
  </code></pre>
  
  <p><strong>Criteria</strong></p>
  
  <pre><code>    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
      CriteriaQuery&lt;Document&gt; query = cb.createQuery(Document.class);
  
      Root&lt;Document&gt; root = query.from(Document.class);
      CriteriaQuery&lt;Document&gt; select = query.select(root);
  
      Subquery&lt;Documents&gt; sQuery = query.subquery(Documents.class);
      Root&lt;Documents&gt; rootSubquery = sQuery.from(Documents.class);
      Predicate p = cb.equal(rootSubquery.get("key").get("document"), root);
      sQuery.select(rootSubquery);
      sQuery.where(p);
      select.where(cb.not(cb.exists(sQuery)));
      TypedQuery&lt;Documento&gt; tQuery = entityManager.createQuery(query);
      List&lt;Document&gt; documents = tQuery.getResultList();
  </code></pre>