Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate how to make a count and group by

 
Natt Gonzalez
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic