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

fetch Sum , count and entity with HQL + ClassCastException

 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi , all

How to get Sum and count aggregate functions with the hibernate entity corresponding to table.?

Is there any way ?

i am in a problem . let me solve this
Thanks.

Query is

select sum(txnlog.amount),count(txnlog.transactionId),txnlog from TransactionLog txnlog WHERE DATE_FORMAT(txnlog.txnDatetime, '%Y-%m-%d') = CURDATE()

thanks
Raza
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HQL supportes thes aggregare functions and their syntax is exactkly the same. So it should just work.

You mention a ClassCastException; can you post the stack trace?
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
18:39:06,136 INFO [STDOUT] Hibernate: select sum(transactio0_.amount) as col_0_0_, count(transactio0_.transaction_id) as col_1_0_, transactio0_.txn_datetime as col_2_0_, transactio0_.transaction_id as col_2_1_, transactio0_.merchantid as col_2_2_, transactio0_.txn_datetime as txn1_0_, transactio0_.transaction_id as transact2_0_, transactio0_.merchantid as merchantid0_, transactio0_.userid as userid0_, transactio0_.cardnumber as cardnumber0_, transactio0_.amount as amount0_, transactio0_.response_code as response7_0_, transactio0_.user_data as user8_0_, transactio0_.txn_status as txn9_0_, transactio0_.auth_code as auth10_0_, transactio0_.rrn as rrn0_, transactio0_.manual_txn_status as manual12_0_, transactio0_.booking_id as booking13_0_, transactio0_.notkt as notkt0_, transactio0_.remarks as remarks0_, transactio0_.mobileno as mobileno0_, transactio0_.pg_txnid as pg17_0_, transactio0_.pg_id as pg18_0_, transactio0_.user_data_1 as user19_0_, transactio0_.user_data_2 as user20_0_, transactio0_.user_data_3 as user21_0_, transactio0_.user_data_4 as user22_0_, transactio0_.user_data_5 as user23_0_, transactio0_.user_data_6 as user24_0_, transactio0_.user_data_7 as user25_0_, transactio0_.user_data_8 as user26_0_, transactio0_.user_data_9 as user27_0_, transactio0_.user_data_10 as user28_0_, transactio0_.channel_id as channel29_0_, transactio0_.channel_type as channel30_0_, transactio0_.attempts as attempts0_, transactio0_.stage as stage0_, transactio0_.digits as digits0_, transactio0_.rdigits as rdigits0_, transactio0_.enccardnum as enccardnum0_ from transaction_log transactio0_ where date_format(transactio0_.txn_datetime, '%Y-%m-%d')=curdate() group by transactio0_.transaction_id
18:39:06,183 INFO [STDOUT] list size is22
18:39:06,183 ERROR [STDERR] java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.atom.brs.entity.TransactionLog
18:39:06,183 ERROR [STDERR] at com.atom.brs.util.BrsProductFileGenerator.run(BrsProductFileGenerator.java:52)
18:39:06,183 ERROR [STDERR] at java.util.TimerThread.mainLoop(Timer.java:512)
18:39:06,183 ERROR [STDERR] at java.util.TimerThread.run(Timer.java:462)
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Chris Hurd
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are using aggregate functions in your query it is no longer returning instances of TransactionLog hence the cast exception. Instead try the following:



with each element in Object[] being a column in the select clause. Also you might want to look at your query, doing an aggregate on transactionId and then including it in your group by - if it works at all - will mean you always have a count of 1.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes , i think i should write another query to fetch sum and count .

As i created a List of TransactionLog entity which i am returning to action.

Thanks for you suggestion!

regards
Raza!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic