• 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

Strange results adding a SUM to my query,

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have an object(table) UserBetCompetitionHistory(user_bet_competition_history ), that in turn holds an object called User and a double value called ODDS_WON

TO get all my UserBetCompetitionHistory objects I use

Query query = session.createQuery( "from UserBetCompetitionHistory as compHist" ) ;



And that works fine but I want to change it to this (working correctly) mySQL query in..

SELECT user_id , SUM(odds_won) FROM user_bet_competition_history group by user_id ;



Query query = session.createQuery( "select user , SUM(ODDS_WON) from UserBetCompetitionHistory as compHist" ) ;



but get..

java.lang.NullPointerException
at org.hibernate.dialect.Dialect$3.getReturnType(Dialect.java:102)
at org.hibernate.hql.ast.util.SessionFactoryHelper.findFunctionReturnType(SessionFactoryHelper.java:382)
at org.hibernate.hql.ast.tree.AggregateNode.getDataType(AggregateNode.java:21)
at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:143)
at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:705)
at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:529)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:645)



But the really weird thing is that if I use this line

Query query = session.createQuery( "select compHist.user from UserBetCompetitionHistory as compHist" ) ;



I get a collection of User objects rather than a collection of UserBetCompetitionHistory objects when I start the HQL query at "from.

Is this normal? Am I doing something wrong?

Can anyone help me to get the SUM working?

Thanks.
 
Author
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if you define a projection, you'll get a collection of arrays, where each array element represents each item in the projection.
 
David Rocks
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take it that the SUM() is not working because I am somehow electing to bring back an User ArrayList. I have never put a select on the front of a HQL query before.

Is it possible just to get back a UserBetCompetitionHistory collection and still have "SELECT user_id , SUM(odds_won) " on the front of the query?

Thanks



 
reply
    Bookmark Topic Watch Topic
  • New Topic