-->
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.  [ 2 posts ] 
Author Message
 Post subject: joining on a subquery ?
PostPosted: Mon Jan 12, 2009 6:34 pm 
Newbie

Joined: Thu Sep 25, 2003 7:21 pm
Posts: 17
hi i am trying to solve the following problem

i have a list of messages and a message has a sender

for each sender i would like to list the amount of messages this person has posted in total

ideally i would have this be a single query. and in sql this would be possible with something like the following code

select m1.*, m2.total
from message m1
join (select sender_id, count(0) as total from messages group by sender_id) m2 on m1.sender_id = m2.sender_id

is it possible to accomplish this in jpa / hibernate ? i was looking for a way to join on a subselect but so far i havent found anything


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 7:00 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
User has a collection of products:

Code:
session.createQuery("select u, u.products.size from User u");


Results in:
Code:
Hibernate:
    select
        user0_.ID as col_0_0_,
        (select
            count(products1_.user_id)
        from
            USER_PRODUCTS products1_
        where
            user0_.ID=products1_.user_id) as col_1_0_,
        user0_.ID as ID0_,
        user0_.username as username0_,
        user0_.password as password0_,
        user0_.title as title0_,
        user0_.firstname as firstname0_,
        user0_.lastname as lastname0_,
        user0_.email as email0_,
        user0_.account_id as account8_0_
    from
        USERS user0_


Hope that helps


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