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