-->
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.  [ 8 posts ] 
Author Message
 Post subject: Wrong query generated by hibernate
PostPosted: Tue Dec 20, 2005 3:56 am 
Newbie

Joined: Tue Dec 20, 2005 3:54 am
Posts: 2
I think there is a bug in a query statement translated by hibernate. When running a query like this :
SELECT COUNT(o) FROM org.ictrace.server.pm.DMWeightBean o WHERE ((o.sale.reference = :reference) OR (o.purchase.localReference = :reference1))

This shows in the hibernate logs :

2005-11-16 11:13:09,838 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] HQL: SELECT COUNT(o) FROM org.ictrace.server.pm.DMWeightBean o WHERE ((o.sale.reference = :reference) OR (o.purchase.localReference = :reference1))
2005-11-16 11:13:09,838 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] SQL: select count(dmweightbe0_.UNID_UOID) as col_0_0_ from t_Weight dmweightbe0_, t_WeightGroup dmweightgr1_, t_WeightGroup dmweightgr2_ where dmweightbe0_.f_Purchase_UOID=dmweightgr2_.UNID_UOID and dmweightbe0_.f_Sale_UOID=dmweightgr1_.UNID_UOID and (dmweightgr1_.f_Reference=? or dmweightgr2_.f_LocalReference=?)

This is wrong because there shouldn't be a "AND" UNID_UOID selections.

We are using JBoss 4.0.2 RC1 with EJB3 if this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 6:57 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
this is a theta styled join criteria, nothing wrong here.

the dotted notation . makes an implicit inner join

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 9:52 am 
Newbie

Joined: Tue Dec 20, 2005 3:54 am
Posts: 2
how should the query look like in this case ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 10:19 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Let me rephrase, I see no error in the SQL query regarding the HQL one.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 9:38 am 
Newbie

Joined: Tue Dec 20, 2005 10:29 am
Posts: 2
I tend to not agree with Emmanuel on this one. I may be wrong but if so I would like an explanation.
So, lets take the WHERE clause from the HQL : "WHERE ((o.sale.reference = :reference) OR (o.purchase.localReference = :reference1))"

This means that it should select "o" if one of the conditions is meet(and regardless of what is happening to the other condition), right?

Then from the log we see that this is translated in :
where dmweightbe0_.f_Purchase_UOID=dmweightgr2_.UNID_UOID and dmweightbe0_.f_Sale_UOID=dmweightgr1_.UNID_UOID and (dmweightgr1_.f_Reference=? or dmweightgr2_.f_LocalReference=?)

And I think that the theta join is wrong, since it uses "and"
"dmweightbe0_.f_Purchase_UOID=dmweightgr2_.UNID_UOID and dmweightbe0_.f_Sale_UOID=dmweightgr1_.UNID_UOID"

And this implies that both FK, f_Purchase_UOID and f_Sale_UOID must match which is wrong.

Shouldn't the where clause be something like :
where
(dmweightbe0_.f_Purchase_UOID=dmweightgr2_.UNID_UOID and dmweightgr2_.f_LocalReference=? )
and
(dmweightbe0_.f_Sale_UOID=dmweightgr1_.UNID_UOID and dmweightgr1_.f_Reference=? )


I'm not very experienced with this but this look more logical to me. Could you please explain why/where I might be wrong?

Regards, Andrei Chiritescu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 12:57 pm 
Newbie

Joined: Tue Dec 20, 2005 10:29 am
Posts: 2
Sorry, I have made a little mistake(used AND instead of OR) so, here is the correct query:
where
(dmweightbe0_.f_Purchase_UOID=dmweightgr2_.UNID_UOID and dmweightgr2_.f_LocalReference=? )
OR
(dmweightbe0_.f_Sale_UOID=dmweightgr1_.UNID_UOID and dmweightgr1_.f_Reference=? )


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 1:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
So, lets take the WHERE clause from the HQL : "WHERE ((o.sale.reference = :reference) OR (o.purchase.localReference = :reference1))"

This means that it should select "o" if one of the conditions is meet(and regardless of what is happening to the other condition), right?

Why, because *you* say so? Based on what? Your gut?

I understand what you are saying from a wishful thinking position, but that has nothing to do with reality, nor how Hibernate has treated this forever, nor what the ejb3 spec actually says on the subject.

Have a look at the Hibernate documentation regarding implicit / explicit joins.

Oh, but that's just vendor interpretation, right? Nope. Go and have a look at the EJB3 spec (section 4.4.4 Path Expressions):
Quote:
Path expression navigability is composed using “inner join” semantics. That is, if the value of a non-terminal
association-field in the path expression is null, the path is considered to have no value, and does
not participate in the determination of the result.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 1:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
So the right way to do it is:
Code:
select count(distinct o)
from org.ictrace.server.pm.DMWeightBean o
    left join o.sale as sale
    left join o.purchase as purchase
where sale.reference = :reference
  or purchase.localReference = :reference1


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.