jsr338-experts@jpa-spec.java.net

[jsr338-experts] Re: query improvements: downcasting

From: Werner Keil <werner.keil_at_gmx.net>
Date: Wed, 9 Mar 2011 02:23:04 +0530

On Wed, Mar 9, 2011 at 1:21 AM, Emmanuel Bernard
<emmanuel.bernard_at_jboss.com> wrote:

On 7 mars 2011, at 21:45, Linda DeMichiel wrote:

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

This is really the use case that makes the most sense to me as the other use
cases Linda has shown seem to overlap with TYPE.

but that leads to some questions:
1. if both LargeProject and SmallProject have a budget property, do we
return all Projects.budget > 1000? Or is the query more like
 SELECT e FROM Employee JOIN e.projects p
 WHERE ( TYPE(p) = LargeProject AND p.budget > 1000) OR
      (TYPE(p) = SmallProject AND p.name LIKE "Persist%" ) OR
       p.description LIKE "COST OVERRUN"

For reference the idiom in Java is
(p instanceof LargeProject && ( (LargeProject) p).projects > 1000) ||
(p instanceof SmallProject && isLike( ((SmallProject) p).name,
"Persist%" ) ) ||
isLike(p.description, "COST OVERRUN") //evaluates to true all the time?

And assuming javac was smarter we could do

(p instanceof LargeProject) && p.projects > 1000) ||
(p instanceof SmallProject) && isLike( p.name, "Persist%" ) ||
isLike(p.description, "COST OVERRUN")

2. if LargeProject does not have a budget property, what happens?

-------

I suppose, if LargeProject does not have a budget property an exception
would have to be thrown in any case.

Although you may as in the Java examples get a simple true/false result,
most cases in Java where you'd try to directly compare or cast two types
into each other would also result in a (ClassCast)Exception.

Werner