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: Find all latest
PostPosted: Thu Apr 22, 2010 7:05 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
I have a Hibernate mapped status class that holds multiple status entries for an entity, like...

Code:
    public class EntityStatus {
       private int OwningEntityID
       private int StatusID
       private java.util.Date StatusUpdated
    }

    OwningEntity    Status ID     StatusUpdated
     -------------------------------------------
           1                   1             23/01/10
           1                   2             23/02/10
           1                   4             23/04/10
           2                   4             19/02/10
           3                   1             21/02/10
           3                   1             21/03/10
           4                   3             23/01/10
           4                   4             11/04/10


I'm struggling to define a valid HQL query that returns just the latest status entries for each owning entity based around the most recent 'StatusUpdated', something like..
Code:
from distinct m EntityStatus as m
where m.statusUpdated = (select max(n.statusUpdated) from EntityStatus as n where n.owningEntityID = m.owningEntityID)


The results should be...
Code:
    OwningEntity    Status ID     StatusUpdated
     -------------------------------------------
           1                   4             23/04/10
           2                   4             19/02/10
           3                   1             21/03/10
           4                   4             11/04/10


Any suggestions?


Top
 Profile  
 
 Post subject: Re: Find all latest
PostPosted: Fri Apr 23, 2010 6:59 pm 
Newbie

Joined: Fri Sep 29, 2006 6:35 pm
Posts: 6
Paraphrasing from your example, this works as straight SQL in postgresql:
Code:
select * from entity_status a
where a.updated =
(select max(updated) from public.entity_status b
where a.owner=b.owner)


Seems like this should translate into HQL as you've stated, except you have an extraneous distinct m if I'm not mistaken.


Top
 Profile  
 
 Post subject: Re: Find all latest
PostPosted: Sat Apr 24, 2010 4:38 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
thanks...

Another question on the same subject. I want to include a 'latestStatus' property for the Entity class. Is it possible to map a 'latest status' property using a HQL query, like...
Code:
   public class EntityStatus {
       private int OwningEntityID
       private int StatusID
       private java.util.Date StatusUpdated
   }

   public class Entity {
       private EntityStatus LatestStatus;
   }

.... where the Entity.latestStatus property is mapped to a HQL query, like...
Code:
   select a from EntityStatus where a.owningEntityID =? and s.statusUpdated = (select max(b.statusUpdated) from EntityStatus as b where b.owningEntityID=?)


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.