-->
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.  [ 4 posts ] 
Author Message
 Post subject: Aggregate functions: sum
PostPosted: Wed Aug 31, 2005 4:47 pm 
Newbie

Joined: Wed Aug 31, 2005 4:32 pm
Posts: 13
I'm trying to run the following query on a MySQL InnoDB database (MySQL InnoDB dialect set) (just an example):

select sum(m.id) from markers m

where markers is a table for mapped entities and id is an integer field in that table.

<class name="controller.dto.MarkerDTO" table="MARKERS">
<id name="id" type="int" column="id">
<generator class="hilo">
<param name="table">marker_dto_hi_value</param>
<param name="column">marker_dto_next_value</param>
<param name="max_lo">100</param>
</generator>
</id>

etc

I get the following exception:

java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.AggregateNode
\-[AGGREGATE] AggregateNode: 'sum'

at org.hibernate.hql.ast.SelectClause.initializeExplicitSelectClause(SelectClause.java:136)
at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:440)
at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:351)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.afterQuery(HqlSqlBaseWalker.java:126)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:471)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)

so on

I looked into hiberante sources and found that the sum() function doesn't have it's own return value (which is reasonable), and according to the logic should take it from its first child node in the AST tree. But getFirstChild() returns NULL (here I think is the problem). The package I've downloaded doesn't contain sources for AST functions (at least I didn't find them), so I cannot figure out why it doesn't work properly.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 5:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is working perfectly for me in HB 3.1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 6:21 pm 
Newbie

Joined: Wed Aug 31, 2005 4:32 pm
Posts: 13
Thanks for your reply, but upgrading from 3.0 to 3.1 didn't help.

Anyway, I think I can make it work via ODBC direct connection or maybe switch to MySQL 5.0 and use views...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 6:29 pm 
Newbie

Joined: Wed Nov 29, 2006 6:18 pm
Posts: 1
Location: Barcelona
I had the same problem working with hibernate 3.0.5 and MySQL 5 but I used the following code as a workaround:
Code:
Session session = sessionFactory.getCurrentSession();

         SQLQuery q = session.createSQLQuery("select sum(u.visitas) as total "
               + "from estadisticasVisitasAUsuario u "
               + "where u.idUsuario = :idUsuario");
         q.setInteger("idUsuario", idUsuario);
         q.addScalar("total",Hibernate.LONG);
         Object o = q.uniqueResult();
         return ((Long) o).longValue();


Mapping conf:

Code:
<class name="EstadisticasVisitasAUsuario" table="estadisticasVisitasAUsuario">
     
      
       <composite-id
        name="pk"
        class="data.dto.EstadisticasVisitasAUsuarioPK" >
       
        <key-property name="idUsuario" column="idUsuario" type="integer" />
        <key-property name="dia" column="dia" type="short"/>
        <key-property name="mes" column="mes" type="short"/>
        <key-property name="anio" column="anio" type="integer"/>
       
       </composite-id>
       
      <!-- Normal properties of a java class -->
     
       <property name="visitas" column="visitas" type="long"></property>
     
   
    </class>


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