-->
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: Native SQL Queries - Aggregate Functions
PostPosted: Tue Dec 30, 2003 4:52 am 
Newbie

Joined: Tue Dec 30, 2003 4:41 am
Posts: 3
Location: Melbourne
I am using the following Native SQL query:

Code:
String query = "SELECT COUNT(*) AS {rowcount} FROM STUDENT WHERE NAME='Coolio'";


To execute using hibernate, i understand that i am meant to use session.createSQLQuery()

This method takes 3 parameters:

String (the query)
String (the identifier..."rowcount" in this instance)
Class (the returned class)

My question is in this case, what should my returned class be?

Code:
Query q = session.createSQLQuery(query, "rowcount", THE_CLASS);


Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 7:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Why don't you use HQL for this, you can easily do a query like this:

Code:
SELECT count(*) FROM Student s WHERE s.name="whatever"

The .createSqlQuery is meant for querying for Hibernate mapped classes using SQL., which you don't do. If you really want to use SQL for that, you can get the SQL connection using session.getConnection() and execute the query manually.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 11:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
As per the documentationt the createSQLQuery only supports returning objects at the moment - not scalar results.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2003 8:08 am 
Newbie

Joined: Tue Dec 30, 2003 4:41 am
Posts: 3
Location: Melbourne
OK Max (and gloeglm), thanks for your quick reply.

I have modified my application to use gloeglm's recommendation and it appears to be working fine.


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.