-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: OR clauses in HQL
PostPosted: Wed Aug 31, 2005 10:14 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 4:54 pm
Posts: 32
is there a good reference for using OR clauses in HQL? i seem to end up with cartesian products against my will. for instance, the following hql:

Code:
select count(*) from Library library where ( library.books.size > 0 or library.magazines.size > 0 )


generates the following SQL:

Code:
select count(*) as col_0_0_
from LIBRARY library0_, BOOK books1_, MAGAZINE magazines2_
where (
(select count(books1_.LIBRARY_GUID)
  from BOOK books1_
  where library0_.LIBRARY_GUID=books1_.LIBRARY_GUID) > 0
or
  (select count(magazines2_.LIBRARY_GUID) from MAGAZINE magazines2_
   where library0_.LIBRARY_GUID=magazines2_.LIBRARY_GUID)>0
and library0_.LIBRARY_GUID=books1_.LIBRARY_GUID
and library0_.LIBRARY_GUID=magazines2_.LIBRARY_GUID)


instead of the correct:

Code:
select count(*) as col_0_0_
from LIBRARY library0_, BOOK books1_, MAGAZINE magazines2_
where (
(select count(books1_.LIBRARY_GUID)
  from BOOK books1_
  where library0_.LIBRARY_GUID=books1_.LIBRARY_GUID) > 0
or
  (select count(magazines2_.LIBRARY_GUID) from MAGAZINE magazines2_
   where library0_.LIBRARY_GUID=magazines2_.LIBRARY_GUID)>0)
and
   library0_.LIBRARY_GUID=books1_.LIBRARY_GUID
   and library0_.LIBRARY_GUID=magazines2_.LIBRARY_GUID


in other words it generates (A or B and C and D) instead of (A or B) and C and D

i'd be grateful for pointers on how to do this correctly in HQL. i am building a query based on several user inputs, and three of the criteria can follow this "or" strategy (so doing some sort of union as a hack is too messy)

john


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.