File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes [JPQL] UNION Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "[JPQL] UNION" Watch "[JPQL] UNION" New topic
Author

[JPQL] UNION

Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Hi!

It is possible to do an "UNION" in JPQL or something like this?
Because I have a abstract class ("Table a") which has 3 different subclass ("Table b", "Table c", "Table d") and when I execute my query I don't know in advance what kind of subclass I need.

Thanks in advance!


SCJP 5
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
JPQL does not support UNION. But any query for a superclass will return all subclasses that match the criteria.
What is the query you are trying to do?

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
I have 4 entities :

The superclass :


The subclasses:



 The variables varB, varC, varD represent the same field but with a different name.



I would like to retrieve an object A with my query
“SELECT a FROM A a, Other o JOIN FETCH a.other WHERE o.varOther like ‘…’”

In this case, if the user puts something in the search field “var” I need to add either varB, varC, or varD in the WHERE clause but I don’t know in advance if the type of the subclass is varB, varC or varD, I’m stuckeeeed!

I've got a "unknown state or association field [varB] of class […A]" by example
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
You might want to consider using a dynamic query, so depending on your argument, then decide which subclass to query.

If you need one query, then you may be able to use EclipseLink's "TREAT" function.

See,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Query_Casting

Otherwise, you could try multiple queries, one per subclass.
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
It doesn't work
To use Treat downcasting, i need a subclass variable no? (example: i don't have a variable "B" in my superclass) so i can't use "TREAT(a.xxx AS B) b"

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [JPQL] UNION