-->
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: HQL question : group by with order by
PostPosted: Tue Mar 11, 2008 5:40 am 
Newbie

Joined: Wed Mar 22, 2006 5:43 am
Posts: 15
Location: Belgium
Hibernate version: 3.2.3

Mapping documents:

Code:
<hibernate-mapping>
   <class name="myPackage.ProductGroup" table="radProductGroup">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
      <property name="version" column="versionx"/>
        <many-to-one name="document" class="myPackage.Document"/>       
        <property name="productGroupId"/>
        <property name="xml"/>
        <property name="created"/>
   </class>
</hibernate-mapping>


i would need the HQL to get for each 'productGroupId' (it is just an Integer value) prepresented in the table, the most recent record ('created' is a date).

in SQL i get the right results by using this query

Code:
SELECT x.* FROM (SELECT * FROM radProductGroup ORDER BY created DESC) x GROUP BY x.productGroupId;


what would be the way to do this in HQL ?


Last edited by bowa on Wed Mar 12, 2008 4:27 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 3:33 am 
Newbie

Joined: Wed Mar 22, 2006 5:43 am
Posts: 15
Location: Belgium
i thought this would be quite simple ... guess i ll have to give up on the 'history' and just keep one record in the db for each productGroupId and overwrite them when updated :s


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 4:38 am 
Newbie

Joined: Tue Mar 11, 2008 6:41 pm
Posts: 8
Location: France
For your information, your query may be rewritten as:

Code:
SELECT productGroupId, max(created) as created FROM radProductGroup GROUP BY productGroupId ORDER BY max(created) DESC;


or

Code:
SELECT productGroupId, max(created) as created FROM radProductGroup GROUP BY productGroupId ORDER BY 2 DESC;


^_^; hope this works for your database at least.


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.