nhibernate version => 1.0.1.0
hi.
i'me trying to get a sum of values. something that in sql would simply be blabla, sum(***) from ****
i've looked into the documentation and although i don't see anything about aggregation in
http://www.hibernate.org/hib_docs/nhibernate/html/ (NHibernate Documentation 1.0.2.0) i'me trying to use some (java) hibernate examples
1.
so i have this statement
HQL
Code:
select m.ActividadeContratada.EntidadePagadora.Id, sum(valor)
from Infos.GestaoRevistas.Core.Movimento m
where m.ActividadeContratada.Id={0}
and m.Donativo=1
group by m.ActividadeContratada.EntidadePagadora.Id
and use this code to get the data
Code:
q = repositorio.Session.CreateQuery(queryString);
r = q.List();
but when i do something like
Code:
=> ? q.List()
{Count=1}
[0]: {1000001} ==> ????
i get only 1 column! the id and no sum... what am i doing wrong here??
while searching for a solution i came across 2 questions that i don't know if they are bugs or are by design but i certainly wasn't excepting the result i got
2.
this statement
HQL
Code:
select m.ActividadeContratada.EntidadePagadora.Id, sum(m.valor)
from Infos.GestaoRevistas.Core.Movimento m
where m.ActividadeContratada.Id={0}
and m.Donativo=1
group by m.ActividadeContratada.EntidadePagadora.Id
gets transformed into this
SQL
Code:
select actividade1_.idEntidadePagadora as x0_0_, sum(m.valor) as x1_0_
from movimentos movimento0_, ActividadesContratadas actividade1_
where movimento0_.idActividadeEntidade=actividade1_.id and ((movimento0_.idActividadeEntidade=40689)and(movimento0_.Donativo=1))
group by actividade1_.idEntidadePagadora
==> ERROR - The column prefix 'm' does not match with a table name or alias name used in the query.
notice "sum(m.valor) as x1_0_"... I imagined that since i had defined m as an alias of Infos.GestaoRevistas.Core.Movimento then the nhibernate engine would know that it belong to something in the Infos.GestaoRevistas.Core.Movimento scope (meaning not necessarily that specific object but maybe some related object)
3.
HQL
Code:
select sum(valor)
from Infos.GestaoRevistas.Core.Movimento m
where m.ActividadeContratada.Id={0}
and m.Donativo=1
group by m.ActividadeContratada.EntidadePagadora.Id
SQL
Code:
select movimento0_.id as id, movimento0_.loginCriacao as loginCri8_, movimento0_.DataCriacao as DataCria7_, movimento0_.idEntrada as idEntrada, movimento0_.Donativo as Donativo, movimento0_.TaxaIvaAplicavel as TaxaIvaA5_, movimento0_.idActividadeEntidade as idActivi2_, movimento0_.ValorIva as ValorIva, movimento0_.Valor as Valor, movimento0_.loginActualizacao as loginAct9_
from movimentos movimento0_, ActividadesContratadas actividade1_
where movimento0_.idActividadeEntidade=actividade1_.id and ((movimento0_.idActividadeEntidade=40689)and(movimento0_.Donativo=1))
group by actividade1_.idEntidadePagadora
==> ERROR - Column 'movimento0_.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
here i was trying to get only the sum value since that's only what i want but looks like something is missing too...