Hibernate version: 2.1.4
Database: PostgreSQL
Looks like there is a problem with multiplications inside aggregation functions (select sum( a * b)...)
(I am using Spiring's HibernateTemplate to run this query)
Code:
private static final String RECOMENDATIONS_QUERY =
"select ra.recommendationId, sum(ra.allocation * ra.allocation) " +
"from " + RecommendedAllocation.class.getName() + " ra " +
"group by ra.recommendationId";
public List getRecommendationDetails() {
return this.getHibernateTemplate().find(RECOMENDATIONS_QUERY);
}
Hibernate generates valid SQL:
Code:
select recommende0_.recommendation_id as x0_0_,
sum(recommende0_.allocation*recommende0_.allocation) as x1_0_
from recommended_allocations recommende0_
group by recommende0_.recommendation_id
I am getting NullPointerException from postgress when resultset is wrapped into array of objects.
The problem is in SelectParser.token() method. It has a special treatment for '*' inside aggreagte function. i.e. '*' inside aggregation considered as count(*) and new scalar column added to query. Later Loader tryes to get value for this non-existing column which causes NullPointerException.
Changing '*' into '+' or '/' in the original query fixes the problem.
Stack trace below:
java.lang.NullPointerException
at org.postgresql.util.PSQLException.getSQLState(PSQLException.java:227)
at net.sf.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:38)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:230)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:955)
at net.sf.hibernate.loader.Loader.list(Loader.java:946)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:846)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1543)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at org.springframework.orm.hibernate.HibernateTemplate$17.doInHibernate(HibernateTemplate.java:331)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:150)
at org.springframework.orm.hibernate.HibernateTemplate.executeFind(HibernateTemplate.java:170)
at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:328)