persistence@glassfish.java.net

Question about generated SQL for a JPQL query using a COUNT aggregate on a joined variable

From: Michael Bouschen <Michael.Bouschen_at_Sun.COM>
Date: Thu, 01 Mar 2007 20:56:04 +0100

Hi,

I have a question about the SQL that should be generated for a a JPQL
query using a count aggregate in the SELECT clause that counts a joined
variable, e.g.:
  SELECT c.name, COUNT(o) FROM Customer c LEFT JOIN c.orders o GROUP BY
c.name
So what is the right SQL for the JPQL COUNT expression?

I think there are three cases and I need some help for case (3):
(1) The entity class Order has a single primary key. Then the SQL should
be "COUNT(o.PK)" where PK is the primary key column of the order table.
(2) Order has a composite primary key and the COUNT does not use
DISTINCT. Then the SQL can do a COUNT on an arbitrary primary key column.
(3) Order has a composite primary key and the JPQL query does a
COUNT(DISTINCT o)). I have no idea what exactly is the right SQL!

Any idea is appreciated!

Regards Michael