-->
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.  [ 3 posts ] 
Author Message
 Post subject: JPA Named Native Query with parameters in ORM.xml failing
PostPosted: Fri Jan 08, 2010 10:02 am 
Newbie

Joined: Thu Dec 24, 2009 7:20 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: JPA Named Native Query with parameters in ORM.xml failing
PostPosted: Mon Jan 11, 2010 8:22 am 
Newbie

Joined: Thu Dec 24, 2009 7:20 am
Posts: 2
SOLVED

Sorry mistake from my side

Code:
query = getEntityManager().createNativeQuery(queryName);


must have been

Code:
query = getEntityManager().createNamedQuery(queryName);


even when we use named native query in orm xml, createNativeQuery is used only when we want to create a query in native SQL String, not for a named native query

Thanks for all.


Top
 Profile  
 
 Post subject: Re: JPA Named Native Query with parameters in ORM.xml failing
PostPosted: Wed Jan 19, 2011 6:26 pm 
Newbie

Joined: Fri Sep 30, 2005 12:45 am
Posts: 8
Great post! thanks a lot for posting this.


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