gpt4 book ai didi

java - JPA/hibernate : call stored procedure with input and output parameters

转载 作者:行者123 更新时间:2023-12-01 15:52:21 29 4
gpt4 key购买 nike

我在使用 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);

这是甲骨文的代码:

"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)
})
})
请问你有什么想法吗?

最佳答案

您还需要为存储过程注册参数,因此您的代码如下所示:

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

编辑:这对 OP 的问题没有帮助,因为使用了 createNamedStoredProcedureQuery,它会自动声明和注册参数。为了完整起见,我将其留在这里。

关于java - JPA/hibernate : call stored procedure with input and output parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52479626/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com