-->
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.  [ 2 posts ] 
Author Message
 Post subject: Executing native sql queries
PostPosted: Fri Mar 31, 2006 3:01 am 
Newbie

Joined: Fri Mar 31, 2006 1:38 am
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 3:37 am 
Newbie

Joined: Fri Mar 31, 2006 1:38 am
Posts: 2
Sorry for the noise. I've found a solution for my problem.
I discovered the method that returns me the JDBC connection,
and i'm using that :

Connection conn = getSession().connection();
Statement stmt = conn.createStatement();
ResultSet r = stmt.executeQuery("select
avg(datediff(dc.diffusionEPReelle, dc.accuseReceptionReelle))
from E37T_T_DateCle dc");
while (r.next()) {
System.out.println("VAL="+r.getInt(1));
}



Cheers,
Adrian Maier


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