persistence@glassfish.java.net

JPQL: fix SQLException thrown for constructor query using COUNT

From: Michael Bouschen <Michael.Bouschen_at_Sun.COM>
Date: Tue, 06 Mar 2007 16:58:40 +0100

Hi Tom,

I looked into issue 2496 "JPQL: constructor query using COUNT results in
SQLException": https://glassfish.dev.java.net/issues/show_bug.cgi?id=2496

The issue is that the generated SQL misses a COUNT argument, if the
COUNT is counting a a joined variable and the COUNT is an argument of a
constructor expression, e.g.
  SELECT NEW test.pojo.CustomerDetail(c.name, COUNT(o)) FROM Customer c
JOIN c.orders o GROUP BY c.name
Method ReportQuery.prepareObjectAttributeCount iterates the SELECT
expressions of a ReportQuery in order to execute some special handling
for COUNT aggregates in the SELECT clause. I extended the method to
iterate the constructor arguments in case of a constructor expression.
This makes sure we generate the same SQL whether there is a constructor
expression or not. You find my ReportQuery changes and a new test method
attached. Please have a look.

Thanks!

Regards Michael