【问题标题】:How to call firebird selectable stored procedure using JPA?如何使用 JPA 调用 firebird 可选存储过程?
【发布时间】:2017-01-02 02:31:33
【问题描述】:

我正在尝试使用 JPA 调用 Firebird 可选过程。方法如下:

 public void addLineToBrokerReport(Map<String, Object> parametersForAddDealsProc) {

        MapUtils.debugPrint(System.out, "Parameters for procedure", parametersForAddDealsProc);

        String q = "select a.dol as id, a.out$error_code as error_code " +
            "from ADD_LINE (:IN$DOC," +
            ":IN$SHARE, " +
            ":IN$B_ACC, " +
            ":IN$S_ACC, " +
            ":IN$COMMENT) a";

      Query query = em.createNamedQuery(q, CallProcedureResult.class);
       for (Map.Entry<String, Object> entry : parametersForAddDealsProc.entrySet()) {
           query.setParameter("\"" + entry.getKey()+ "\"", entry.getValue());
        }
        CallProcedureResult result = (CallProcedureResult) query.getSingleResult();
        LOG.info("Error_code = " + result.getError_code()  + " dol = " + result.getId());

    }

调用后返回下一个错误:

java.lang.IllegalArgumentException:未找到命名查询:选择 a.dol 作为 id,a.out$error_code 作为 error_code 来自 P_TDA_ADD_LINE_TO_BROKER_REP (:IN$DOC,:IN$SHARE, :IN$B_ACC, :IN$S_ACC, :IN$COMMENT) 一个

在 org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:665) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344) 在 com.sun.proxy.$Proxy33.createNamedQuery(Unknown Source) 在 com.comp.app.TradesUpload.TradesUpload.addLineToBrokerReport(TradesUpload.java:454) 在 com.comp.app.TradesUpload.TradesUploadTest.addLineToBrokerReport(TradesUploadTest.java:69) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) 在 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

  • 是否可以使用 JPA 调用 firebird 选择过程?
  • 如果方法中有Query query = em.createNamedQuery(q, CallProcedureResult.class);,为什么“找不到命名查询”?

【问题讨论】:

  • 请注意,可选存储过程的行为类似于普通的选择查询,因此您不需要像存储过程一样处理它们;使用 CallProcedureResult 可能是不必要的。

标签: java jpa stored-procedures firebird


【解决方案1】:

em.createNamedQuery 第一个参数应该是NamedQuery 的名称(不是 JPQL)。 NamedQuery 本身您将在注释或 XML 中定义,并针对它指定名称。此外,如果您打算直接调用 SQL(而不是 JPQL),则需要一个用于本机查询的 NamedNativeQuery

JPA 2.1 支持存储过程;我不知道这是否适用于调用 Firebird 的东西,但你也可以尝试一下

【讨论】:

    【解决方案2】:

    是否可以使用 JPA 调用 firebird 选择过程?

    是的。您可以改用EntityManager#createNativeQuery

    如果方法中有Query query = em.createNamedQuery(q, CallProcedureResult.class);,为什么“找不到命名查询”?

    EntityManager#createNamedQuery 的第一个参数必须是NamedQuery,它不是,它是你的实际查询。

    与您的问题无关,Hibernate 支持在本机查询中使用命名参数,但它不能在 JPA 实现中可移植地使用 (https://stackoverflow.com/a/28829942/5078385)。

    【讨论】:

    • 如果我使用em.createNativeQuery(q, CallProcedureResult.class); 然后query.setParameter("IN$SHARE", parametersForAddDealsProc.get("IN$SHARE") ) 抛出Cannot resolve query parameter 'IN$SHARE' less... (Ctrl+F1) This inspection checks for the following Persistence model problems: Named query not found Query parameter not found
    • 这是想法问题。
    • 现在我收到日志:Hibernate: select a.dol as id, a.out$error_code as error_code from ADD_LINE (?, ?, ?, ?, ?) a。参数在哪里?
    • 那么您的查询有效吗?如果是这样,并且您希望能够在日志中看到您的参数值,也许您正在寻找以下答案:stackoverflow.com/a/1713464/5078385
    猜你喜欢
    • 1970-01-01
    • 2021-09-02
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2011-09-12
    相关资源
    最近更新 更多