...
Maven users will need to add the following dependency to their pom.xml
for this component:
...
Sending to the endpoint
You 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 (that is, a POJO with an @Entity annotation on it) or a collection or array of entity beans.
...
Note: Since Camel 2.18, JPA now includes a JpaPollingConsumer
implementation that better supports Content Enricher using pollEnrich()
to do an on-demand poll that returns either none, one or a list of entities as the result.
URI format
...
...
For sending to the endpoint, the entityClassName is optional. If specified, it helps the Type Converter to ensure the body is of the correct type.
...
Camel adds the following message headers to the exchange:
...
...
Header | Type | Description |
---|---|---|
|
| Not supported anymore since Camel 2.12: The |
|
| Camel 2.12: JPA consumer / Camel 2.12.2: JPA producer: The JPA |
Configuring EntityManagerFactory
...
For example, you can instantiate a JPA component that references the myEMFactory
entity manager factory, as follows:
...
...
In Camel 2.3 the JpaComponent
will auto lookup the EntityManagerFactory
from the Registry which means you do not need to configure this on the JpaComponent
as shown above. You only need to do so if there is ambiguity, in which case Camel will log a WARN.
...
If more than single instance of the TransactionManager
is found, Camel will log a WARN. In such cases you might want to instantiate and explicitly configure a JPA component that references the myTransactionManager
transaction manager, as follows:
...
Using a consumer with a named query
For consuming only selected entities, you can use the consumer.namedQuery
URI query option. First, you have to define the named query in the JPA Entity class:
...
...
After that you can define a consumer uri like this one:
...
Using a consumer with a query
For consuming only selected entities, you can use the consumer.query
URI query option. You only have to define the query option:
...
...
Using a consumer with a native query
For consuming only selected entities, you can use the consumer.nativeQuery
URI query option. You only have to define the native query option:
...
If you use the native query option, you will receive an object array in the message body.
...
For retrieving selected entities or execute bulk update/delete, you can use the namedQuery
URI query option. First, you have to define the named query in the JPA Entity class:
...
...
After that you can define a producer uri like this one:
...
...
Using a producer with a query
For retrieving selected entities or execute bulk update/delete, you can use the query
URI query option. You only have to define the query option:
...
...
Using a producer with a native query
For retrieving selected entities or execute bulk update/delete, you can use the nativeQuery
URI query option. You only have to define the native query option:
...
...
If you use the native query option without specifying resultClass, you will receive an object array in the message body.
...
First we need to setup a persistence-unit
in the persistence.xml file: Wiki Markup org.springframework.orm.jpa.JpaTemplate
which is used by the org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository
: Wiki Markup org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository
: Wiki Markup
...
...
In case you run the tests of this component directly inside your IDE (and not necessarily through Maven itself) then you could spot exceptions like:
...
The problem here is that the source has been compiled/recompiled through your IDE and not through Maven itself which would enhance the byte-code at build time. To overcome this you would need to enable dynamic byte-code enhancement of OpenJPA. As an example assuming the current OpenJPA version being used in Camel itself is 2.2.1, then as running the tests inside your favorite IDE you would need to pass the following argument to the JVM:
...
...
Then it will all become green again
...