-->
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.  [ 2 posts ] 
Author Message
 Post subject: HibernateException Could not locate named parameter wildcard
PostPosted: Wed Mar 11, 2009 8:37 am 
Newbie

Joined: Fri Aug 10, 2007 2:04 am
Posts: 7
I am using Hibernate 3.2.6 GA with Oracle 9i.

I am writing a native SQL query to do join on two tables (to avoid the eager fetch of other associated tables..) and retrieving scalar values. I am using session.nativeSQLQuery(..) method in this case.

I need to do a wildcard based search with named parameters

Code:
"select * from tablename t where  t.colName like '%:paramName%' "

but when I later do
Code:
query.setParameter("paramName",paramNameValue)


to set the named parameter value, I get the Hibernate Exception


Code:
Hibernate Exception - Could not locate named parameter paramName


ofcourse it works all fine when i remove the wildcards % characters

Code:
"select * from tablename t where  t.colName like ':paramName' "

but that does not do the wildcard search for me anymore.

Here's the actual code . Any help will be greatly appreciated.


Code:
StringBuffer queryStringBuffer =
    new StringBuffer("select d.description as descr," +
      " d.id as id from L_RATE_CLASS_DESCRIPTION d," +
      " L_RATE_CLASS_VEHICLE_USE_TYPE veh" +
      " where d.VEHICLE_USE_TYPE_ID = veh.ID");
if (StringUtils.isNotBlank(searchCriteria.getRateDescription())) {
   queryStringBuffer.
     append(" and upper(trim(d.description)) like %:desDescription%");
}
if (StringUtils.isNotBlank(searchCriteria.getVehicleUseType())) {
   queryStringBuffer.
     append(" and upper(trim(veh.description)) like %:vehDescription%)");
}
         
Query query = session.createSQLQuery(queryStringBuffer.toString())
         .addScalar("descr")
         .addScalar("id");
if (StringUtils.isNotBlank(searchCriteria.getRateDescription())) {
   query = query.setParameter("desDescription", searchCriteria.getRateDescription());
}
if (StringUtils.isNotBlank(searchCriteria.getVehicleUseType())) {
   query = query.setParameter("vehDescription", searchCriteria.getVehicleUseType());
}
List list = query.list();
return list


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 9:07 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
try using like this:

Code:
Query q = session.createQuery("from tablename t where  t.colName like :paramName");
q.setString("paramName", "%Andi%");


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