aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate how to make a count and group by Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate how to make a count and group by" Watch "hibernate how to make a count and group by" New topic
Author

hibernate how to make a count and group by

Natt Gonzalez
Ranch Hand

Joined: Apr 28, 2008
Posts: 31
Hi! I'm new using Hibernate and I want to make this query in hibernate:

select count(*) from (select ID from myTable where fieldId =196135 group by ID)

I wrote this code




And the result is what I want but I think there could be another way to do this.
The sql query this generates is :


Help me please!
Thanks you very much
Andreas Erber
Greenhorn

Joined: Oct 03, 2009
Posts: 20
Hi Natt,

Natt Gonzalez wrote:Hi! I'm new using Hibernate and I want to make this query in hibernate:

...


I don't really think I get what you intend to do...

What do you actually want to count? It looks like you want to count all entries of myTable having fieldId =196135, since I suppose the field ID is unique, actually a PK. So why not use

??? Or did I miss something?

CU
Froestel
Natt Gonzalez
Ranch Hand

Joined: Apr 28, 2008
Posts: 31
Hi, thanks for your reply.You missed something because I didn't write my whole sql, sorry.

This is the query i would like to write in hibernate:

select count(*) from (select ID from myTable,secondTable where field2 = field1 and fieldId =196135 group by ID)

I want to count the number of ids returned by the subquery. MyTable and second table is a one to many relationship this is why I need the group by.

Thank you very much and sorry for my English!!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate how to make a count and group by