Java 實作
每個提供的組件都使用 o.s.i.jpa.core.JpaExecutor
類別,而該類別又使用 o.s.i.jpa.core.JpaOperations
介面的實作。JpaOperations
的運作方式類似於典型的資料存取物件 (DAO),並提供諸如 find、persist、executeUpdate 等方法。對於大多數使用案例,預設實作 (o.s.i.jpa.core.DefaultJpaOperations
) 應該已足夠。但是,如果您需要自訂行為,則可以指定自己的實作。
若要初始化 JpaExecutor
,您必須使用接受下列其中一項的建構子:
-
EntityManagerFactory
-
EntityManager
-
JpaOperations
以下範例示範如何使用 entityManagerFactory
初始化 JpaExecutor
,並在輸出閘道中使用它
@Bean
public JpaExecutor jpaExecutor() {
JpaExecutor executor = new JpaExecutor(this.entityManagerFactory);
executor.setJpaParameters(Collections.singletonList(new JpaParameter("firstName", null, "#this")));
executor.setUsePayloadAsParameterSource(true);
executor.setExpectSingleResult(true);
return executor;
}
@ServiceActivator(inputChannel = "getEntityChannel")
@Bean
public MessageHandler retrievingJpaGateway() {
JpaOutboundGateway gateway = new JpaOutboundGateway(jpaExecutor());
gateway.setGatewayType(OutboundGatewayType.RETRIEVING);
gateway.setOutputChannelName("resultsChannel");
return gateway;
}