-->
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.  [ 6 posts ] 
Author Message
 Post subject: Query for SUM(IFNULL)
PostPosted: Fri Dec 14, 2007 12:58 am 
Newbie

Joined: Fri Dec 14, 2007 12:48 am
Posts: 4
I am trying to execute this query using HQL and it throws a NULL pointer exception

The query is this
<b>
select new list(SUM(IFNULL(customer.count,0))) from Customer customer
</b>

I am using MYSQL as the backend database. And this is the exception I get:

Code:
Caused by: 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.ConstructorNode.resolveConstructorArgumentTypes(ConstructorNode.java:101)
        at org.hibernate.hql.ast.tree.ConstructorNode.prepare(ConstructorNode.java:79)
        at org.hibernate.hql.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:840)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1956)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
        at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
        at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
        at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
        at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
        at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
        at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:92)




Thanks
K


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 14, 2007 9:52 am 
Beginner
Beginner

Joined: Tue Apr 24, 2007 12:53 pm
Posts: 28
I think that there's a problem with your HQL, what is the 'new list(' supposed to be for? Or more precisely, what are you trying to do in the query?


-B


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 15, 2007 10:44 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
The NPE looks like a bug in Hibernate to me. However, I agree with the previous poster that the new list(...) clause seems strange. As this is an aggregate query it will only return a single row with a single value. Why would you want to construct a custom object for that? I would try if

select SUM(IFNULL(customer.count,0)) from Customer customer

works or if it also throws the NPE?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 18, 2007 9:22 am 
Beginner
Beginner

Joined: Tue Apr 24, 2007 12:53 pm
Posts: 28
I think that the NPE is caused by an invalid (or rather unsupported) function being used in the HQL query. IFNULL is a not supported function in any of the MySql Dialect (I checked all those named MySQL...class). It appears to trying to be sum the IFNULL return, but it first has to map it to its return type. Since IFNULL doesn't exist, the value is null and NPE is thrown.


I agree that the NPE might be a bug, but if it's being caused by what I think it's being caused by, then instead of an NPE at this point, you'd get a IllegalArgumentException, or UnsupportedOperation... Not an NPE, but still an exception.

-B


Top
 Profile  
 
 Post subject: NVL
PostPosted: Sun Dec 23, 2007 2:41 am 
Beginner
Beginner

Joined: Sat Nov 19, 2005 11:54 am
Posts: 22
I Think you should use
select SUM(NVL(customer.count,0)) from Customer customer


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 9:00 am 
Newbie

Joined: Thu Aug 16, 2007 3:02 am
Posts: 12
Location: Banglore
Hi,
Use either coalesce () or nullif() for null check.

_________________
sirajulmuneer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.