jsr338-experts@jpa-spec.java.net

[jsr338-experts] Re: query improvements: downcasting

From: Matthew Adams <matthew_at_matthewadams.me>
Date: Thu, 10 Mar 2011 17:12:29 -0600

On Thu, Mar 10, 2011 at 4:56 PM, Michael Bouschen
<michael.bouschen_at_akquinet.de> wrote:
> In "JOIN TREAT(o.product AS Book) b" Product is a superclass of Book, so
> this is a valid downcast (=> no Exception). The only way for an exception
> would be if there is no inheritance relationship between Product and Book.
>
> Regards Michael
>
Ah, the latter example is where I was missing the nuance. Essentially, then:

Case 1:
if (Product.class.isAssignableFrom(Book.class)) {
  // consider expression false for those products that are not books
}
Case 2:
if (!Product.class.isAssignableFrom(Book.class)) {
  throw new PersistenceException(new ClassCastException(...));
}

Correct?

If so, then I would restate **for the deferred,
not-currently-specified projection use of TREAT AS** that if either
Book is not assignment-compatible with Product or the filter results
in type-incompatible instances with the projection expression, an
exception is thrown.

-matthew