camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Hibernate Example
Date Wed, 13 Feb 2013 08:17:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Hibernate+Example">Hibernate
Example</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="HibernateExample-HibernateExample"></a>Hibernate Example</h2>
<p><b>Available as of Camel 2.11</b></p>

<p>This example is located in the <tt>examples/camel-example-hibernate</tt>
directory of the <a href="http://camel-extra.googlecode.com/" class="external-link" rel="nofollow">Camel
Extras</a> project.<br/>
There is a <tt>README.txt</tt> file with instructions how to run it.</p>

<p>The source code for this example can be viewed online at <a href="https://code.google.com/a/apache-extras.org/p/camel-extra/source/browse/#svn%2Ftrunk%2Fexamples%2Fcamel-example-hibernate"
class="external-link" rel="nofollow">this link</a></p>

<p>If you use maven then you can easily compile and install the example from the command
line:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
mvn install
</pre>
</div></div>

<h3><a name="HibernateExample-About"></a>About</h3>

<p>This example shows how to exchange data using a shared database table.</p>

<p>The example has two Camel routes. The first route insert new data into the table,
triggered by a timer to run every 5th second.<br/>
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.</p>

<h3><a name="HibernateExample-Implementation"></a>Implementation</h3>

<p>In the <tt>camel-context.xml</tt> file in the <tt>src/main/resources/META-INF/spring</tt>
folder we have the Spring XML file to setup and configure the database and Hibernate, as well
the <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Setting up database</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag"><span class="code-comment">&lt;!-- this is the JDBC
data source which uses an in-memory only Apache Derby database --&gt;</span></span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"dataSource"</span>
class=<span class="code-quote">"org.apache.commons.dbcp.BasicDataSource"</span>
destroy-method=<span class="code-quote">"close"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"driverClassName"</span>
value=<span class="code-quote">"org.apache.derby.jdbc.EmbeddedDriver"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"url"</span>
value=<span class="code-quote">"jdbc:derby:memory:orders;create=true"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"username"</span>
value=""/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"password"</span>
value=""/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>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.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Setting up Hibernate</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag"><span class="code-comment">&lt;!-- setup the Camel
hibernate component --&gt;</span></span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"hibernate"</span>
class=<span class="code-quote">"org.apacheextras.camel.component.hibernate.HibernateComponent"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"sessionFactory"</span>
ref=<span class="code-quote">"sessionFactory"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"transactionStrategy"</span>
ref=<span class="code-quote">"springTransactionStrategy"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>

  <span class="code-tag"><span class="code-comment">&lt;!-- setup hibernate
and spring to use transaction --&gt;</span></span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"springTransactionStrategy"</span>
class=<span class="code-quote">"org.apacheextras.camel.component.hibernate.SpringTransactionStrategy"</span>&gt;</span>
    <span class="code-tag">&lt;constructor-arg ref=<span class="code-quote">"sessionFactory"</span>/&gt;</span>
    <span class="code-tag">&lt;constructor-arg ref=<span class="code-quote">"transactionTemplate"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"transactionManager"</span>
class=<span class="code-quote">"org.springframework.orm.hibernate4.HibernateTransactionManager"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"sessionFactory"</span>
ref=<span class="code-quote">"sessionFactory"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"transactionTemplate"</span>
class=<span class="code-quote">"org.springframework.transaction.support.TransactionTemplate"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"transactionManager"</span>
ref=<span class="code-quote">"transactionManager"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>

  <span class="code-tag"><span class="code-comment">&lt;!-- setup Hibernate
session factory --&gt;</span></span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"sessionFactory"</span>
class=<span class="code-quote">"org.springframework.orm.hibernate4.LocalSessionFactoryBean"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"dataSource"</span>
ref=<span class="code-quote">"dataSource"</span>/&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- here we define
the hibernate mapping files we use --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"mappingResources"</span>&gt;</span>
      <span class="code-tag">&lt;list&gt;</span>
        <span class="code-tag">&lt;value&gt;</span>Order.hbm.xml<span
class="code-tag">&lt;/value&gt;</span>
      <span class="code-tag">&lt;/list&gt;</span>
    <span class="code-tag">&lt;/property&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- and here we
have additional hibernate options --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"hibernateProperties"</span>&gt;</span>
      <span class="code-tag">&lt;value&gt;</span>
        hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect
        hibernate.hbm2ddl.auto=create
      <span class="code-tag">&lt;/value&gt;</span>
    <span class="code-tag">&lt;/property&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>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.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Camel application</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag"><span class="code-comment">&lt;!-- order bean is
our business logic bean that creates new orders --&gt;</span></span>
  <span class="code-tag">&lt;bean id=<span class="code-quote">"orderBean"</span>
class=<span class="code-quote">"org.apacheextras.camel.examples.hibernate.OrderBean"</span>/&gt;</span>

  <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- route that generate
new orders and insert them in the database --&gt;</span></span>
    <span class="code-tag">&lt;route id=<span class="code-quote">"generateOrder-route"</span>&gt;</span>
      <span class="code-tag">&lt;from uri=<span class="code-quote">"timer:foo?period=5s"</span>/&gt;</span>
      <span class="code-tag">&lt;transform&gt;</span>
        <span class="code-tag">&lt;method ref=<span class="code-quote">"orderBean"</span>
method=<span class="code-quote">"generateOrder"</span>/&gt;</span>
      <span class="code-tag">&lt;/transform&gt;</span>
      <span class="code-tag">&lt;to uri=<span class="code-quote">"hibernate:org.apacheextras.camel.examples.hibernate.Order"</span>/&gt;</span>
      <span class="code-tag">&lt;log message=<span class="code-quote">"Inserted
new order ${body.id}"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>


    &lt;!-- route that process the orders by picking up new rows from the database
         and when done processing then update the row to mark it as processed --&gt;
    <span class="code-tag">&lt;route id=<span class="code-quote">"processOrder-route"</span>&gt;</span>
      <span class="code-tag">&lt;from uri=<span class="code-quote">"hibernate:org.apacheextras.camel.examples.hibernate.Order?delay=1s"</span>/&gt;</span>
      <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:orderBean?method=processOrder"</span>/&gt;</span>
      <span class="code-tag">&lt;log message=<span class="code-quote">"${body}"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

  <span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>


<h4><a name="HibernateExample-HibernatemappingandPOJObeans"></a>Hibernate
mapping and POJO beans</h4>

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

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Order pojo</b></div><div class="codeContent
panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class Order {

    <span class="code-keyword">private</span> <span class="code-object">int</span>
id;
    <span class="code-keyword">private</span> <span class="code-object">int</span>
item;
    <span class="code-keyword">private</span> <span class="code-object">int</span>
amount;
    <span class="code-keyword">private</span> <span class="code-object">String</span>
description;

    <span class="code-comment">// getter/setter here
</span>    ...
}
</pre>
</div></div>

<p>The Order pojo is mapped to Hibernate using the mapping file <tt>Order.hbm.xml</tt>
which is located in the <tt>src/main/resources</tt> directory of the example</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Hibernate mapping file to Order pojo</b></div><div
class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;hibernate-mapping&gt;</span>

	<span class="code-tag">&lt;class name=<span class="code-quote">"org.apacheextras.camel.examples.hibernate.Order"</span>
schema=<span class="code-quote">"orders"</span> table=<span class="code-quote">"incoming_order"</span>&gt;</span>

		<span class="code-tag">&lt;id name=<span class="code-quote">"id"</span>&gt;</span>
			<span class="code-tag">&lt;generator class=<span class="code-quote">"native"</span>/&gt;</span>
		<span class="code-tag">&lt;/id&gt;</span>

		<span class="code-tag">&lt;property name=<span class="code-quote">"item"</span>
length=<span class="code-quote">"8"</span>/&gt;</span>
		<span class="code-tag">&lt;property name=<span class="code-quote">"amount"</span>
length=<span class="code-quote">"4"</span>/&gt;</span>
		<span class="code-tag">&lt;property name=<span class="code-quote">"description"</span>
length=<span class="code-quote">"50"</span>/&gt;</span>
	<span class="code-tag">&lt;/class&gt;</span>

<span class="code-tag">&lt;/hibernate-mapping&gt;</span>
</pre>
</div></div>



<h3><a name="HibernateExample-Runningtheexample"></a>Running the example</h3>

<p>This example can be run from the command line</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
mvn camel:run
</pre>
</div></div>

<p>Press ctrl + c to stop the example.</p>

<p>When running this example you should see logging in the console about orders being
processed, for example as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Apache Camel 2.11.0
(CamelContext: camel-1) started in 0.836 seconds
[mel-1) thread #0 - timer:<span class="code-comment">//foo] generateOrder-route    
       INFO  Inserted <span class="code-keyword">new</span> order 1
</span>[camel.examples.hibernate.Order] processOrder-route             INFO  Processed
order id 1 item 222 of 2 copies of ActiveMQ in Action
[mel-1) thread #0 - timer:<span class="code-comment">//foo] generateOrder-route    
       INFO  Inserted <span class="code-keyword">new</span> order 2
</span>[camel.examples.hibernate.Order] processOrder-route             INFO  Processed
order id 2 item 111 of 2 copies of Camel in Action
...
</pre>
</div></div>

<h3><a name="HibernateExample-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a></li>
	<li><a href="/confluence/pages/createpage.action?spaceKey=CAMEL&amp;title=Hibernate+Component&amp;linkCreation=true&amp;fromPageId=30753511"
class="createlink">Hibernate Component</a></li>
	<li><a href="http://camel-extra.googlecode.com/" class="external-link" rel="nofollow">Camel
Extra</a></li>
</ul>

    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/Hibernate+Example">View
Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/Hibernate+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message