JPA2.1 JPQL查询在JOIN中有多个TREAT(...)操作

时间:2013-09-20 12:12:49

标签: jpql jpa-2.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中可能存在错误,或者我正在尝试禁止的内容?

1 个答案:

答案 0 :(得分:3)

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

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

没有任何逗号。