Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

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

CamelJpaTemplate

JpaTemplate

Not supported anymore since Camel 2.12: The JpaTemplate object that is used to access the entity bean. You need this object in some situations, for instance in a type converter or when you are doing some custom processing. See CAMEL-5932 for the reason why the support for this header has been dropped.

CamelEntityManager

EntityManager

Camel 2.12: JPA consumer / Camel 2.12.2: JPA producer: The JPA EntityManager object being used by JpaConsumer or JpaProducer.

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{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml}Second we have to setup a org.springframework.orm.jpa.JpaTemplate which is used by the org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository: Wiki Markup{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml}Afterwards we can configure our org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository: Wiki Markup{snippet:id=jpaStore|lang=xml|url=camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml}And finally we can create our JPA idempotent repository in the spring XML file as well:

...

...

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 (smile)

...

Endpoint See Also