-->
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.  [ 4 posts ] 
Author Message
 Post subject: Calling size() on List causes multiple SELECTs
PostPosted: Wed Apr 28, 2004 4:06 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
Hello,

I have Hibernate queries returning Lists of my POJOs from DB.
I would like to check the size of the returned List, so I can use it for "showing XXX matches" messages in my application.

I can call size() on returned Lists and get that count, but that causes Hibernate to load each POJO by running a separate SELECT for each POJO in the List. This is, obviously, bad, and I am wondering if there is a way to instruct Hibernate not to do that when I call size()?

If that cannot be done, how do people handle situations like this?

Do you just have 2 DB calls, one with COUNT(*) returning the count as Integer, and the second one that that actually returns the List?

I am using Hibernate 2.1.1.

Thanks,
Otis


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 28, 2004 5:30 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
try this:


How can I find the size of a collection without initializing it?

( (Integer) s.createFilter( collection, "select count(*)" ).iterate().next() ).intValue()


in the FAQ:

http://www.hibernate.org/118.html#A4


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 1:53 am 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
lagcisco wrote:
try this:


How can I find the size of a collection without initializing it?

( (Integer) s.createFilter( collection, "select count(*)" ).iterate().next() ).intValue()


in the FAQ:

http://www.hibernate.org/118.html#A4


Yes, I have tried that.
However, that requires me that I change all return types from my DAOs from List to some object that includes both the List and the Integer count. :(

Or, I guess, this could be a generic method that just returns size of any collection?

Thanks,
Otis


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 12:54 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
eh ?! What can be more generic than ( (Integer) s.createFilter( collection, "select count(*)" ).iterate().next() ).intValue()

It takes a collection into parameter.

_________________
Emmanuel


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