Java Implementation

所提供的每个组件都使用`o.s.i.jpa.core.JpaExecutor`类,而后者又使用`o.s.i.jpa.core.JpaOperations`接口的实现。JpaOperations`的工作方式类似于典型的数据访问对象 (DAO),并提供诸如查找、持久化、executeUpdate 等方法。对于大多数用例,默认实现(`o.s.i.jpa.core.DefaultJpaOperations)就应该足够了。但是,如果您需要自定义行为,可以指定自己的实现。

Each of the provided components uses the o.s.i.jpa.core.JpaExecutor class, which, in turn, uses an implementation of the o.s.i.jpa.core.JpaOperations interface. JpaOperations operates like a typical Data Access Object (DAO) and provides methods such as find, persist, executeUpdate, and so on. For most use cases, the default implementation (o.s.i.jpa.core.DefaultJpaOperations) should be sufficient. However, you can specify your own implementation if you require custom behavior.

要初始化`JpaExecutor`,您必须使用接受以下其中之一的参数的构造函数:

To initialize a JpaExecutor, you must use one of the constructors that accept one of:

  • EntityManagerFactory

  • EntityManager

  • JpaOperations

以下示例演示如何使用`entityManagerFactory`初始化`JpaExecutor`并将其用于出站网关:

The following example shows how to initialize a JpaExecutor with an entityManagerFactory and use it in an outbound gateway:

@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;
}