persistence@glassfish.java.net

Re: SQLException running EJBQL query selecting association field

From: Michael Bouschen <Michael.Bouschen_at_Sun.COM>
Date: Tue, 01 Nov 2005 14:34:11 +0100

Hi Gordon,

The parser has been changed in order to fix a problem with multiple
navigations in the select clause expression, e.g. "SELECT
o.customer.name FROM Order o". This query ran into an exception "Invalid
query key [customer] in expression.". The fix changed class SelectNode
to use the class of the left most node of the select expression as
reference class. This seems to not work if the select expression selects
an association field.

I think I found a fix. I changed the code in SelectNode and check
whether the select expression is selecting a direct-to-field. If yes, I
use the code added in the previous fix, meaning the class of the left
most node determines the reference class. If not, I use the class of the
selected field as reference class (which is what the old parser code did).

Attached you find the changes of SelectNode.java in 'cvs diff -u' output
format.

Thanks!

Regards Michael

> Hello Michael,
> I am not familliar enough with your changes to the EJBQL parser to point out what is causing this issue but I can explain why this error is occuring within TopLink.
> The Reference Class has been set to Order on the query but the reference class should be Customer. I think the old parser code used to evaluate the path expressions to determine what the real target type is. (ie Order.customer is really selecting customers).
>
> --Gordon
>
> -----Original Message-----
> From: Michael Bouschen [mailto:Michael.Bouschen_at_Sun.COM]
> Sent: Monday, October 31, 2005 9:11 AM
> To: Tom Ware
> Cc: persistence_at_glassfish.dev.java.net
> Subject: SQLException running EJBQL query selecting association field
>
>
> Hi Tom,
>
> I run into a problem with an EJBQL query selecting an association field:
>
> EJBQL: SELECT o.customer FROM Order o
> SQL: SELECT CMP3_ORDER.ORDER_ID, CMP3_ORDER.SHIP_ADDR,
> CMP3_ORDER.CREATION_DATE, CMP3_ORDER.TOTAL_PRICE,
> CMP3_ORDER.ORDER_VERSION, CMP3_ORDER.QUANTITY,
> CMP3_ORDER.ITEM_ID, CMP3_ORDER.CUST_ID
> FROM CMP3_CUSTOMER t0, CMP3_ORDER t1
> WHERE (t0.CUST_ID (+) = t1.CUST_ID)
> Running the above query on a Oracle database results in a SQLException:
> ORA-00904: "CMP3_ORDER"."CUST_ID": invalid identifier.
>
> Any idea?
>
> I filed an glassfish issue and assigned to you:
> https://glassfish.dev.java.net/issues/show_bug.cgi?id=67
>
> Regards Michael
>
>



Index: SelectNode.java
===================================================================
RCS file: /cvs/glassfish/entity-persistence/src/java/oracle/toplink/essentials/internal/parsing/SelectNode.java,v
retrieving revision 1.2
diff -u -r1.2 SelectNode.java
--- SelectNode.java 18 Oct 2005 19:39:17 -0000 1.2
+++ SelectNode.java 1 Nov 2005 13:31:31 -0000
@@ -310,7 +310,9 @@
    * @return the class this query is querying for
    */
     public Class getReferenceClass(GenerationContext context) {
- return resolveBaseClass(context);
+ return selectingDirectToField(context) ?
+ resolveBaseClass(context) :
+ resolveClass(context);
     }
 
     /**