-->
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.  [ 9 posts ] 
Author Message
 Post subject: complex query
PostPosted: Thu Mar 25, 2004 2:38 pm 
Newbie

Joined: Thu Mar 25, 2004 2:32 pm
Posts: 6
Hi all,

I am having trouble to get this query to work, and wondering if you all more experienced hibernate users can shed some light on this.

Let's say I have a class that holds, say, cvs files and their versions:

public class CvsFile
{
private String fileName;
private int version;
...
}

How do you write a query to return all the CvsFile objects for each file and its maximum version?

For example, if I have

aaa 1
aaa 2
aaa 3
bbb 1
bbb 2

I want to return

aaa 3
bbb 2

Any ideas?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 3:43 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
select max(c.version), c from CsvFile c
group by c.version

this will return a list of Object[], just retrieve Object[1] from each result

there must other ways to do it...

take a look at "sql query" for example


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 4:32 pm 
Newbie

Joined: Thu Mar 25, 2004 2:32 pm
Posts: 6
Hmm, I actually figured it out:

from CvsFile c
where c.version =
(select max(c2.version)
from CvsFile c2
where c.fileName = c2.fileName)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 5:16 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
delpouve's way might be a tad more efficient... :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 5:34 pm 
Newbie

Joined: Thu Mar 25, 2004 2:32 pm
Posts: 6
and a tad incorrect too... :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 5:45 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
details, details...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 5:59 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
have you tried sql "style" query?
it seems to be the easier way to do it


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 6:35 pm 
Newbie

Joined: Thu Mar 25, 2004 2:32 pm
Posts: 6
Well, in this case, HQL is actually pretty close to SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 6:37 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
can you give what sql is logged with my and your hql query?
this can help us


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.