-->
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: How to design a Hibernate Query for this case?
PostPosted: Sun Oct 11, 2009 11:50 am 
Newbie

Joined: Sun Oct 11, 2009 11:48 am
Posts: 2
In a MySQL DB / Table livedata_tbl has two columns: Device_Id (numeric) & Stored_At (Timestamp). Which represents Device POJO Class, which has deviceId as Integer and storedAt as Timestamp.

Records are stored into this database very frequently, and each Device_Id can repeat multiple times in this table.

But the requirement is, How to select each unique device_id with latest timestamp. This query has to written in Java.

In other words, we need to select unique Device Object based on their individual latest timestamp value.

Any thoughts?


Top
 Profile  
 
 Post subject: Re: How to design a Hibernate Query for this case?
PostPosted: Sun Oct 11, 2009 4:54 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi deararsaiuk,

Pre: Using Device as the name of the entity that is mapped to table livedata_tbl.
Code:
session.createCriteria(Device.class)
  .setProjection(Projections.projectionList()
    .add(Projections.groupProperty("deviceId"))
    .add(Projections.max("storedAt"))
  ).list();

Don't know if it works - but it's at least close. Build some proper session and transaction management around and play with it. I'm sure, you'll figure it out.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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.