Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Correlated Subquery in Hibernate

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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?
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • 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?
 
Craig Taylor
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried with left (outer) join?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic