由于eclipselink 2.5.2中的JPQL语句而引发的查询密钥无效,但在2.3.2中有效

时间:2014-07-18 00:08:21

标签: java jpa eclipselink jpql

我有以下JQPL查询:

SELECT e FROM Emp e WHERE SIZE(e.companyBean.emps) > 0

问题是它在eclipselink 2.5.2上没有用,当我在2.3.2上尝试它时它成功运行

我认为JPA可能会有一些变化,不允许这种路径导航,但无法在网络上找到任何内容。

这个查询或eclipselink中的更改可能是导致此异常的原因吗?

由于

整个maven项目:https://github.com/mohamedkomalo/jpa2-eclipselink/

Emp实体:https://github.com/mohamedkomalo/jpa2-eclipselink/blob/master/src/main/java/model/Emp.java

公司实体:https://github.com/mohamedkomalo/jpa2-eclipselink/blob/master/src/main/java/model/Company.java

例外

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-6015] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: Invalid query key [
Query Key emps
   Base model.Emp{DatabaseTable(t0)=DatabaseTable(emp)}] in expression.
Query: ReadAllQuery(name="Emp.findEmployeesInBigCompanies" referenceClass=Emp jpql="SELECT e FROM Emp e WHERE SIZE(e.companyBean.emps) > 0")
    at org.eclipse.persistence.exceptions.QueryException.invalidQueryKeyInExpression(QueryException.java:697)
    at org.eclipse.persistence.internal.expressions.ObjectExpression.getDescriptor(ObjectExpression.java:453)
    at org.eclipse.persistence.internal.expressions.SubSelectExpression.normalizeSubSelect(SubSelectExpression.java:186)
    at org.eclipse.persistence.internal.expressions.ExpressionNormalizer.normalizeSubSelects(ExpressionNormalizer.java:114)
    at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1534)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:549)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1720)
    at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:813)
    at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:744)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:661)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:901)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:613)
    at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:341)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1124)
    at practicies.Day2.runPractice3(Day2.java:58)
    at practicies.Day2.main(Day2.java:22)

2 个答案:

答案 0 :(得分:1)

看起来像是在companyBean关系上创建子查询的错误。解决方法可能是在SIZE函数之外指定联接,即SELECT e FROM Emp e join e.companyBean companyBean WHERE SIZE(companyBean.emps) > 0

答案 1 :(得分:0)

Company没有名为empCollection的字段。也许您在此期间更改了Company实现并忘记更新命名查询?如果您将查询更新为

SELECT e FROM Emp e WHERE SIZE(e.company.emps) > 100

应该可行。

我也发现您在EmpCompany之间没有正确的关系。 mappedBy="companyBean"未正确指向Emp。

中的字段