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