Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

[JPQL] UNION

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"

 
please buy my thing and then I'll have more money:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic