In this tutorial we will learn how to persist some data on a Database using a Camel route which is based on the Java Persistence API defined in a project.

The steps to complete this tutorial are:

  1. Let's start from a maven archetype
  2. Create the entity bean
  3. Configure the persistence.xml
  4. Configure the camel-context
  5. Define the camel route
  6. enjoy running it !

1. Camel Maven archetype

There are a lot of archetypes to start with camel. This one configures a camel project based on java:


Using Eclipse: File > New Maven Project

2. Create the entity bean

The class Person represents the entity bean that will be inserted into the database:

public class Person {
	private Long id;
private String name;
private String surname;
public String getName() {
	return name;
public void setName(String name) { = name;
public String getSurname() {
	return surname;
public void setSurname(String surname) {
	this.surname = surname;
public Long getId() {
    return id;

public void setId(Long id) { = id;


To help jaxb to create the JAXBContext we add the file jaxb.index in the model package with this single line inside the model package:


3. Configure the persistence.xml

In the META-INF folder we add the following persistence.xml file with the jdbc properties to connect to the database:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi=""
    version="2.0" xmlns="">
    <persistence-unit name="persistenceUnit"


            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="" value="create" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />


4. Configure the camel-context

The following file, using spring, configures a camel context. A simple java route is added to the camel context.

In the camel context we add the jpa component. It needs also an EntityManagerFactory and a JpaTransactionManager.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

  <camelContext trace="true" xmlns="">
    <routeBuilder ref="myroute"/>
    <bean id="myroute" class=""/>
    <bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
       <property name="entityManagerFactory" ref="entityManagerFactory"/>
       <property name="transactionManager" ref="jpaTxManager"/>
    <bean id="jpaTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="persistenceUnit"/>

5. Define the camel route

The camel route is defined by MyRouteBuilder class in the configure method.

public void configure() {
    	org.apache.camel.converter.jaxb.JaxbDataFormat jxb = new JaxbDataFormat("");

A JaxbDataFormat transforms data from xml to a Person entity and the jpa component inserts it in the database.

6. Enjoy running it

Before running it a database table has to be create.

The hibernate3:hbm2ddl is the maven plugin we are looking for!

mvn hibernate3:hbm2ddl generates the sql create table:

[INFO] writing generated schema to file: /home/marco/FuseWorkshopWorkspace/camel-java-jpa/target/hibernate3/sql/schema.ddl

    drop table if exists Person;

    create table Person (
        id bigint not null auto_increment,
        name varchar(255),
        surname varchar(255),
        primary key (id)

Then camel plugin runs the route:

mvn clean  hibernate3:hbm2ddl compile camel:run

The trace="true" option on the camel context make camel to trace the exchange transported in the route:

[] SpringCamelContext             INFO  Apache Camel 2.10.0 (CamelContext: camel-1) started in 0.318 seconds
[1) thread #1 - file://src/data] Tracer                         INFO  ID-localhost-localdomain-47554-1425079065546-0-2 >>> (route1) from(file://src/data?noop=true) -->  <<< Pattern:InOnly, Headers:{CamelFileRelativePath=message1.xml, CamelFileParent=src/data, CamelFileLastModified=Fri Feb 27 18:24:56 CET 2015, breadcrumbId=ID-localhost-localdomain-47554-1425079065546-0-1, CamelFileAbsolutePath=/home/marco/FuseWorkshopWorkspace/camel-java-jpa/src/data/message1.xml, CamelFilePath=src/data/message1.xml, CamelFileName=message1.xml, CamelFileLength=123, CamelFileNameOnly=message1.xml, CamelFileAbsolute=false}, BodyType:org.apache.camel.component.file.GenericFile, Body:[Body is file based: GenericFile[message1.xml]]
[1) thread #1 - file://src/data] Tracer                         INFO  ID-localhost-localdomain-47554-1425079065546-0-2 >>> (route1)  --> jpa:// <<< Pattern:InOnly, Headers:{CamelFileName=message1.xml, CamelFilePath=src/data/message1.xml, CamelFileAbsolute=false, CamelFileNameOnly=message1.xml, CamelFileAbsolutePath=/home/marco/FuseWorkshopWorkspace/camel-java-jpa/src/data/message1.xml, CamelFileParent=src/data, CamelFileLength=123, CamelFileLastModified=Fri Feb 27 18:24:56 CET 2015, CamelFileRelativePath=message1.xml, breadcrumbId=ID-localhost-localdomain-47554-1425079065546-0-1},, Body:<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

[1) thread #1 - file://src/data] Tracer                         INFO  ID-localhost-localdomain-47554-1425079065546-0-4 >>> (route1) from(file://src/data?noop=true) -->  <<< Pattern:InOnly, Headers:{CamelFileLastModified=Fri Feb 27 18:24:56 CET 2015, CamelFilePath=src/data/message2.xml, CamelFileAbsolutePath=/home/marco/FuseWorkshopWorkspace/camel-java-jpa/src/data/message2.xml, CamelFileName=message2.xml, breadcrumbId=ID-localhost-localdomain-47554-1425079065546-0-3, CamelFileNameOnly=message2.xml, CamelFileAbsolute=false, CamelFileLength=132, CamelFileRelativePath=message2.xml, CamelFileParent=src/data}, BodyType:org.apache.camel.component.file.GenericFile, Body:[Body is file based: GenericFile[message2.xml]]
[1) thread #1 - file://src/data] Tracer                         INFO  ID-localhost-localdomain-47554-1425079065546-0-4 >>> (route1)  --> jpa:// <<< Pattern:InOnly, Headers:{CamelFileAbsolute=false, CamelFileLastModified=Fri Feb 27 18:24:56 CET 2015, CamelFileRelativePath=message2.xml, CamelFileParent=src/data, breadcrumbId=ID-localhost-localdomain-47554-1425079065546-0-3, CamelFileAbsolutePath=/home/marco/FuseWorkshopWorkspace/camel-java-jpa/src/data/message2.xml, CamelFileLength=132, CamelFilePath=src/data/message2.xml, CamelFileNameOnly=message2.xml, CamelFileName=message2.xml},, Body:<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

6.a) You can also use another mvn plugin in order to run camel using java directly.

1) Change the import in the Main class:

import org.apache.camel.spring.Main

in order to make spring to manage the route

2) remove the line "main.addRouteBuilder(new MyRouteBuilder());"

Spring will automatically add the route to the camel context using the camel-context.xml file.

3) Execute:

mvn clean hibernate3:hbm2ddl compile exec:java

6.b) How to read from database?

Exercise: Try it:

public void configure() {
org.apache.camel.converter.jaxb.JaxbDataFormat jaxb = new JaxbDataFormat("");
        from(" o from o where'Marco'")

The complete code is here: