-->
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.  [ 11 posts ] 
Author Message
 Post subject: HQL: How use a parameter in a like %foo% expression?
PostPosted: Fri Jun 10, 2005 10:45 am 
Newbie

Joined: Tue Mar 29, 2005 5:52 am
Posts: 7
Hi. Is there any way in Hibernate 3 to use a unnamed parameter in a "like '%foo%'" part of a HQL where clause?

Example:

Code:
from MyClass myObject where myObject.field like '%?%'


If i try that exact syntax via the Spring HibernateTemplate i get this exception:

Code:
java.lang.IllegalArgumentException: No positional parameters in query: from MyClass myObject where myObject.field like '%?%'
   at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:171)
   at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:408)
   at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:745)
    ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 10:56 am 
Newbie

Joined: Thu Jun 09, 2005 2:49 pm
Posts: 6
You should pass the "%"s in with the positional parameter. Hibernate isn't recognizing your '?' as a positional parameter marker because it is in quotes.

Do this:
Code:
from MyClass myObject where myObject.field like ?


And then set the positional parameter in the normal way but with %'s added to the beginning and end.[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 10:59 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Or this seems to work for me:

Code:
from MyClass myObject where myObject.field like '%' || ? || '%'


I'm using named parameters, but that shouldn't make a difference.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 11:24 am 
Newbie

Joined: Tue Mar 29, 2005 5:52 am
Posts: 7
Good ideas - both work in my test code. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 12, 2005 10:32 am 
Newbie

Joined: Tue Jul 12, 2005 9:04 am
Posts: 13
ok... This work fine, except if I inject quote in the positional parameter.
How can I resolve it?!

ty

_________________
Elton Kuzniewski
http://www.eltonk.com.br


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 8:17 am 
Newbie

Joined: Tue Jul 12, 2005 9:04 am
Posts: 13
public SearchBean myMethod(String search){
...
}
where "search" = Mac'Donalds

Its don't works...
The solution is use Criteria, buts its really so difficult where we have a lot of relationship...

_________________
Elton Kuzniewski
http://www.eltonk.com.br


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 9:43 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Before passing the 'search' information to the Hibernate you could have an utility method that precedes the single quotes with backslash character.

For example Mac'Donalds becomes Mac\'Donalds. This worked for me, hopefully it should also work for you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 10:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
How do you "bind" the value [Mac'Donalds] into the query?

Using parameters? If so, Hibernate simply passes those values onto the jdbc PreparedStatement as-is and it is actually the resposnibility of the jdbc driver to ensure that parameter bind values are properly escaped. If that is not the case, then you have a very buggy jdbc driver.

If you do not really "bind" the value, and instead just use string concatenation to "inject" the valiue into the query string, then yes, you'd need to manually escape it first.

To be clear, the following will work:
session.createQuery( "from Eatery e where e.corporateEntity like ?" )
.setString( 1, "%Mac'Donalds%" )
.list();


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 11:08 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Steve, you are true. The jdbc driver was escaping respective information and I dont need to do specifically.

I have a question regarding the numbers used to bind positional parameters. Will it start from 0 or 1. In your above query you have been using

setString( 1, "Mac'Donalds" );

When I use '1' instead of '0', was getting the following exception.

Code:
java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
   at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:55)
   at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:61)
   at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:382)
   at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:362)


But when I use setString( 0, "Mac'Donalds" ) everything is working fine. I am confused especially with the exception message is conflicting to the number I used to bind parameters.

-----
an update - I was using setParameter( 1, "Mac'Donalds" );

and when I use setString( 1, "Mac'Donalds" ) the exception is as follows

Code:
11:10:19,151 DEBUG ErrorCounter:68 - throwQueryException() : no errors
java.lang.IllegalArgumentException: Positional parameter does not exist: 1 in query: from HibernatePojo myObject where myObject.name like ?
   at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:328)
   at org.hibernate.impl.AbstractQueryImpl.setString(AbstractQueryImpl.java:434)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 11:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
no, that was a copy/paste error; it should be setString( 0, ... )


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 11:21 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Thanks for clearing my confusion.


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