-->
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.  [ 5 posts ] 
Author Message
 Post subject: Escaping characters in strings
PostPosted: Tue Sep 09, 2003 3:27 pm 
Newbie

Joined: Tue Sep 09, 2003 12:49 pm
Posts: 8
Is there a standard way to escape SQL characters like apostrophes or comment characters, when passing in strings to HQL queries? They seem to be escaped properly when mapped properties have them, and are updated or inserted, but I haven't found any exposed API for it for session.find()...

jea


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2003 5:41 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Could you give an example of an escape sequence you're attempting that fails?

Also, note that in quoted Java Strings you have to escape first for the compiler, then for whatever entity you're sending the string to. For example, to create a regex with escape sequences, you have to first escape the escape character. i.e.

Code:
Pattern.compile("\\s*\\d+")


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2003 10:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Apostrophes can be escaped using '' (ie. two apostrophes)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2003 9:35 am 
Newbie

Joined: Tue Sep 09, 2003 12:49 pm
Posts: 8
I got it worked out. I had tried switching to using Query with setString rather than building a String for session.find, and for some reason, I didn't think it was escaping the apostrophes in the parameters, but it does. Just a brain fart.

jea


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 7:15 am 
Newbie

Joined: Fri Sep 26, 2003 1:50 pm
Posts: 9
I am also having some difficulty using single and double quotes in HQL. What I have found (following Gavin's advice above) is that '' works only when the string is surrounded by ", and "" works only when the string is surrounded by '. Is it possible to write a query string that includes both ' and " ? It would be nice to be able to escape ' and " in the same way, without having to be concerned with how the string is quoted.

I have found that Hibernate nicely handles ' and " when they are used in strings passed as parameters to a prepared statement. The reason I am currently formulating the query as a complete string (not as a prepared statement) is that there are several optional parts in the query. My first attempt at a prepared statement for this turned out to take twice as long to execute (since the prepared statement includes all parts of the query I suppose). I might try the criteria interface.


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