jsr338-experts@jpa-spec.java.net

[jsr338-experts] Re: query improvements: downcasting

From: Michael Bouschen <michael.bouschen_at_akquinet.de>
Date: Tue, 8 Mar 2011 21:26:33 +0100

Hi Linda, hi all,

I would like to second what Matthew replied: an invalid downcast should
cause the query condition to evaluate to false.

Otherwise the example Linda gave in her initial mail will result in an
exception in any case:

   SELECT e FROM Employee JOIN e.projects p
   WHERE TREAT(p AS LargeProject).budget > 1000 OR
         TREAT(p AS SmallProject).name LIKE "Persist%" OR ...

For LargeProject instances the second TREAT expression will cause the
query to fail with an exception, where for SmallProject instances it is
the first TREAT expression causing the exception.

Regards Michael

> Responses inline...
>
>> SQL supports this via the TREAT ... AS operator:
>> TREAT (expression AS datatype)
>> where datatype is a subtype of the static type of the expression.
>>
> ok
>
>> An open issue however is the handling of the case where the
>> instance passed to TREAT is not of the same type or a subtype of the
>> specified datatype.
>>
>> Databases seem to differ as to whether the result should be null or
>> whether an error is raised. We need to decide this as well.
>>
> It seems appropriate to me that in the event that the downcast is
> invalid, it should cause all query conditions involving the identifier
> that is being downcast to evaluate to false. I don't think an error
> should be raised. This would also prevent our having to worry about
> attempting to return results whose specification includes instances or
> reachable properties of instances of the downcast type (as in your
> first example).
>
>> The Criteria API already provides the Expression method
>> <X> Expression<X> as(Class<X> type);
>>
> Same semantics as the string query form, IMHO.
>
> -matthew
>


-- 
*Michael Bouschen*
*Prokurist*
akquinet tech_at_spree GmbH
Bülowstr. 66, D-10783 Berlin
Fon:   +49 30 235 520-33
Fax:   +49 30 217 520-12
Email: michael.bouschen_at_akquinet.de
Url: www.akquinet.de <http://www.akquinet.de>
akquinet tech_at_spree GmbH, Berlin
Geschäftsführung: Martin Weber, Prof. Dr. Christian Roth
Amtsgericht Berlin-Charlottenburg HRB 86780 B
USt.-Id. Nr.: DE 225 964 680