Namespace Support

使用 XML 名称空间支持时,底层解析器类可为您实例化相关的 Java 类。因此,通常无需处理 JPA 适配器的内部工作原理。本节记录了 Spring Integration 提供的 XML 名称空间支持,并向您展示如何使用 XML 名称空间支持来配置 JPA 组件。

When using XML namespace support, the underlying parser classes instantiate the relevant Java classes for you. Thus, you typically need not deal with the inner workings of the JPA adapter. This section documents the XML namespace support provided by Spring Integration and shows you how to use the XML Namespace support to configure the JPA components.

Common XML Namespace Configuration Attributes

所有 JPA 组件都共享某些配置参数:

Certain configuration parameters are shared by all JPA components:

auto-startup

Lifecycle attribute that signals whether this component should be started during application context startup. Defaults to true. Optional.

id

Identifies the underlying Spring bean definition, which is an instance of either EventDrivenConsumer or PollingConsumer. Optional.

entity-manager-factory

The reference to the JPA entity manager factory that the adapter uses to create the EntityManager. You must provide this attribute, the entity-manager attribute, or the jpa-operations attribute.

entity-manager

The reference to the JPA Entity Manager that the component uses. You must provide this attribute, the entity-manager-factory attribute, or the jpa-operations attribute.

通常,你的 Spring 应用程序上下文仅定义一个 JPA 实体管理器工厂,并且 EntityManager 通过使用 @PersistenceContext 注解进行注入。此方法不适用于 Spring Integration JPA 组件。通常,最好注入 JPA 实体管理器工厂,但是,当你想显式注入 EntityManager 时,你必须定义一个 SharedEntityManagerBean。有关详细信息,请参阅相关的 Javadoc

Usually, your Spring application context defines only a JPA entity manager factory, and the EntityManager is injected by using the @PersistenceContext annotation. This approach does not apply for the Spring Integration JPA components. Usually, injecting the JPA entity manager factory is best, but, when you want to inject an EntityManager explicitly, you have to define a SharedEntityManagerBean. For more information, see the relevant Javadoc.

以下示例展示如何显式包含一个实体管理器工厂:

The following example shows how to explicitly include an entity manager factory:

<bean id="entityManager"
      class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
    <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
jpa-operations

A reference to a bean that implements the JpaOperations interface. In rare cases, it might be advisable to provide your own implementation of the JpaOperations interface instead of relying on the default implementation (org.springframework.integration.jpa.core.DefaultJpaOperations). If you use the jpa-operations attribute, you must not provide the JPA entity manager or JPA entity manager factory, because JpaOperations wraps the necessary datasource.

entity-class

The fully qualified name of the entity class. The exact semantics of this attribute vary, depending on whether we are performing a persist or update operation or whether we are retrieving objects from the database.

在检索数据时,您可以指定 entity-class 属性,以表明您希望从数据库中检索此类型的对象。在这种情况下,您不得定义任何查询属性(jpa-querynative-querynamed-query)。

When retrieving data, you can specify the entity-class attribute to indicate that you would like to retrieve objects of this type from the database. In that case, you must not define any of the query attributes (jpa-query, native-query, or named-query).

在持久化数据时,entity-class 属性表示要持久化的对象类型。如果没有指定(对于持久化操作),则从消息的有效负载自动检索实体类。

When persisting data, the entity-class attribute indicates the type of object to persist. If not specified (for persist operations) the entity class is automatically retrieved from the message’s payload.

jpa-query

Defines the JPA query (Java Persistence Query Language) to be used.

native-query

Defines the native SQL query to be used.

named-query

Refers to a named query. A named query can be defined in either Native SQL or JPAQL, but the underlying JPA persistence provider handles that distinction internally.

Providing JPA Query Parameters

要提供参数,您可以使用 parameter XML 元素。它具有一种机制,使您可以为基于 Java 持久性查询语言 (JPQL) 或本机 SQL 查询的查询提供参数。您还可为命名查询提供参数。

To provide parameters, you can use the parameter XML element. It has a mechanism that lets you provide parameters for queries that are based on either the Java Persistence Query Language (JPQL) or native SQL queries. You can also provide parameters for named queries.

Expression-based Parameters

The following example shows how to set an expression-based parameter:

<int-jpa:parameter expression="payload.name" name="firstName"/>
Value-based Parameters

The following example shows how to set a value-based parameter:

<int-jpa:parameter name="name" type="java.lang.String" value="myName"/>
Positional Parameters

The following example shows how to set an expression-based parameter:

<int-jpa:parameter expression="payload.name"/>
<int-jpa:parameter type="java.lang.Integer" value="21"/>

Transaction Handling

所有 JPA 操作(如 INSERTUPDATEDELETE)都要求在执行操作时保持事务处于活动状态。对于入站通道适配器,无需执行任何特殊操作。其工作方式类似于我们在使用其他入站通道适配器时使用轮询器配置事务管理器的方式。以下 XML 示例配置了一个使用轮询器和入站通道适配器的事务管理器:

All JPA operations (such as INSERT, UPDATE, and DELETE) require a transaction to be active whenever they are performed. For inbound channel adapters, you need do nothing special. It works similarly to the way we configure transaction managers with pollers that are used with other inbound channel adapters. The following XML example configures a transaction manager that uses a poller with an inbound channel adapter:

<int-jpa:inbound-channel-adapter
    channel="inboundChannelAdapterOne"
    entity-manager="em"
    auto-startup="true"
    jpa-query="select s from Student s"
    expect-single-result="true"
    delete-after-poll="true">
    <int:poller fixed-rate="2000" >
        <int:transactional propagation="REQUIRED"
            transaction-manager="transactionManager"/>
    </int:poller>
</int-jpa:inbound-channel-adapter>

但是,在使用出站通道适配器或网关时,可能需要专门启动事务。如果 DirectChannel 是出站适配器或网关的输入通道,并且事务在当前执行线程中处于活动状态,则 JPA 操作在相同的事务上下文中执行。您还可以配置此 JPA 操作以作为新事务运行,如下例所示:

However, you may need to specifically start a transaction when using an outbound channel adapter or gateway. If a DirectChannel is an input channel for the outbound adapter or gateway and if the transaction is active in the current thread of execution, the JPA operation is performed in the same transaction context. You can also configure this JPA operation to run as a new transaction, as the following example shows:

<int-jpa:outbound-gateway
    request-channel="namedQueryRequestChannel"
    reply-channel="namedQueryResponseChannel"
    named-query="updateStudentByRollNumber"
    entity-manager="em"
    gateway-type="UPDATING">
    <int-jpa:parameter name="lastName" expression="payload"/>
    <int-jpa:parameter name="rollNumber" expression="headers['rollNumber']"/>
		<int-jpa:transactional propagation="REQUIRES_NEW"
        transaction-manager="transactionManager"/>
</int-jpa:outbound-gateway>

在前述示例中,出站网关或适配器的 transactional 元素指定了事务属性。如果在适配器中将 DirectChannel 作为输入通道,并且希望适配器在与调用者相同的事务上下文中执行操作,那么定义此子元素是可选的。但是,如果您使用 ExecutorChannel,则必须有 transactional 元素,因为未传播调用客户端的事务上下文。

In the preceding example, the transactional element of the outbound gateway or adapter specifies the transaction attributes. It is optional to define this child element if you have DirectChannel as an input channel to the adapter, and you want the adapter to execute the operations in the same transaction context as the caller. If, however, you use an ExecutorChannel, you must have the transactional element, because the invoking client’s transaction context is not propagated.

与在 Spring Integration 名称空间中定义的轮询器的 transactional 元素不同,出站网关或适配器的 transactional 元素在 JPA 名称空间中定义。

Unlike the transactional element of the poller, which is defined in Spring Integration’s namespace, the transactional element for the outbound gateway or adapter is defined in the JPA namespace.