Hibernate Example

Available as of Camel 2.11

This example is located in the examples/camel-example-hibernate directory of the Camel Extras project.
There is a README.txt file with instructions how to run it.

The source code for this example can be viewed online at this link

If you use maven then you can easily compile and install the example from the command line:

About

This example shows how to exchange data using a shared database table.

The example has two Camel routes. The first route insert new data into the table, triggered by a timer to run every 5th second.
The second route pickup the newly inserted rows from the table, process the row(s), and mark the row(s) as processed when done; to avoid picking up the same rows again.

Implementation

In the camel-context.xml file in the src/main/resources/META-INF/spring folder we have the Spring XML file to setup and configure the database and Hibernate, as well the CamelContext.

Setting up database

And in the same file we setup Hibernate. At first we have the Camel Hibernate component, and then a number of beans to setup transactions. And then the last bean setup the Hibernate session factory where we refer to the data source and the hibernate mapping files, and any other hibernate configurations we may need.

Setting up Hibernate

And then in the same file we setup our Camel application. At first we have a orderBean that we use in the routes to generate new orders and process orders as well.

Camel application

Hibernate mapping and POJO beans

In this example we have a Order POJO in the class org.apacheextras.camel.examples.hibernate.Order which we use Hibernate to map to/from the database. The Order pojo is just a bean with the following attributes with getter/setter

Order pojo

The Order pojo is mapped to Hibernate using the mapping file Order.hbm.xml which is located in the src/main/resources directory of the example

Hibernate mapping file to Order pojo

Running the example

This example can be run from the command line

Press ctrl + c to stop the example.

When running this example you should see logging in the console about orders being processed, for example as shown below:

See Also

© 2004-2014 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram