-->
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: using a wildcard in the 'like' where clause
PostPosted: Fri Nov 18, 2005 12:19 pm 
Newbie

Joined: Fri Nov 18, 2005 11:57 am
Posts: 6
Hi,

I'm very new to hibernate, and I'm making a small change to an existing site. THe current search page doesn't use wildcards for the various string fields. So I want to add this in.

Short Story:
The query and param values are sent to hibernate like so:
getHibernateTemplate().find(query, valuesList.toArray());

Is this not valid for HQL?: like '%?%'


Long Story:
The params are stored in a hashmap, and they are looped over to build up the query - something like:

while(keys.hasNext()) {
.....
String key = (String) keys.next();
Object value = params.get(key);
valuesList.add(value);

if(value instanceof String) {
query += "p." + key + " like ?";
} else {
query += "p." + key + "=?";
}
}

The values are stored in the valuesList map and sent to the following hibernate function:
return getHibernateTemplate().find(query, valuesList.toArray());


I want to add the wildcard before and after the "?" in the first 'if' body ('%?%'). I got it to work by adding the wildcard to the parameter value (when the hashmap is built up), but I'd rather do it here so the change is only done in one section as opposed to doing it everywhere a String variable is added to a hashmap.

Thanks

Hibernate version: 3

Full stack trace of any exception that occurs:
javax.servlet.ServletException: stpns.services.DaoServiceException: java.lang.IllegalArgumentException: No positional parameters in query: from stpns.dom.Publisher as p where p.name like '%?%'
org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:402)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:355)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Name and version of the database you are using: MySQL 4.1

The generated SQL (show_sql=true): ?

Debug level Hibernate log excerpt: ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 12:21 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
http://forum.hibernate.org/viewtopic.php?t=949145&highlight=

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 12:28 pm 
Newbie

Joined: Fri Nov 18, 2005 11:57 am
Posts: 6
Solution: As the other thread suggested, I changed it to this and it worked:
query += "p." + key + " like '%' || ? || '%'";

nathanmoon wrote:

Sorry! I should've searched on the ERROR MSG, and I might have found this. (Although searching by phrases would be really useful in this forum -- too bad for PHPBB)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 3:30 pm 
Beginner
Beginner

Joined: Thu Aug 04, 2005 2:24 pm
Posts: 45
MaritimeSource wrote:
Solution: As the other thread suggested, I changed it to this and it worked:
query += "p." + key + " like '%' || ? || '%'";

nathanmoon wrote:

Sorry! I should've searched on the ERROR MSG, and I might have found this. (Although searching by phrases would be really useful in this forum -- too bad for PHPBB)


I do this:
Code:
Criteria criteria = s.createCriteria(myclass.class)
   .add(Restrictions.like("id", id + "%"))
   .add(Restrictions.like("description", description + "%"))
   .addOrder(Order.asc("id")) ;


_________________
Eduardo Mylonas


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.