-->
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.  [ 1 post ] 
Author Message
 Post subject: Cannot figure out how to implement complex query
PostPosted: Thu Nov 12, 2009 1:36 pm 
Newbie

Joined: Thu Aug 07, 2008 9:40 am
Posts: 1
I have an entity OfficeView. Each OfficeView has many Devices.

Code:
public class OfficeView implements java.io.Serializable {

   private Integer officeId;
   private List<Devices> devices;

   @OneToMany(mappedBy = "officeView", fetch = FetchType.EAGER)
   public List<Devices> getDevices() {
      return devices;
   }

   public void setDevices(List<Devices> devices) {
      this.devices = devices;
   }


The Devices data sometimes contain more than one entry per device, and in this case I only want to fetch the most recent device. I can do this using this very complex sql query (mysql):

Code:
select t.* from
  (select dev_id, dev_name, max(last_update) maxValue from devices
  where dev_name like 'ssa-%' group by dev_name) x
join devices t on x.dev_name=t.dev_name
and x.maxValue=t.last_update


I can join this with the office_view table like this

Code:
select o.addr1, o.city, o.state, o.site_code, d.dev_name
from office_view o join
(select t.* from
(select dev_id, dev_name, dev_serial_num,
dev_platform, max(last_update)
maxValue from devices
where dev_name like 'ssa-%' group by dev_name) x
join devices t on x.dev_name=t.dev_name
and x.maxValue=t.last_update) d
on d.office_id=o.office_id
where o.office_id=:id


How the heck do I do this in Hibernate?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.