JPA EclipseLink支持子查询选择? ClassCastException异常

时间:2016-08-20 15:24:30

标签: jpa subquery eclipselink selection

使用Eclipse 2.6.3,选择子查询会发出以下错误。 它在选择中是否支持子查询?

CriteriaQuery query = criteriaBuilder.createQuery(String.class);
Root root = query.from(Employee.class);
Subquery subquery = query.subquery(String.class);
Root subqueryRoot = subquery.collate(root);
subquery.select(subqueryRoot.get("name"));
query.select(subquery);

java.lang.ClassCastException: org.eclipse.persistence.internal.jpa.querydef.SubQueryImpl cannot be cast to org.eclipse.persistence.internal.jpa.querydef.SelectionImpl
        at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryImpl.createCompoundQuery(CriteriaQueryImpl.java:511)
        at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryImpl.getDatabaseQuery(CriteriaQueryImpl.java:447)
        at org.eclipse.persistence.internal.jpa.querydef.CommonAbstractCriteriaImpl.translate(CommonAbstractCriteriaImpl.java:198)
        at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryImpl.translate(CriteriaQueryImpl.java:691)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1551)

1 个答案:

答案 0 :(得分:1)

您无法直接选择子查询,因为子查询使用仅限于单个结果,因此您必须在函数中使用它。根据EclipseLink docs,EclipseLink支持在FROM子句中使用子查询的JPQL,因此您可以尝试使用它与Criteria API类似,但我不知道它是否可行。