-->
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: derived properties with MySQL 5
PostPosted: Sun Mar 25, 2007 5:38 pm 
Newbie

Joined: Sun Mar 25, 2007 5:28 pm
Posts: 2
Hi there

I got two tables in my Database model :
T_RESTAURANT and T_RATING

I have written the following mapping for the Restaurant Class

Quote:
...
<property name="score" generated="always" update="false" insert="false">
<formula>
(select avg(r.RATING) from T_RATING r where r.RESTAURANT_ID = RESTAURANT_ID)
</formula>
</property>
...


It generates the following SQL code :

Quote:
select
restaurant0_.ID_RESTAURANT as ID1_1_0_,
restaurant0_.NAME as NAME1_0_,
restaurant0_.DESCCRIPTION as DESCCRIP3_1_0_,
restaurant0_.STREET as STREET1_0_,
restaurant0_.CITY as CITY1_0_,
restaurant0_.PROVINCE as PROVINCE1_0_,
restaurant0_.ZIPCODE as ZIPCODE1_0_,
restaurant0_.COUNTRY as COUNTRY1_0_,
(select
avg(r.RATING)
from
T_RATING r
where
r.RESTAURANT_ID = restaurant0_.RESTAURANT_ID) as formula0_0_
from
eatproject_db.T_RESTAURANT restaurant0_
where
restaurant0_.ID_RESTAURANT=?


and create the following error :

Quote:
Unknown column 'restaurant0_.RESTAURANT_ID' in 'where clause'


a correct request for MySQL would be :


Quote:
select
restaurant0_.ID_RESTAURANT as ID1_1_0_,
restaurant0_.NAME as NAME1_0_,
restaurant0_.DESCCRIPTION as DESCCRIP3_1_0_,
restaurant0_.STREET as STREET1_0_,
restaurant0_.CITY as CITY1_0_,
restaurant0_.PROVINCE as PROVINCE1_0_,
restaurant0_.ZIPCODE as ZIPCODE1_0_,
restaurant0_.COUNTRY as COUNTRY1_0_,
(select
avg(r.RATING)
from
T_RATING r
where
r.RESTAURANT_ID = <ID_VALUE>) as formula0_0_
from
eatproject_db.T_RESTAURANT restaurant0_
where
restaurant0_.ID_RESTAURANT=<ID_VALUE>


IS there a workaround to use that kind of function with MySQL

Thinks for your help


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 25, 2007 9:44 pm 
Newbie

Joined: Sun Mar 25, 2007 9:39 pm
Posts: 1
I'm not sure if I'm reading this right or not but looking at the SQL Hibernate has generated it appears you have a column called ID_RESTAURANT, I get that from the following:

restaurant0_.ID_RESTAURANT as ID1_1_0_,

If that's the case (and assuming that the equivalent ID in T_RATING is still REStTAURANT_ID) maybe the following formula would work:

(select avg(r.RATING) from T_RATING r where r.RESTAURANT_ID = ID_RESTAURANT)


Probably just a typo.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 5:48 pm 
Newbie

Joined: Sun Mar 25, 2007 5:28 pm
Posts: 2
YOu were right sorry for that stupid mistake I should have seen it before posting


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.