Hello,
I am trying to figure out how to execute a (native) SQL query and put
the result in a java variable:
select avg(datediff(dc.diffusionEPReelle, dc.accuseReceptionReelle)))
from E37T_T_DateCle ;
This query returns the mean number of days between two date columns,
and i need to gather this number of days.
(a) When executing this code :
Integer o = (Integer) (getSession().createSQLQuery(
"select avg(datediff(dc.diffusionEPReelle, dc.accuseReceptionReelle)))
as {delaiMoyen} from E37T_T_DateCle",
"delaiMoyen", Integer.class).uniqueResult() );
i'm getting the exception :
net.sf.hibernate.MappingException: No persister for: java.lang.Integer
(b) I have also tried to define a class ResultDelaiMoyen which only
contains two methods getDays() and setDays().
ResultDelaiMoyen o = (ResultDelaiMoyen)
(getSession().createSQLQuery(
"select avg(datediff(dc.diffusionEPReelle, dc.accuseReceptionReelle)))
as {delay.days} from E37T_T_DateCle",
"delay", ResultDelaiMoyen.class).uniqueResult() );
The exception becomes :
net.sf.hibernate.MappingException: No persister for: fr.paris.dsti.aea.services.statistiques.ResultDelaiMoyen
(Note: i haven't created any hbm.xml file for the class ResultDelaiMoyen
because there is no corresponding table in the database. )
(c) I've found an example using <return-scalar> , but this doesn't
work for me - the tag is not recognized. Probably because I'm
using hibernate 2.
So, my question is : how can I execute an arbitrary SQL query
in hibernate 2 ?
Thanks,
Adrian Maier
|