-->
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.  [ 5 posts ] 
Author Message
 Post subject: hql question
PostPosted: Thu Nov 20, 2003 9:58 pm 
Newbie

Joined: Mon Sep 01, 2003 9:48 pm
Posts: 17
Hi all,

I'm finally getting to some interesting HQL, having some problems:

Code:
User <----------> * Post

table user(id integer)
table post(id integer, parent integer)

class User {
   int id;
   Set posts; // inverse many-to-one
   ....
}

class Post {
   ...
}


I'm trying to get a count of the groups. The following SQL works:

Code:
SELECT count(*) c,u.name, FROM POSTS p,USERS u where u.id=p.id group by id order by c desc


I thought that this HQL should work, but it doesn't:

Code:
select user from User user order by count(elements(user.posts))


The generated SQL has no where clause, no alias definition of the groups table, only the use of the group table alias (which doesn't exist):

Code:
select user0_.id as id [...] from USERS user0_ order by  count(posts1_.id)


So it would seem that I need to express the relation in a 'where' clause? I thought that was the purpose of the hbm(?) The corresponding .hbm.xml are:

Code:
    <class
        name="User"
        table="USERS"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Integer"
            unsaved-value="null"
        >
            <generator class="native">
            </generator>
        </id>

        <set
            name="posts"
            table="POSTS"
            lazy="true"
            inverse="true"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="ID"
              />

              <one-to-many
                  class="Post"
              />
        </set>

    </class>


and

Code:
<class
        name="Post"
        table="POSTS"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Integer"
        >
            <generator class="assigned">
            </generator>
        </id>

        <many-to-one
            name="user"
            class="User"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="ID"
        />
         </class>


Any ideas on what I am missing here?

thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 10:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
select user.name
from User user
   join user.posts post
group by user.name
order by count(post) desc



I think.

The HQL parser should have thrown an exception over the use of elements() in the order by clause.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 12:15 am 
Newbie

Joined: Mon Sep 01, 2003 9:48 pm
Posts: 17
Hmm. I'm getting an error I've been seeing for a while: "ERROR 1111: Invalid use of group function." This is coming from MySQL. The query that HBN generates is

Code:
select user0_.name as x0_0_ from USERS user0_ inner join POSTS posts1_ on user0_.id=posts1_.ID group by  user0_.name order by  count(posts1_.id)


thanks for checking it out!! :-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 12:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yeah this is an annoying little "feature" of MySQL.

We don't have a good solution for this right now. A workaround is to fall back to a native SQL query.

MySQL should allow aggregate functions in the order by clause. Its silly.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 06, 2003 3:23 pm 
Newbie

Joined: Mon Sep 01, 2003 9:48 pm
Posts: 17
Cool thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.