我遇到了带有JPQL的子查询问题,Criteria也出现了同样的错误。查询返回语法错误:
Encountered "," javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement.
JPQL
StringBuilder query = new StringBuilder("SELECT d FROM Document d WHERE NOT EXISTS (SELECT ds FROM Documents ds WHERE d = ds.key.document)");
TypedQuery<Documento> tQuery = entityManager.createQuery(query.toString());
List<Document> documents = tQuery.getResultList();
标准
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Document> query = cb.createQuery(Document.class);
Root<Document> root = query.from(Document.class);
CriteriaQuery<Document> select = query.select(root);
Subquery<Documents> sQuery = query.subquery(Documents.class);
Root<Documents> 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<Documento> tQuery = entityManager.createQuery(query);
List<Document> documents = tQuery.getResultList();
答案 0 :(得分:1)
查询似乎是正确的。但是@RequestMapping(value = "/api/" + REST_API_VERSION + "/" + ONE_INTERFACE, method = RequestMethod.GET, produces ={MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public String pmmSearch() { ... }
这个词在德比中是一个保留词。那应该是原因。因此,您最好在KEY
实体中重命名此属性。
答案 1 :(得分:0)
我不知道它是否是一个错误,或者应该是这样,但是我在子查询的列中指定了一个复合键,它是受影响的键,它可以工作。这种情况只发生在其他选择的子查询中我没有遇到过这个错误
变化:
<强> JPQL 强>
StringBuilder query = new StringBuilder("SELECT d FROM Document d WHERE NOT EXISTS (SELECT ds.key.document FROM Documents ds WHERE ds.key.document = d)");
<强>标准强>
...
Subquery<Documents> sQuery = query.subquery(Documents.class);
Root<Documents> rootSubquery = sQuery.from(Documents.class);
Predicate p = cb.equal(rootSubquery.get("key").get("document"), root);
sQuery.select(rootSubquery.get("key").get("document"));
sQuery.where(p);
select.where(cb.not(cb.exists(sQuery)));
...