Hi,
i have the following fragment in my orm.xml Code:
<orm:named-native-query name="ProductModification.updateQtyInStockById" result-class="com.refapp.domain.Product">
<orm:query>
<![CDATA[SELECT PRODUCT_ID, QTY_IN_STOCK, QTY_ON_ORDER FROM PRODUCT WHERE PRODUCT_ID =:productId]]>
</orm:query>
</orm:named-native-query>
I use the named native query in the DAO method:Code:
protected <T extends Object> T executeNativeQueryByNameSingleResult(
final String queryName, final Map<String, Object> parameters){
Query query = null;
Object result = null;
query = createNativeQuery(queryName, parameters);
result = query.getSingleResult();
return (T)result;
}
protected Query createNativeQuery(final String queryName,
final Map<String, Object> parameters){
assert StringUtils.isNotBlank(queryName) : "Query name should not be null or blank";
Query query = null;
query = getEntityManager().createNativeQuery(queryName); //getEntityManager() gets a ref of entityManager
if(parameters != null){
for (Map.Entry<String, Object> e : parameters.entrySet()){
query.setParameter(e.getKey(), e.getValue());
}
}
return query;
}
and call the DAO method using:Code:
public boolean updateQtyInStockById(Integer productId, Integer quantityInStock) {
Product product = null;
try {
HashMap<String,Object> parameters = new HashMap<String,Object>();
parameters.put("productId", productId);
product = executeNativeQueryByNameSingleResult("ProductModification.updateQtyInStockById",parameters);
}catch(NoResultException noResultException){
return false;
}
product.setQtyInStock(quantityInStock);
store(product);
return true;
}
and i get the following error:Code:
Caused by: org.hibernate.QueryParameterException: could not locate named parameter [productId]
at org.hibernate.engine.query.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:75)
at org.hibernate.engine.query.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:81)
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:413)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:383)
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:179)
... 40 more
am using:
Spring 2.5.6 + Hibernate 3.2.6 GA
Can anyone guide me through this ????
Thanks for reading in.