JPA& PostgreSQL:如何使用NamedNativeQuery Annotation调用存储过程

时间:2012-06-13 08:24:53

标签: java spring jpa stored-procedures

使用Postgresql 8.1,Spring 3.0,Hibernate 3.6。 我有一个方法调用一个不使用Annotations的存储过程,基本上它是

         ....
         return (Integer) getJpaTemplate().execute(new JpaCallback() {

            public Object doInJpa(EntityManager em) {                   
                // Query query = em.createNamedQuery("checkZone");
                Query query = em.createNativeQuery("select zoneArea from zoneArea(:pId, :zId)");
                query.setParameter("pId", p.getId());
                query.setParameter("zId", z.getId());

                try {
                    return query.getSingleResult(); // Integer expected
                } catch (NoResultException e) {
                    return 0;
                }
            }
        });
    ....

如何使用Annotations执行此操作,这是我的尝试无效。

@NamedNativeQueries({
@NamedNativeQuery(
    name = "checkZone",
    query = "select zoneArea from zoneArea(:pId, :zId)",
    hints = {
        @QueryHint(name = "org.hibernate.callable", value = "true")
    },
    resultSetMapping = "scalar",
    resultClass = Integer.class)})
@SqlResultSetMapping(name="scalar",columns=@ColumnResult(name="result"))
@Entity

这是异常

Caused by: org.postgresql.util.PSQLException: This statement does not declare an OUT parameter.  Use { ?= call ... } to declare one.
at org.postgresql.jdbc2.AbstractJdbc2Statement.registerOutParameter(AbstractJdbc2Statement.java:1849)
at org.postgresql.jdbc3.AbstractJdbc3Statement.registerOutParameter(AbstractJdbc3Statement.java:1513)
at org.hibernate.dialect.PostgreSQLDialect.registerResultSetOutParameter(PostgreSQLDialect.java:335)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1713)
at org.hibernate.loader.Loader.doQuery(Loader.java:801)

我有工作代码但希望能够使用Annotations,任何想法都会受到赞赏。

0 个答案:

没有答案