-->
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: Using JPA @NamedNativeQuery for calling stored procedure
PostPosted: Fri Aug 27, 2010 3:58 am 
Newbie

Joined: Tue Aug 09, 2005 10:29 am
Posts: 10
Howdy all!

We're trying out JPA 2 using Hibernate as persistence provider (doh!). I'm looking at calling stored procedures using the named native query annotation but I get strange error messages.

My code:
Code:
@NamedNativeQuery(
        name="callStoredProcedure",
        query="{call getReference(?)};",
        resultClass = Reference.class
)
public class JPA2 {

    public static void main(String[] args) {
        JPA2 jpa2 = new JPA2();

        jpa2.execute();
    }

    private void execute() {

        EntityManagerFactory emf =  Persistence.createEntityManagerFactory("reference");
        EntityManager em = emf.createEntityManager();

        Query q = em.createNativeQuery("callStoredProcedure", Reference.class);
        q.setParameter(1, "1");
        List<Reference> l = q.getResultList();

        if (!l.isEmpty() ) {
            System.out.println("We got a hit");
        } else {
            System.out.println("We didn't get a hit");
        }
    }

This fails with this error message:
Code:
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
   at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:435)
   at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:71)
   at com.memnon.jpa.JPA2.execute(JPA2.java:43)
   at com.memnon.jpa.JPA2.main(JPA2.java:24)
Caused by: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
   at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:81)
   at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:87)
   at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:445)
   at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:417)
   at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:429)
   ... 3 more


When I try the code without the name native query part like so:
Code:
public class JPA2 {

    public static void main(String[] args) {
        JPA2 jpa2 = new JPA2();

        jpa2.execute();
    }

    private void execute() {
        EntityManagerFactory emf =  Persistence.createEntityManagerFactory("reference");
        EntityManager em = emf.createEntityManager();
     
        Query q = em.createNativeQuery("{call getReference(?)};",Reference.class);
        q.setParameter(1,"1");
        List<Reference> l = q.getResultList();

    }
}


Then everything works as it should.
Am I doing something wrong or is this a bug somewhere?

Cheers
Erik Svensson, Memnon


Top
 Profile  
 
 Post subject: Re: Using JPA @NamedNativeQuery for calling stored procedure
PostPosted: Fri Aug 27, 2010 8:04 am 
Newbie

Joined: Tue Aug 09, 2005 10:29 am
Posts: 10
Found the problem myself. The @NamedNativeQuery wasn't in an entity class which apparently is necessary.
I did not know that and can't find where it is stated. If anybody knows, please let me know.

Cheers
Erik Svensson, Memnon


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.