【问题标题】:Parameterizing JPA Inbound Channel Adapters参数化 JPA 入站通道适配器
【发布时间】:2017-04-28 21:34:10
【问题描述】:

我目前正在开发一个基于 Spring Boot 框架的应用程序,该框架使用 JPA 入站通道适配器从关系数据库中检索数据。我有一个名为 GetEmployee 的存储过程,它需要接受一个根据运行应用程序的服务器实例而变化的值。

我查看了适配器上的 Spring documentation,它提到了一个名为 int-jpa:parameter 的标签,但这似乎只在 named-query 属性为使用而不是 native-query 属性。

int-jpa:inbound-channel-adapter标签上还有一个parameter-source属性,但是我没有找到任何关于如何使用它的好文档。

有没有办法将参数从 Spring bean 传递到本机查询?还是使用命名查询属性的更好方法?

配置 XML:

    <int-jpa:inbound-channel-adapter
        id="employeeAdapter"
        channel="employeeChannel"
        entity-manager-factory="entityManagerFactory"   
        entity-class="com.example.entities.Employee"
        native-query="EXEC emp.dbo.GetEmployee"
        expect-single-result="false"    
        delete-after-poll="false">
        <int:poller fixed-rate="10000">
            <int:transactional propagation="REQUIRED" isolation="DEFAULT"
              transaction-manager="transactionManager" />
        </int:poller>
    </int-jpa:inbound-channel-adapter>

【问题讨论】:

    标签: java spring spring-integration spring-data-jpa


    【解决方案1】:

    好吧,只有当您的查询声明了一些参数时,这才真正起作用:

    /**
     * Given a JPQL query, this method gets all parameters defined in this query and
     * use the {@link ParameterSource} to find their values and set them.
     *
     */
    private void setParametersIfRequired(String queryString, ParameterSource source, Query query) {
        Set<Parameter<?>> parameters = query.getParameters();
    

    由于您的EXEC在过程调用中没有任何参数,因此提供的int-jpa:parameters没有任何可提取和申请的内容。

    考虑使用CALL emp.dbo.GetEmployee(?) 或BEGIN emp.dbo.GetEmployee(?); END;。不确定哪种语法适用于您的 RDBMS。

    另一方面,我想看看 Spring Integration Stored Procedure support。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多