参数化JPA入站通道适配器

时间:2017-04-28 21:34:10

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

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

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

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

有没有办法将参数从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>

1 个答案:

答案 0 :(得分:0)

嗯,只有当你的查询声明了一些参数时才能真正起作用:

/**
 * 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:parameter

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

另一方面,我会考虑一下Spring Integration Stored Procedure support