You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 14 Next »

JPA Component

The jpa: component allows you to work with databases using JPA (EJB 3 Persistence) such as for working with OpenJPA, Hibernate, TopLink to work with relational databases.

Sending to the endpoint

Sending POJOs to the JPA endpoint inserts entities into the database. The body of the message is assumed to be an entity bean (i.e. a POJO with an @Entity annotation on it).

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

Consuming from the endpoint

Consuming messages removes (or updates) entities 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 when it has been processed you can specify consumeDelete=false on the URI. This will result in the entity being processed each poll.

If you would rather perform some update on the entity to mark it as processed (such as to exclude it from a future query) then you can annotate a method with @Consumed which will be invoked on your entity bean when the entity bean is consumed.

URI format

jpa:[entityClassName]

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

For consuming the entityClassName is mandatory.

Options

Name

Default Value

Description

entityType

entityClassName

Is the provided entityClassName from the URI

persistenceUnit

camel

the JPA persistence unit used by default

consumeDelete

true

Option for JpaConsumer only. Enables / disables whether or not the entity is deleted after it is consumed.

consumeLockEntity

true

Option for JpaConsumer only. Enables / disables whether or not to use exclusive locking of each entity while processing the results from the pooling.

flushOnSend

true

Option for JpaProducer only. Flushes the EntityManager after the entity beans has been persisted.

maximumResults

-1

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

transactionManager

null

Camel 1.6.1/2.0: Sets the transaction manager to use. If none provided Camel will default use JpaTransactionManager. Can be used to set a JTA transaction manager.

consumer.delay

500

Option for JpaConsumer only. Delay in millis between each poll.

consumer.initialDelay

1000

Option for JpaConsumer only. Millis before polling starts.

consumer.userFixedDelay

false

Option for JpaConsumer only. true to use fixed delay between pools, otherwise fixed rate is used. See ScheduledExecutorService in JDK for details.

Configuring EntityManagerFactory

You can configure the EntityManagerFactory to use on the JpaComponent itself. For instance in Spring XML:

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

Configuring TransactionManager

You can configure the TransactionManager to use on the JpaComponent itself. For instance in Spring XML:

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

Example

See Tracer Example for an example using JPA to store traced messages into a database.

  • No labels