• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

group by

 
Joe Jose
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please tell me how can I use a group by clause in hibernate ? Can I map a key for the group by clause ?

I am using the query like :
select work.deptId,work.workId from WorkBasedQuote work where work.quoteNo = 123 group by work.deptId,work.workId

my dao:


How can I access the deptId and workId ?
Please help me .

Thanks,
Joe
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HQL supports group by, and its syntax is pretty much the same a SQL (which the exception that you can group by an entire object, as well as individual properties). Is you code not working?
 
Joe Jose
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
HQL supports group by, and its syntax is pretty much the same a SQL (which the exception that you can group by an entire object, as well as individual properties). Is you code not working?


The above code I have given is working. But can I represent the group by in hbm.xml like order-by ?

Thanks,
Joe.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to my knowledge. I'm not sure why you would do this - isn't aggregating only really used in specific queries? Why would you want to map your objects to always use a group by?
 
Joe Jose
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
Not to my knowledge. I'm not sure why you would do this - isn't aggregating only really used in specific queries? Why would you want to map your objects to always use a group by?


How can I get data group by something from the database ? You said we can get a whole object using group by ? How can we ?
Could you please give me an example ?

I have given the query like this

" select new WorkBasedQuote(work.quoteNo,work.deptId,work.workId,work.int_qt_val) from WorkBasedQuote work where work.quoteNo = ? group by work.deptId,work.workId "

and in the dao :



Can it be taken as a list of 'WorkBasedQuote' objects ?

Thanks,
Joe.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How can I get data group by something from the database

Just as you might in SQL. See the example in the documentation.


You said we can get a whole object using group by ? How can we ?

By writing "group by [object] " at the end of the query. This will only work if all properties in the object are aggregated.
 
Joe Jose
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:

By writing "group by [object] " at the end of the query. This will only work if all properties in the object are aggregated.


Thanks for the reply Paul.

I think it works like :
" select new WorkBasedQuote(work.deptId,work.workId,work.int_qt_val) from WorkBasedQuote work where work.quoteNo = ? group by work.deptId,work.workId,work.int_qt_val "

But need more testing .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic