Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange results adding a SUM to my query,

 
David Rocks
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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.
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • 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
  • 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



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic