Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Rephrased intro paragraph + copy edits

JPA Component

The jpa: component allows enables you to work with databases using JPA (EJB 3 Persistence) such as for working with store and retrieve Java objects from persistent storage using EJB 3's Java Persistence Architecture (JPA), which is a standard interface layer that wraps Object/Relational Mapping (ORM) products such as OpenJPA, Hibernate, TopLink to work with relational databases, and so on.

Sending to the endpoint

Sending POJOs to the JPA endpoint inserts entities into the databaseYou can store a Java entity bean in a database by sending it to a JPA producer endpoint. The body of the In message is assumed to be an entity bean (i.e. that is, a POJO with an @Entity annotation on it).

If the body does not contain an entity bean then use , put a Message Translator in front of the endpoint to perform the necessary conversion first.

Consuming from the endpoint

Consuming messages from a JPA consumer endpoint removes (or updates) entities entity beans in the database. This allows you to use a database table as a logical queue, : consumers take messages from the queue and then delete/update them to logically remove them from the queue.

If you do not wish to delete the entity bean when it has been processed, you can specify consumeDelete=false on the URI. This will result in the entity being processed each poll.

...

Code Block
jpa:[entityClassName][?options]

For sending to the endpoint, the entityClassName is optional. If specified, it is used to help use helps the Type Converter to ensure the body is of the correct type.

For consuming, the entityClassName is mandatory.

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

Name

Default Value

Description

entityType

entityClassName

Is Overrides the provided entityClassName from the URI.

persistenceUnit

camel

the The JPA persistence unit used by default.

consumeDelete

true

Option for JpaConsumer only. Enables / disables whether or not JPA consumer only: If true, the entity is deleted after it is consumed; if false, the entity is not deleted.

consumeLockEntity

true

Option for JpaConsumer only. Enables / disables JPA consumer only: Specifies whether or not to use exclusive locking of set an exclusive lock on each entity bean while processing the results from the poolingpolling.

flushOnSend

true

Option for JpaProducer only. JPA producer only: Flushes the EntityManager after the entity beans bean has been persisted.

maximumResults

-1

Option for JpaConsumer only. JPA consumer only: Set the maximum number of results to retrieve on the Query.

transactionManager

null

Camel 1.6.1/2.0: Sets Specifies the transaction manager to use. If none provided, Camel will default use a JpaTransactionManager by default. Can be used to set a JTA transaction manager (for integration with an EJB container).

consumer.delay

500

Option for JpaConsumer only. JPA consumer only: Delay in millis milliseconds between each poll.

consumer.initialDelay

1000

Option for JpaConsumer only. Millis JPA consumer only: Milliseconds before polling starts.

consumer.userFixedDelay

false

Option for JpaConsumer only. JPA consumer only: Set to true to use fixed delay between poolspolls, otherwise fixed rate is used. See ScheduledExecutorService in JDK for details.

maxMessagesPerPoll

0

Camel 2.0: Option for JpaConsumer only. JPA consumer only: An integer value to define a the maximum number of messages to gather per poll. By default, no maximum is set. Can be used to set a limit of e.g. 1000 to avoid avoid polling many thousands of messages when starting up the server that there are thousands of files. Set a value of 0 or negative to disabled itdisable.

Message Headers

Camel will add adds the following message headers to the Exchangeexchange:

Header

Type

Description

CamelJpaTemplate

JpaTemplate

Camel 2.0: The JpaTemplate object that is used to access the entity with. Allows you to get hold on it in case you need it bean. You need this object in some situations, for instance in a type converter or when you do are doing some custom processing.

Configuring EntityManagerFactory

You can configure the EntityManagerFactory JPA component to use on the JpaComponent itself. For instance in Spring XMLa specific EntityManagerFactory instance. For example, you can instantiate a JPA component that references the myEMFactory entity manager factory, as follows:

Code Block
xml
xml
<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
   <property name="entityManagerFactory" ref="myEMFactory"/>
</bean>

Configuring TransactionManager

You can configure specify the TransactionManager to use on the JpaComponent itself. For instance in Spring XML instance used by the JPA component. For example, you can instantiate a JPA component that references the myTransactionManager transaction manager, as follows:

Code Block
xml
xml
<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
   <property name="entityManagerFactory" ref="myEMFactory"/>
   <property name="transactionManager" ref="myTransactionManager"/>
</bean>

...