JPA / Hibernate:使用输入和输出参数调用存储过程

时间:2018-09-24 12:39:21

标签: java spring oracle hibernate jpa

我在用JPA调用Java中的排序过程时遇到问题

这是Java代码:

    StoredProcedureQuery storedProcedureQuery = getEntityManager().createNamedStoredProcedureQuery("RECPOCH");

storedProcedureQuery.setParameter(Pochette.SP_RECPOCH_PARAM_STRSTARTSIM, pochetteCriteria.getDebutNumPochette());
storedProcedureQuery.setParameter(Pochette.SP_RECPOCH_PARAM_STRLASTSIM, pochetteCriteria.getFinNumPochette());
storedProcedureQuery.setParameter(Pochette.SP_RECPOCH_PARAM_NACTELID, pochetteCriteria.getActelId());

storedProcedureQuery.execute();
Integer result = (Integer) storedProcedureQuery.getOutputParameterValue(Pochette.SP_RECPOCH_PARAM_NRESULT);

return Long.valueOf(result);

以下是代码Oracle:

"RECPOCH" ( NACTELID IN NUMBER, STRSTARTSIM IN VARCHAR, STRLASTSIM IN VARCHAR, NRESULT OUT INTEGER )

跟踪异常:

13:24:40.295 [http-nio-8080-exec-6] DEBUG o.s.orm.jpa.JpaTransactionManager - Participating in existing transaction
Hibernate: {call RECPOCH(?,?,?,?)}
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Error calling CallableStatement.getMoreResults
    at org.hibernate.jpa.internal.StoredProcedureQueryImpl.execute(StoredProcedureQueryImpl.java:230)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.exception.GenericJDBCException: Error calling CallableStatement.getMoreResults
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
    at org.hibernate.jpa.internal.StoredProcedureQueryImpl.execute(StoredProcedureQueryImpl.java:223)
    ... 90 more
Caused by: java.sql.SQLException: Le nombre de noms de paramètre ne concorde pas avec celui des paramètres inscrits
    at oracle.jdbc.driver.OracleSql.setNamedParameters(OracleSql.java:198)
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:197)
13:24:40.833 [http-nio-8080-exec-6] DEBUG o.s.orm.jpa.JpaTransactionManager - Initiating transaction commit
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:197)
13:24:40.833 [http-nio-8080-exec-6] DEBUG o.s.orm.jpa.JpaTransactionManager - Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@6db6eb5c]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
    at com.zaxxer.hikari.pool.HikariProxyCallableStatement.execute(HikariProxyCallableStatement.java)
    at org.hibernate.result.internal.OutputsImpl.(OutputsImpl.java:52)
    ... 95 more

实体中命名的排序过程定义:

    @NamedStoredProcedureQueries({
            @NamedStoredProcedureQuery(name = Pochette.SP_RECPOCH_NAME, procedureName = "RECPOCH", parameters = {
                    @StoredProcedureParameter(name = Pochette.SP_RECPOCH_PARAM_NRESULT, mode = ParameterMode.OUT, type = Long.class),
                    @StoredProcedureParameter(name = Pochette.SP_RECPOCH_PARAM_STRSTARTSIM, mode = ParameterMode.IN, type = String.class),
                    @StoredProcedureParameter(name = Pochette.SP_RECPOCH_PARAM_STRLASTSIM, mode = ParameterMode.IN, type = String.class),
                    @StoredProcedureParameter(name = Pochette.SP_RECPOCH_PARAM_NACTELID, mode = ParameterMode.IN, type = Long.class)
            })
    })
请问您有什么主意吗?

1 个答案:

答案 0 :(得分:0)

您还需要为存储过程注册参数,因此您的代码应类似于:

StoredProcedureQuery storedProcedureQuery = getEntityManager().createNamedStoredProcedureQuery("RECPOCH");

storedProcedureQuery.registerStoredProcedureParameter(Pochette.SP_RECPOCH_PARAM_STRSTARTSIM, String.class, ParameterMode.IN);
storedProcedureQuery.setParameter(Pochette.SP_RECPOCH_PARAM_STRSTARTSIM, pochetteCriteria.getDebutNumPochette());

storedProcedureQuery.registerStoredProcedureParameter(Pochette.SP_RECPOCH_PARAM_STRLASTSIM, String.class, ParameterMode.IN);
storedProcedureQuery.setParameter(Pochette.SP_RECPOCH_PARAM_STRLASTSIM, pochetteCriteria.getFinNumPochette());

storedProcedureQuery.registerStoredProcedureParameter(Pochette.SP_RECPOCH_PARAM_NACTELID, Integer.class, ParameterMode.IN);
storedProcedureQuery.setParameter(Pochette.SP_RECPOCH_PARAM_NACTELID, pochetteCriteria.getActelId());

storedProcedureQuery.registerStoredProcedureParameter(Pochette.SP_RECPOCH_PARAM_NRESULT, Integer.class, ParameterMode.OUT);

storedProcedureQuery.execute();

Integer result = (Integer) storedProcedureQuery.getOutputParameterValue(Pochette.SP_RECPOCH_PARAM_NRESULT);

return Long.valueOf(result);

编辑:由于使用createNamedStoredProcedureQuery(它会自动声明和注册参数),因此对OP的问题无济于事。为了完整起见,我将其保留在这里。