-->
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: Need help using count (aggregation)
PostPosted: Wed Sep 20, 2006 3:18 pm 
Newbie

Joined: Wed Sep 20, 2006 2:57 pm
Posts: 16
Location: Encinitas, CA
Hi all. Did some searching, couldn't find answer to total newbie question.

Hibernate version: 3.1
I'm using Eclipse and Java 5.

I have one question and one comment:
1. How do I use the count function.
2. Criticism of section 7.4.2 of Hibernate in Action


1. After reading chapter 7, I tried writing this code to get a count of records in a table that had a boolean field set to true of false:

Query query = session.createQuery(
"select count(sample.isStored = :status) from sample");
query.setEntity("status", stored);
assert query.list().size() == 1;
return (Integer)query.list().get(0);

Could someone explain a good fast way(s) to get record counts?


2. Having a bad day on a crunch project, so sorry for the orneriness, but to the authors: "how about complete code examples?" Soon this will all be tirvial for me, but look at the confusion this newbie experienced and consider how your books could be better by using complete examples.

E.g., the paragraph that talks about the sum function indicates that "the query returns a BigDecimal." So there I am looking all over the place for how exactly that works. Go ahead, insult my intelligence and show me how in your books. I'm guessing that I get back a row of Object's and then I cast the first one, etc., but the key word here is "guessing" and not "reading."


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 20, 2006 7:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
1) select count(*) from sample s where s.isStored = :status

2) I think it is difficult for authors to deal with a wide potential audience. I prefer reference books and hate the books that have large sections of very basic code samples. On the other hand I can see how this could help beginners. This provides opportunities for multiple books being successful in this topic space.

Often the wiki is a good place to look, and much of the online manual is very good.


Top
 Profile  
 
 Post subject: Related question about count aggregation
PostPosted: Thu Sep 21, 2006 9:38 pm 
Newbie

Joined: Wed Sep 20, 2006 2:57 pm
Posts: 16
Location: Encinitas, CA
Thank you, David, for the help and thoughtful comments despite my grumpy attitude.

Could you advise me how I could count the number of records that have no "child" records under them?

Say I have a Carton table and it has Item children that point at it. In Java, the Carton class simply has a list or set that references Item objects.

I wish to count the number of empty Carton's. Alternatively, I wish to count the Carton's that have at least one Item. Is this possible using count() in HQL?

I wish I could write something like
select count(*) from Carton c where c.items.size() == 0

or
select count(c.items) from Carton c

or?

Thanks,
BE


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 2:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Your close - Hibernate includes a number of reserved word in HQL for dealing with lists. For example; You may test the size of a collection with the special property size, or the special size() function.

from Cat cat where cat.kittens.size > 0

from Cat cat where size(cat.kittens) > 0

Then its just prepending 'select count(*) '. Check out the manual for further information
http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html and maybe its a good idea to install the hibernate tools and try the query tool out.


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.