1

我尝试对以下实体图进行 JPQL 查询:Derived1 和 Derived2 是 ReferencedEntity 的子类。主实体有一个 ReferencedEntity 列表。现在假设每个 MainEntity 的列表中只有一个 Derived1 和 Derived2 的实例。

+--------------+
|  MainEntity  |
+--------------+                    +------------------+
|  list        | --- OneToMany ---> | ReferencedEntity |
+--------------+                    +------------------+
                                    | String a1        |
                                    +------------------+
                                              ^
                                              |
                                  +-----------+-----------+
                                  |                       |
                          +--------------+         +--------------+
                          |   Derived1   |         |   Derived2   |
                          +--------------+         +--------------+
                          |   String d1  |         |   String d2  |
                          +--------------+         +--------------+

现在我想通过它们在列表中包含的 d1 和 d2 值对 MainEntities 进行分组。因此,我编写了以下 JPQL 查询:

SELECT COUNT(m) FROM MainEntity m JOIN TREAT(m.list AS Derived1) der1, TREAT(m.list AS Derived2) der2 GROUP BY der1.d1, der2.d2

我认为这应该可行,但我得到:

Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [SELECT COUNT(m) from MainEntity m JOIN TREAT(m.list AS Derived1) der1, TREAT(m.list AS Derived2) der2 GROUP BY der1.d1, der2.d2]. 
[83, 84] The FROM clause has 'TREAT(m.list' and 'AS Derived2' that are not separated by a comma.
[83, 83] The right parenthesis is missing from the sub-expression.
[77, 83] The identification variable 'm.list' is not following the rules for a Java identifier.
[84, 84] A "root object" must be specified.
[95, 127] The query contains a malformed ending.
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1583)
        ... 2 more
Java Result: 1

仅出于测试目的,我尝试了不同的连接:

SELECT COUNT(m) FROM MainEntity m JOIN TREAT(m.list AS Derived1) der1, m.list refd GROUP BY der1.d1, refd.a1

这个有效并提供了结果。

然后我尝试了第三个查询,只是改变了连接的顺序:

SELECT COUNT(m) FROM MainEntity m JOIN m.list refd, TREAT(m.list AS Derived1) der1 GROUP BY der1.d1, refd.a1

这会导致完全相同的异常(但这次是 Derived1)。

Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [SELECT COUNT(m) from MainEntity m JOIN m.list refd, TREAT(m.list AS Derived1) der1 GROUP BY der1.d1, refd.a1]. 
[64, 65] The FROM clause has 'TREAT(m.list' and 'AS Derived1' that are not separated by a comma.
[64, 64] The right parenthesis is missing from the sub-expression.
[58, 64] The identification variable 'm.list' is not following the rules for a Java identifier.
[65, 65] A "root object" must be specified.
[76, 108] The query contains a malformed ending.
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
        at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1583)
        ... 2 more
Java Result: 1

对我来说,EclipseLink 中可能存在错误,或者我是否正在尝试一些被禁止的东西?

4

1 回答 1

3

根据规范,正确的语法应该是:

... JOIN TREAT(m.list AS Derived1) der1 JOIN TREAT(m.list AS Derived2) der2 ...

没有任何逗号。

于 2013-09-23T16:54:17.513 回答