-->
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.  [ 12 posts ] 
Author Message
 Post subject: can HQL do substring
PostPosted: Mon Jan 19, 2004 2:07 pm 
Newbie

Joined: Mon Jan 19, 2004 1:54 pm
Posts: 8
Dear all,
I am new to Hibernate. I am doing a project which uses Hibernate as the bridge to access database.
My problem is:
The user will give a zip code range(either 3 or 5 digits), I will have to get the total pieces of mails grouped by the 3-digit or 5-digit zip code. My problem is the data(zip code) in the database is of size 5 digits or more. So I would like to use substring(zipcode from 1 for 3) or something similar in HQL. I can get the queries in SQL, but no luck with HQL. I've even tried using native SQL, nothing helps.
Anybody helps?

My HQL kinda looks like:
totalQuery="select count(doc.id) as total, " +
"substring(doc.addressBlockZip from 1 for " + fromZip.length()+ ") as zip " +
"from DocumentVO as doc " +
"group by zip " +
"having zip between '"+fromZip+"' and '"+toZip+
"' order by zip asc";


Thanks a lot
Jessica


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 2:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Can't do that. Use session.createSQLQuery()


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 3:10 pm 
Newbie

Joined: Mon Jan 19, 2004 1:54 pm
Posts: 8
I did try using native SQL.
I made the query very simple:
"SELECT {doc}.id" +" FROM Documents AS {doc} limit 10";/*Documents is the table in the database*/
I got following error:
java.sql.SQLException: Column 'id0_' not found.
...
I don't know why it's happening. My query could not be simpler.
Any thoughts?

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 3:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can onyl query for hibernate mapped classes using createSQLQuery. So you cant query for the id, only for full Documents


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 3:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You don't need to use a native SQL query as far as i can tell. Just use whatever SQL function your database provides for doing substrings.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 3:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Really? I thought something like substring(doc.addressBlockZip from 1 for " + fromZip.length()+ ") will not work (multiple parameters). Correct me if I am wrong ...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 3:42 pm 
Newbie

Joined: Mon Jan 19, 2004 1:54 pm
Posts: 8
Ok, since galvin mentioned that I can use substring function, I tried the following query(I can execute the query sucessfully in MySQL):

totalQuery="select count(doc.id) as total, " +
"substring(doc.addressBlockZip from 1 for " + fromZip.length()+ ") as zip " +"from DocumentVO as doc " +"group by zip " +"having zip between '"+fromZip+"' and '"+toZip+"' order by zip asc";

I got the following error messages:
net.sf.hibernate.QueryException: , expected in SELECT [select count(doc.id) as total, substring(doc.addressBlockZip from 1 for 3) as zip from DocumentVO as doc group by zip having zip between '333' and '444' order by zip asc]
...

Could you point out where I did wrong?

Again, many thanks!

Jessica


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 3:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Okay, what I know for sure is you can't use custom SQL functions in the SELECT part of the HQL. Only in the where part.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 4:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Something like substring(foo from 1 to 3) will not work, but substring(foo, 1, 3) will. (It can even be made to work in the select clause if you add support to the Dialect.)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 4:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually, scratch that - I think select clause SQL functions don't support multiple arguments yet.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2004 8:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I've been meaning to get back to this but lots of paid work and with the AST parser coming along (which makes this a snack to do) I have not addressed this issue (also need to deal with constants as arguments aswell).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 11:32 am 
Newbie

Joined: Tue Aug 24, 2004 6:47 am
Posts: 3
It works! (with a Oracle DB)

Example:
Query sqlQuery = session.createQuery("select substr(quiz.a_StartDate,1,2) from E_Quiz quiz");


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