File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes EJB QL & heterogeneous relationships Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB QL & heterogeneous relationships" Watch "EJB QL & heterogeneous relationships" New topic
Author

EJB QL & heterogeneous relationships

Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
In the following entity relationship:
<blockquote>code:
<pre name="code" class="core">
A ----> B
rel / \
C D
</pre>
</blockquote>

basically:
B is an abstract class,
an instanceA can have a rel that is an instanceC or and instanceD

Is there a way to use EJB QL to search for A's who have a (B that is a) C with name='foo'? (where name is a property on the C entity, not on the B or D)

I'm guessing the answer is no, but i really wish it was yes!

Otherwise I need to refactor this to:
<blockquote>code:
<pre name="code" class="core">
A ----> C
\ relC
----> D
relD
</pre>
</blockquote>
where only one of the C or D will ever be set,
and to make it worse, there's not only C & D,
there are E and F as well, and maybe more in
the future.

ugliness.

Anyway, if you have any insight, I'd like to hear it.



Things I wished worked:
1. understandably doesn't work...
<blockquote>code:
<pre name="code" class="core">SELECT a
FROM com.baz.A a
WHERE a.rel.name = 'foo'

(a.rel "is a" B, with no name, ergo no go)
</pre>
</blockquote>

2. possible new syntax, perhaps too Java-y
<blockquote>code:
<pre name="code" class="core">SELECT a FROM com.baz.A a,
IN ((com.baz.C)a.name) c
WHERE c = 'foo'

(adds casting syntax, basically casting to the needed subclass, returning null otherwise)
</pre>
</blockquote>

3. a more SQL syntax, adding a function
<blockquote>code:
<pre name="code" class="core">SELECT a
FROM com.baz.A a
WHERE CLASS(a.baz) LIKE com.baz.C
AND c = 'foo'

(adds a CLASS keyword)
</pre>
</blockquote>

Edit: OUCH. Trying to edit my post, it's spitting me HTML instead of UBB
[ July 17, 2008: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
I found a solution.
The Sun full syntax and examples was useful (includes BNF).

It's similar as to objects that have no navigable relationships.

<blockquote>code:
<pre name="code" class="core">
SELECT a
FROM A a,
IN (a.rel) b
WHERE EXISTS
(SELECT c
FROM C c
WHERE c = b
AND c.name = 'foo')
</pre>
</blockquote>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB QL & heterogeneous relationships