Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get a count query of a mapped table

 
Sverre Moe
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I get a count query of a mapped table?

I need to count the number of rows given a few parametres.



The value I want returned is a number not any mapped object or list. The number of rows where the table attributes corresponds to the param values.

I am using Spring and its hibernateTemplate with Hibernate mappings
[ January 16, 2008: Message edited by: Sverre Moe ]
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the aggregate functions in the Hibernate Reference.
 
Sverre Moe
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edvins Reisons:
See the aggregate functions in the Hibernate Reference.


That was allready clear. But, I had to create a query in the mapping file and access it through findByNamedQueryAndNamedParam. Is the no better way to get a count? If not I could just use JDBC to get the count in stead of going through Hibernate.
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried making that HQL count a named query and accessing it with that long-named method?
 
Sverre Moe
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edvins Reisons:
Have you tried making that HQL count a named query and accessing it with that long-named method?


Yes, and it worked. I was just hoping I could use the mapped object in some way instead of creating a named query. Or perhaps Hibernate is not meant to be that way. That all other than getting a complete object (one row) of data or a list of them should be accessed through named queries. Such as queries with mathematical operations.

Well well. All is good.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic