EJBQL unable to reference PKs not mapped as ObjAttributes
---------------------------------------------------------
Key: CAY-1391
URL: https://issues.apache.org/jira/browse/CAY-1391
Project: Cayenne
Issue Type: Improvement
Components: Core Library
Affects Versions: 3.0RC2
Reporter: Dave Lamy
I hit a case where an ObjEntity that contained an auto-generated PK needed to
be referenced by an EJBQL query. The PK is not mapped as an ObjAttribute.
The specific query case is when trying to perform a COUNT query with criteria
on a to-many association, such as:
SELECT count(p) FROM Parent p JOIN p.children c WHERE c.age > 5
The result in this case will be erroneous, as the generated SQL will count
duplicated parent IDs across the cartesian join on the children.
The SQL fix for this is to include a DISTINCT in the count, but requesting a
count(distinct p) does not produce workable SQL. Ideally then, one could
reference the PK of the parent.
I've written a solution to this using an HQL-esque "id" property to be
referenced in the select. Thus this statement will work properly:
SELECT count(distinct p.id) FROM Parent p JOIN p.children c WHERE c.age > 5
The code will attempt to use the "normal" ObjAttribute discovery method first;
but failing that, if the last path component == "id" and the ObjEntity has a
single PK then it will utilize the PK ObjAttribute from the ObjEntity.
This code works on both my production test case as well as in the unit test.
Please consider for inclusion in the next release, I hate monkey patches!
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.