-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Query not returning the type of Object I expect
PostPosted: Wed Feb 11, 2009 4:14 pm 
Newbie

Joined: Sun Mar 27, 2005 1:22 pm
Posts: 15
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

Quote:
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..

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


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


but get..

Quote:
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

Quote:
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.

I realise that the SUM does not work becasue the query is not returning UserBetCompetitionHistory objects. Can anyone help me by telling me why an arraylist of User objects is being returned and hopefully can anyone help me to get the SUM working?

Thanks.

Hibernate version: 3.0


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 5:11 pm 
Newbie

Joined: Sun Mar 27, 2005 1:22 pm
Posts: 15
hmm, i think I have got a bit further with

Quote:
Query query = session.createQuery( "select new UserBetCompetitionHistory( user , odds_won ) from UserBetCompetitionHistory as" +
" compHist" ) ;


Not working yet but i'll give it another flog.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 5:32 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Can anyone help me by telling me why an arraylist of User objects is being returned


Well, your query do say 'select compHist.user...' which is a User object, so it is very expected that this should return a list of User objects. There are lots of examples in the Hibernate documentation http://www.hibernate.org/hib_docs/v3/re ... elect.html

For the query, when you need separate sum() for each user you also need to 'group by' the user. Try something like:

Code:
select user, sum(compHist.ODDS_WON) from UserBetCompetitionHistory as compHist
join compHist.user as user
group by user.id, .... <-add ALL user properties as a comma-separated list


See http://www.hibernate.org/hib_docs/v3/re ... uping.html for more information about the group by clause.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.