• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hibernate how to make a count and group by

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!

reply
    Bookmark Topic Watch Topic
  • New Topic