According to the specification, the correct syntax should be:
... JOIN TREAT(m.list AS Derived1) der1 JOIN TREAT(m.list AS Derived2) der2 ...
without any commas.
Question
I try to do a JPQL Query on the following Entity Graph: Derived1 and Derived2 are Subclasses of ReferencedEntity. Main Entity has a List of ReferencedEntity. Assume for now, that there is only one instance of Derived1 and Derived2 in the list of each MainEntity.
+--------------+
| MainEntity |
+--------------+ +------------------+
| list | --- OneToMany ---> | ReferencedEntity |
+--------------+ +------------------+
| String a1 |
+------------------+
^
|
+-----------+-----------+
| |
+--------------+ +--------------+
| Derived1 | | Derived2 |
+--------------+ +--------------+
| String d1 | | String d2 |
+--------------+ +--------------+
Now I want to Group MainEntities by the d1 and d2 Values they contain in their List. Therefore I write the following JPQL Query:
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
I think this should work but I get:
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
Just for testing purposes I tried a different join:
SELECT COUNT(m) FROM MainEntity m JOIN TREAT(m.list AS Derived1) der1, m.list refd GROUP BY der1.d1, refd.a1
This one works and delivers a result.
Then i tried a third query and just changed the order of the join:
SELECT COUNT(m) FROM MainEntity m JOIN m.list refd, TREAT(m.list AS Derived1) der1 GROUP BY der1.d1, refd.a1
This leads to exactly the same Exception (but this time for 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
To me it looks like there might be a bug in EclipseLink, or am I trying something thats forbidden?
Solution
According to the specification, the correct syntax should be:
... JOIN TREAT(m.list AS Derived1) der1 JOIN TREAT(m.list AS Derived2) der2 ...
without any commas.