-->
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: How to make a Set to contain only the last 50 elements?
PostPosted: Wed Nov 22, 2006 1:22 pm 
Newbie

Joined: Wed Nov 22, 2006 1:17 pm
Posts: 5
Hi everyone!

I have a Set<Event> in a persistent object. Is there a way to let only the last 50 Events to be loaded? I have a whole mess of them, and loading all of those would be a bit too slow.

I see there's Criteria.setMaxResults and the "where" attribute for Set. But what I don't know is how to apply criterias to Sets, or how to select only the last X elements instead of the first ones.

Any advice?

Thanks in advice!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 2:48 pm 
Newbie

Joined: Wed Aug 16, 2006 7:30 pm
Posts: 7
You could always use 'order by' on whatever your field is that determines the last entries and sort desc instead of the default asc. Then if you take the first 50 entries you are effectively taking the most recently added ones (ie. the last 50 entries).

Either that or you'd have to do something where you get a count of the rows in the table and then just start from count - 50 when you get results back (this is the more yucky one though).

egregory


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 2:53 pm 
Newbie

Joined: Wed Nov 22, 2006 1:17 pm
Posts: 5
egregory wrote:
You could always use 'order by' on whatever your field is that determines the last entries and sort desc instead of the default asc.

Hmm, the documentation said that this feature is only for Java 1.4. I'm using 1.5, so I thought I wouldn't be able to use it. But maybe it should read "Java 1.4 or later".

Still, I don't know how to set the 50 row limit for the <Set>...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 3:00 pm 
Newbie

Joined: Wed Aug 16, 2006 7:30 pm
Posts: 7
criteria.setMaxResults(50)
should do it.

Clarification: you can set the max results on the query itself but not on the Set.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 3:06 pm 
Newbie

Joined: Wed Nov 22, 2006 1:17 pm
Posts: 5
Well, I'm a total newbie in Hibernate, so this may sound stupid - but it's Hibernate that loads the <Set> for me, right? I don't know how to control that...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 3:15 pm 
Newbie

Joined: Wed Aug 16, 2006 7:30 pm
Posts: 7
Ahh I see, misread the original question sort of. You have an Entity that has a Set<Event> in it and only want to load 50 of what the set contains. As far as I know it's an all or nothing proposition if you have a Set in an Entity. ie. you can eager/lazy load, but not only load part of a Set.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 4:40 pm 
Newbie

Joined: Wed Nov 22, 2006 1:17 pm
Posts: 5
Oh, that's unfortunate. I guess I'll have to figure out how to do this manually (using HQL or something)...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 4:57 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Depending on the database you are using, you can use the where attribute of the set node to limit the results. For instance in Oracle:

Code:
...
<set name="events" where="rownum <= 50" order-by="column_name desc">
...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 6:18 pm 
Newbie

Joined: Wed Nov 22, 2006 1:17 pm
Posts: 5
There's no rownum in MySQL, which I'm using. Is there a workaround?


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.