• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Correlated Subquery in Hibernate

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on converting a fairly hairy SQL query into HQL and running into some trouble. I was hoping someone else would be able to shed some light on the problem.

Here's the original SQL query:



Notice that the pl.prevID in the subquery is related to the alias pl defined in the outer query.

The purpose of this query is to generate a list of all rows in the PrevLevels table and, for each row, get the number of records in the CRev table that correspond. So, for example, I might see something like this:



Here's what I have in HQL:



I can get the outer query to work without the subquery and, likewise, I can get the subquery to work without the outer query, but if I try to execute this query as is, I get this error message:



It then goes on to spit out the query I just wrote above.

Any bright ideas?
 
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that I would call it bright , but could it be that this "aggregate function" confusion comes from the parentheses around the inner SELECT?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you rewrite as :



I've always ran into problems with hibernate and multi selects (eg: select x,y, (select z) ) - I usually have to rewrite as a group...
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Craig Taylor:
Can you rewrite as :



Unfortunately, no - the two queries are not the same. The query you have here (which is actually what I had started with) will leave out rows from the table PrevLevels if there are no matching records in the other tables. For my use, it's very important that, if there are no matching rows, I still get that row back with a value of 0.
 
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried with left (outer) join?

 
machines help you to do more, but experience less. Experience this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic