-->
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: Sort with a Calculated Method
PostPosted: Fri Apr 11, 2008 4:45 am 
Newbie

Joined: Tue Jan 30, 2007 5:58 pm
Posts: 14
Hi

I have 2 java classes: Media and Vote and a OneToMany association

Media has a Set of vote
Vote has a note (integer) as an attribute

I have a calculated method which sum all of notes of my media

Code:
public class Media {
....
private Set votes = new HashSet();
...
   public int getEtoiles() {
      int total = 0;
      for (Iterator iterator = votes.iterator(); iterator.hasNext();) {
         Vote vote = (Vote) iterator.next();
         Integer note = vote.getNote();
         total += note.intValue();
      }
      return total;
   }
}


Code:
public class Vote {
   private Long id;

   private Integer note;
}


I want to get all medias sorted by the result of my method getEtoiles().

Any ideas to do this with a HQL query ? (with Criteria)
(I think I can use the sum method but I don't know how to do this)

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 5:34 am 
Newbie

Joined: Tue Jan 30, 2007 5:58 pm
Posts: 14
With Oracle, I can use this SQL query:

select m.id, sum(note) as total
from t_media m, t_vote v
where m.id=v.media_id
group by m.id
order by total desc

But with HQL and the Criteria API, how can I do the same query ?

I don't want only the m.id attributes, but all the Media results objects and the total information.


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.