Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with OR parenthesis in HQL

 
Sandeep Bhoyar
Greenhorn
Posts: 17
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So are you going to show us the query and what is going wrong with it?
 
Sandeep Bhoyar
Greenhorn
Posts: 17
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic