-->
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.  [ 3 posts ] 
Author Message
 Post subject: new to hql/hibernate - howto sql except clause?
PostPosted: Tue Nov 27, 2007 5:45 pm 
Newbie

Joined: Tue Nov 27, 2007 5:31 pm
Posts: 2
Hello!

Let's say I've got an HQL query that's looking something like this:

Code:
from MyTable t where
t.field1 = 'something' and
t.bar = 'blah' or
t.bar2 = 'blah2' or
t.field3 = 'that'



Well, unfortunately that doesn't work the way I want, because it of course returns 't.field1' results that _do_not_ equal "something", so long as 't.bar', 't.bar2', etc equal one of those specified values.

I want to ensure that the results returned from the query only produce records where 't.field1' == 'something' BUT where one or more of those OR's are also satisfied.

Using sql, I'd use an except clause, something like:

Code:
select * from MyTable where
bar = 'blah' or
bar2 = 'blah2' or
field3 = 'that'
except (
select * from MyTable where
field1 != 'something' )


How would I achieve equivalent results using HQL?

Many thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 10:21 pm 
Newbie

Joined: Sat Dec 25, 2004 11:18 pm
Posts: 11
Do you want field one to be equal to something or not equal to something ?
You have = to operator for field1 in the HQL you posted ...

from MyTable t where
t.field1 = 'something' and
t.bar = 'blah' or
t.bar2 = 'blah2' or
t.field3 = 'that'

I assume it was a typo. Have you tried this :

from MyTable t where
t.field1 != 'something' and
(t.bar = 'blah' or
t.bar2 = 'blah2' or
t.field3 = 'that' )

I have added brackets for expression priority...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 12:01 am 
Newbie

Joined: Tue Nov 27, 2007 5:31 pm
Posts: 2
mananmehra wrote:
Do you want field one to be equal to something or not equal to something ?
You have = to operator for field1 in the HQL you posted ...


Oh, yes - that's because I was using the EXCEPT clause, so using that, I was specifying to exclude all records that were not equal to 'something'.


Quote:
from MyTable t where
t.field1 != 'something' and
(t.bar = 'blah' or
t.bar2 = 'blah2' or
t.field3 = 'that' )

I have added brackets for expression priority...



Ah sheesh - it was as easy as using brackets! I should have known to have tried that...

Thank you very much for helping me out there!

Cheers


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

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.