IntelliJ Java IDE
The moose likes Object Relational Mapping and the fly likes Problems with OR parenthesis in HQL Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Problems with OR parenthesis in HQL" Watch "Problems with OR parenthesis in HQL" New topic
Author

Problems with OR parenthesis in HQL

Sandeep Bhoyar
Greenhorn

Joined: May 17, 2010
Posts: 14
I'm using Hibernate and I am facing Problems with OR parenthesis in HQL.
Hibernate removes parenthesis and my search in the database gets broken.
Dose any one know why this is happening and how I can solve this issue.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 2686

I think you need to give us much more information if we're going to help you. For starters, what is your query, how do you know the parentheses are being removed, and what do you mean by "broken"?
Sandeep Bhoyar
Greenhorn

Joined: May 17, 2010
Posts: 14
I am showing the hql query on console

<prop key="hibernate.show_sql">true</prop>

and I tried the same query directly in SQL SERVER (with parenthesis) its working fine but then I the same query I am using in HSQL its giving me wrong.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 2686

So are you going to show us the query and what is going wrong with it?
Sandeep Bhoyar
Greenhorn

Joined: May 17, 2010
Posts: 14
here is the part of my query

strQuery =
....
and ((po.punumber != 'Multiple' and cp.splitPoflag = 1) or (cp.splitPoflag = 0))
.....


Hibernate :
....
and (purchaseor0_.PUNumber<>'Multiple' and compprofil4_.SplitPOFlag=1 or compprofil4_.SplitPOFlag=0)
....


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 2686

AND has higher precedence than OR in SQL, so that query is equivalent to yours - the brackets would make no difference. So it seems that your problem is actually somewhere else. So what is it doing that you don't expect?
Tatiana Vorobeva
Greenhorn

Joined: Aug 06, 2011
Posts: 11

Matthew Brown wrote:...the brackets would make no difference...


Everywhere they are used to change operand's priority. It's not working in Hibernate?

In all other cases "... and ((a and b) or c)" <> "and (a and b or c)". Strange, that it's not correct for Hibernate.
How do we should use OR in such cases?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 2686

Tatiana Vorobeva wrote:In all other cases "... and ((a and b) or c)" <> "and (a and b or c)".

You sure about that?
Tatiana Vorobeva
Greenhorn

Joined: Aug 06, 2011
Posts: 11

Matthew Brown wrote:
Tatiana Vorobeva wrote:In all other cases "... and ((a and b) or c)" <> "and (a and b or c)".

You sure about that?



Oh, sorry, you are right.
I was right about the goal of parentheses, but I confused the operands AND and OR.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 2686

Tatiana Vorobeva wrote:I was right about the goal of parentheses.

Oh, absolutely - always add the parentheses if there's any chance it might be misunderstood. Here Hibernate has stripped some out that weren't needed - I'd assume the way it builds the query isn't a straightforward symbol-for-symbol translation.
 
 
subject: Problems with OR parenthesis in HQL
 
Threads others viewed
how to use UNION function in HQL?
Get only the newest 5 items
Hibernate Newbie, help on queries that do some grouping
operator & cannot be applied to boolean.java.lang.String
Hibernate with DAO?
IntelliJ Java IDE