Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 347562

Summary: Query parse error when using GROUP BY/HAVING
Product: z_Archived Reporter: Edward Rayl <j.edward.rayl>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: RESOLVED FIXED QA Contact:
Severity: major    
Priority: P2 CC: j.edward.rayl, jamesssss, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Edward Rayl CLA 2011-05-28 17:29:15 EDT
Build Identifier: 2.2.0

The query below fails to parse correctly.  This query is on the Oracle sample schema called HR, with the appropriate entities mapped to it.

    select d from Department d group by d having COUNT(d.employees1) = 3

with the following SQL output:

    SQL: SELECT DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID 
    FROM DEPARTMENTS 

This next query works however:

    select d, COUNT(d.employees1) from Department d group by d having COUNT(d.employees1) = 3

with the following SQL output:

    SQL: SELECT t0.DEPARTMENT_ID, t0.DEPARTMENT_NAME, t0.MANAGER_ID, 
        t0.LOCATION_ID, COUNT(t1.EMPLOYEE_ID) 
    FROM DEPARTMENTS t0, EMPLOYEES t1 
    WHERE (t1.DEPARTMENT_ID = t0.DEPARTMENT_ID) 
    GROUP BY t0.DEPARTMENT_ID, t0.DEPARTMENT_NAME, t0.MANAGER_ID, t0.LOCATION_ID 
    HAVING (COUNT(t1.EMPLOYEE_ID) = ?) 

The specification should allow the first query. A portion of Section 4.7 of JSR 317: Java Persistence API, Version 2.0 says;

The HAVING clause is used to filter over the groups, and can contain aggregate functions over
attributes included in the groups and/or functions or other query language operators over the attributes that are used for grouping. It is not required that an aggregate function used in the HAVING clause also be used in the SELECT clause.


Reproducible: Always

Steps to Reproduce:
1. Create JPA entities for the Oracle HR schema
2. Create a JPQL query as shown above.
3. The result of the query should be one and only one department, not 27
Comment 1 Edward Rayl CLA 2011-05-29 10:38:22 EDT
This bug can be reproduced in 2.3.0 Milestone Build RC2.
Comment 2 Edward Rayl CLA 2011-06-03 16:05:32 EDT
This bug can be reproduced in 2.3.0 Milestone Build RC3.

I can email an Eclipse project containing HR entities and a command line JPQL query tool to the assignee if that would be useful.
Comment 3 Tom Ware CLA 2011-06-09 10:03:53 EDT
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Community: Please vote for this bug if it is important to you.  Votes are one of the main criteria we use to determine which bugs to fix next.
Comment 4 James Sutherland CLA 2012-02-01 13:39:43 EST
The issue seems to be a ReadAllQuery is used for the query, but a ReportQuery is required.  JPQL should check the group by and create a ReportQuery.

Will fix with Hermes.
Comment 5 James Sutherland CLA 2012-04-11 10:40:36 EDT
Fixed in 2.4
Comment 6 Eclipse Webmaster CLA 2022-06-09 10:21:27 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink