1

使用 Postgresql 8.1、Spring 3.0、Hibernate 3.6。我有一个调用存储过程的方法,该过程无需使用注释即可工作,本质上是

         ....
         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;
                }
            }
        });
    ....

我如何使用注释来做到这一点,这是我的尝试,但不起作用。

@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)

我有工作代码,但想用注释来工作,任何想法都值得赞赏。

4

0 回答 0