[英]Parameterizing JPA Inbound Channel Adapters
我目前正在開發一個基於Spring Boot框架的應用程序,該框架使用JPA入站通道適配器從關系數據庫中檢索數據。 我有一個名為GetEmployee的存儲過程,它需要接受一個值,該值根據運行應用程序的服務器實例而變化。
我查看了關於適配器的Spring 文檔 ,它提到了一個名為int-jpa:parameter的標記,但只有在使用named-query屬性而不是native-query屬性時才會使用它。
int-jpa:inbound-channel-adapter標簽上還有一個parameter-source屬性,但我沒有找到任何關於如何利用它的好文檔。
有沒有辦法將參數從Spring bean傳遞給本機查詢? 或者是使用named-query屬性的更好方法?
配置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>
好吧,只有當你的查詢聲明了一些參數時,這才真正起作用:
/**
* 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
s。
考慮使用CALL emp.dbo.GetEmployee(?)
或BEGIN emp.dbo.GetEmployee(?); END;
BEGIN emp.dbo.GetEmployee(?); END;
。 不確定哪種語法適用於您的RDBMS。
另一方面,我將查看Spring Integration Stored Procedure支持 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.