-->
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.  [ 7 posts ] 
Author Message
 Post subject: Using createSQLQuery to return strings
PostPosted: Sun Jan 11, 2004 7:50 pm 
Newbie

Joined: Sat Nov 22, 2003 8:42 pm
Posts: 9
Hi,
I have a query I could perfectly execute with JDBC which I need to return results in pages. The query must return a list of Strings. For example in MySQL:

Code:
"SELECT t.stringCol FROM my_table AS t order by t.stringCol LIMIT 10, 10"


As specifying a "LIMIT" is not standard I wanted to see if there was a way of adapting this to Hibernate's Query; something like:

Code:
String sqlQuery = "select t.stringCol from my_table as t order by t.stringCol"
Query query = session.createSQLQuery(sqlQuery, "", String.class);
query.setMaxResults(pageSize);
query.setFirstResult(pageSize*pageNumber);
List res = query.list();


In this case I'd get a "no persister for java.lang.String" error, but maybe I could create a dummy class for getting the string or an integer.
Any ideas would be appreciated.

Thanks in advance for your help!!

Andr


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 7:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
createSQLQuery can only return mapped classes. Use session.connection() to get the underlying JDBC connection and execute your query just like you would with JDBC.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 7:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Why don't you use a HQL query for this, you can use setMaxResult and setFirstResult for a query generated with session.createQuery()


Top
 Profile  
 
 Post subject: sqlQuery
PostPosted: Sun Jan 11, 2004 7:57 pm 
Newbie

Joined: Sat Nov 22, 2003 8:42 pm
Posts: 9
I know. What I mean to do is to create a fake mapped class with a String attribute to contain the value, so I can use the database independent features of Hibernte for paging:

query.setMaxResults(pageSize);
query.setFirstResult(pageSize*pageNumber);

That way I wouldn't be hardcoding the "LIMIT a,b" form MySQL.

I know createSQLQuery wasn't created for doing this, but maybe there is a workaround.

Thanks again,

Andr


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 7:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just use a HQL query ...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 8:00 pm 
Newbie

Joined: Sat Nov 22, 2003 8:42 pm
Posts: 9
The problem is that I generate some SQL tables dinamically and can't map them. So I need to use raw SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 8:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well, you could probably create a class mapped to any known table and then use it to keep the result of the createSQLQuery - just try it.


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