-->
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: Most effecient way to count rows
PostPosted: Sat Feb 28, 2009 10:46 am 
Newbie

Joined: Sun Nov 23, 2008 2:32 pm
Posts: 4
Hi,

So I have two tables called "kiosk" and "venue". A venue can have many kiosks. I want to be able to count how many kiosks are at a given venue. Now this is really simple in SQL by...

Code:
select count(*) from kIOSK_INFORMATION where venueId=2;

This returns 4 (which is the case for venueId 2).

Now using hibernate I came up with something similar to this...

Code:
Venue venue = (Venue) session.load(Venue.class, id);

int count = venue.getKiosks().size();


So this works but the SQL generated is ...
Code:
Hibernate: select venue0_.id as id7_0_, venue0_.openTimeWeekDay as openTime2_7_0_, venue0_.closeTimeWeekDay as closeTim3_7_0_, venue0_.openTimeWeekEnd as openTime4_7_0_, venue0_.closeTimeWeekEnd as closeTim5_7_0_, venue0_.businessName as business6_7_0_, venue0_.ownerName as ownerName7_0_, venue0_.venueStreetAddress1 as venueStr8_7_0_, venue0_.venueStreetAddress2 as venueStr9_7_0_, venue0_.venueCity as venueCity7_0_, venue0_.venueState as venueState7_0_, venue0_.venueZip as venueZip7_0_, venue0_.venueTel1 as venueTel13_7_0_, venue0_.venueTel2 as venueTel14_7_0_, venue0_.venueFax as venueFax7_0_, venue0_.venueEmail1 as venueEmail16_7_0_, venue0_.venueEmail2 as venueEmail17_7_0_, venue0_.venueSize as venueSize7_0_, venue0_.manager1 as manager19_7_0_, venue0_.manager2 as manager20_7_0_, venue0_.bartender1 as bartender21_7_0_, venue0_.bartender2 as bartender22_7_0_, venue0_.bartender3 as bartender23_7_0_, venue0_.bartender4 as bartender24_7_0_, venue0_.comments as comments7_0_, venue0_.installDate as install26_7_0_, venue0_.removeDate as removeDate7_0_, venue0_.operatorId as operatorId7_0_ from VENUE venue0_ where venue0_.id=?
Hibernate: select kiosks0_.venueId as venueId1_, kiosks0_.id as id1_, kiosks0_.id as id3_0_, kiosks0_.serialNumber as serialNu2_3_0_, kiosks0_.preparedBy as preparedBy3_0_, kiosks0_.containerID as containe4_3_0_, kiosks0_.usCustomsArrivalDate as usCustom5_3_0_, kiosks0_.usCustomsClearedDate as usCustom6_3_0_, kiosks0_.warehouseDeliveryDate as warehous7_3_0_, kiosks0_.preparationDate as preparat8_3_0_, kiosks0_.ipAddressUSA as ipAddres9_3_0_, kiosks0_.ipAddressUK as ipAddre10_3_0_, kiosks0_.operatorId as operatorId3_0_, kiosks0_.venueId as venueId3_0_, kiosks0_.routerId as routerId3_0_, kiosks0_.adslId as adslId3_0_, kiosks0_.model as model3_0_ from KIOSK_INFORMATION kiosks0_ where kiosks0_.venueId=?


Is there a better way to do this using the hibernate API? meaning is there a way to tell hibernate to do a count query? I know I can just use createSQLQuery but if I can get hibernate to do it directly it would be better.

Thanks for any help


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2009 1:39 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
You should do that with a count() in HQL because .size() initializes the whole collection which could be very bad if there is a large number of objects.

Code:
Long count = (Long)session.createQuery("select count(v.kiosks) from Venue v where v.venueId = :id).setLong("id",yourId).uniqueResult();


Rating is welcome


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.