-->
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:36 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 2:58 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
see this thread:-
http://forum.hibernate.org/viewtopic.ph ... 17dbc7e8d9

I have tried it and it works.
public void testQuery()throws Exception{

Session session=somehow get session;
String value="approval process";
Query q1=session.createSQLQuery("select name from tbl_activity where name like :description");
q1.setText("description", "%"+value+"%");
List records =q1.list();
if(records !=null & records.size()>0){
System.out.println("records "+records.get(0).toString());
}


}

Hope this helps!
latha.


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.