-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: can i use formula for this?
PostPosted: Fri Jul 09, 2004 3:09 am 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
how can I do something like this but as a formula? is it even possible?

select avg(r.score) from Rating as r where r.ratee = ?

i searched the docs but couldnt find anything too helpful with this

i basically want to get back a list of Users along with the average rating but have them sorted by rating


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 4:51 am 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
oops forgot my mappings, im using mysql 4.0

Code:
    <class name="model.Rating" table="rating" proxy="model.Rating">
        <id column="id" name="id" type="long" unsaved-value="-1">
            <generator class="increment"/>
        </id>

        <property name="score"/>
        <property name="comment"/>
        <property name="date" type="timestamp"/>

        <many-to-one name="ratee" class="model.User" column="rateeUserId" cascade="save-update"/>

        <many-to-one name="rater" class="model.User" column="raterUserId" cascade="save-update"/>

    </class>


    <class name="model.User" table="user" proxy="model.User">
        <id column="id" name="id" type="long" unsaved-value="-1">
            <generator class="increment"/>
        </id>


        <property name="name" type="string"/>
        <property name="lastName" type="string"/>
        <property name="firstName" type="string"/>
        <property name="password" type="string"/>

        <property name="dateCreated" type="timestamp"/>

        <set name="sentRatings" lazy="true" table="rating" cascade="save-update" order-by="date desc">
            <key column="raterUserId"/>
            <one-to-many class="model.Rating"/>
        </set>

        <set name="receivedRatings" lazy="true" table="rating" cascade="save-update" order-by="date desc">
            <key column="rateeUserId"/>
            <one-to-many class="model.Rating"/>
        </set>

    </class>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 4:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I'd suggest you just try it and look at the generated SQL. This might also be doable in plain old HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 5:14 am 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
this is the sql query that does what I want it to do, still trying to figure its HQL equiv

Code:
select user.name as name, avg(r.score) as avgRating
from user, rating as r
where user.id = r.rateeUserId
group by name desc order by avgRating desc limit 10


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 5:25 am 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
in HQL this is as far as ive gotten, almost but not quite there yet

Code:
HQL
select u.name, avg(r.score)  from User as u, Rating as r where r.ratee.id = u.id group by name


so what i tried was to atleast order the results by the avg(r.score), in sql I would use an alias and then order by that alias like avg(r.score) as avgRating but this doesnt work in HQL so im stuck here, anyways ill keep trying different things, but for now ive run out of ideas, oh and i also tried order by avg(r.score) but that didnt work

Code:
HQL
select u.name, avg(r.score)  from User as u, Rating as r where r.ratee.id = u.id group by name


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 11:35 am 
Beginner
Beginner

Joined: Mon May 17, 2004 7:15 am
Posts: 24
lagcisco wrote:
Code:
HQL
select u.name, avg(r.score)  from User as u, Rating as r where r.ratee.id = u.id group by name


Try:
Code:
    select user, avg(r.score)
    from User as user join user.receivedRatings as r
    group by user
    order by avg(r.score)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 8:36 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
that didnt work out, any other ideas?

I really dont want to use direct jdbc for this seemingly simple scenario

1 errors occurred while listing (and calling getPathNames).
net.sf.hibernate.JDBCException: Could not execute query
java.sql.SQLException: General error, message from server: "Invalid use of group function"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 8:38 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You have to "group by" all properties of user. Please, show the exact code you are executing. We can't help you if we have to guess.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 9:56 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
i'm using Hibern8IDE 1.0 to test these querys

i tried what jamstang suggested

Code:
select user, avg(r.score)
    from User as user join user.receivedRatings as r
    group by user
    order by avg(r.score)


and got this error
1 errors occurred while listing (and calling getPathNames).
net.sf.hibernate.JDBCException: Could not execute query
java.sql.SQLException: General error, message from server: "Invalid use of group function"


I also tried

Code:
select user.name, avg(r.score)
    from User as user join user.receivedRatings as r
    group by user.name
    order by avg(r.score)


and got

1 errors occurred while listing (and calling getPathNames).
net.sf.hibernate.JDBCException: Could not execute query
java.sql.SQLException: General error, message from server: "Invalid use of group function"

this is the working SQL that does what I need:
Code:
select user.name as name, avg(r.score) as avgRating
from user, rating as r
where user.id = r.rateeUserId
group by name desc order by avgRating desc limit 10


Code:
mysql> select user.name as name, avg(r.score) as avgRating
    -> from user, rating as r
    -> where user.id = r.rateeUserId
    -> group by name desc order by avgRating desc limit 10 ;
+----------+-----------+
| name     | avgRating |
+----------+-----------+
| lagcisco |    8.0000 |
| andy3    |    5.7500 |
+----------+-----------+
2 rows in set (0.03 sec)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 10:03 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Those queries are OK, please enable the SQL log output in Hibernate and show the translated SQL. I guess that your database doesn't handle it properly.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 1:35 am 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
here is the output log, i am using mysql 4.0.20a


select user.name, avg(r.score)
from User as user join user.receivedRatings as r
group by user.name
order by avg(r.score)


Hibernate: select user0_.name as x0_0_, avg(receivedra1_.score) as x1_0_ from user user0_ inner join rating receivedra1_ on user0_.id=receivedra1_.rateeUserId group by user0_.name order by avg(receivedra1_.score)
[WARN] JDBCExceptionReporter - SQL Error: 1111, SQLState: S1000
[ERROR] JDBCExceptionReporter - General error, message from server: "Invalid use of group function"
[WARN] JDBCExceptionReporter - SQL Error: 1111, SQLState: S1000
[ERROR] JDBCExceptionReporter - General error, message from server: "Invalid use of group function"
[ERROR] JDBCExceptionReporter - Could not execute query
java.sql.SQLException: General error, message from server: "Invalid use of group function"






select user, avg(r.score)
from User as user join user.receivedRatings as r
group by user
order by avg(r.score)






Hibernate: select user0_.id as id, user0_.name as name, user0_.lastName as lastName, user0_.firstName as firstName, user0_.password as password, user0_.dateCreated as dateCrea6_, user0_.birthDate as birthDate, user0_.sex as sex, user0_.email as email, user0_.addressId as addressId, user0_.id as x0_0_, avg(receivedra1_.score) as x1_0_ from user user0_ inner join rating receivedra1_ on user0_.id=receivedra1_.rateeUserId group by user0_.id order by avg(receivedra1_.score)
[WARN] JDBCExceptionReporter - SQL Error: 1111, SQLState: S1000
[ERROR] JDBCExceptionReporter - General error, message from server: "Invalid use of group function"
[WARN] JDBCExceptionReporter - SQL Error: 1111, SQLState: S1000
[ERROR] JDBCExceptionReporter - General error, message from server: "Invalid use of group function"
[ERROR] JDBCExceptionReporter - Could not execute query
java.sql.SQLException: General error, message from server: "Invalid use of group function"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 4:15 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
can i get some help on this issue? any information I still need to provide to get this working?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 5:10 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what happens when you copy/paste the generated sql into a db client?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 14, 2004 7:29 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
heres the output

mysql> select user0_.id as id, user0_.name as name, user0_.lastName as lastName, user0_.firstName as firstName, user0_.password as password, user0_.dateCre
ated as dateCrea6_, user0_.birthDate as birthDate, user0_.sex as sex, user0_.email as email, user0_.addres
-> -> ;
ERROR 1109: Unknown table 'user0_' in field list
mysql> select user0_.id as id, user0_.name as name, user0_.lastName as lastName, user0_.firstName as firstName, user0_.password as password,
-> user0_.dateCreated as dateCrea6_, user0_.birthDate as birthDate, user0_.sex as sex, user0_.email as email, user0_.addressId as addressId, user0_.id
as x0_0_,
-> avg(receivedra1_.score) as x1_0_ from user user0_ inner join rating receivedra1_ on user0_.id=receivedra1_.rateeUserId group by user0_.id order by
-> avg(receivedra1_.score) ;
ERROR 1111: Invalid use of group function
mysql>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 15, 2004 7:37 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
any ideas?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.