camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > JPA
Date Fri, 23 Jul 2010 06:55:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/JPA">JPA</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        added description for the new usePersist option<br />
    </div>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >| {{consumer.namedQuery}} | | *JPA
consumer only:* To use a named query when consuming data. | <br>| {{consumer.nativeQuery}}
| | *JPA consumer only:* To use a custom native query when consuming data. | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{usePersist}} | {{false}} | *JPA producer only:* Indicates to use {{entityManager.persist(entity)}}
instead of {{entityManager.merge(entity)}}. Note: {{entityManager.persist(entity)}} doesn&#39;t
work for detached entities (where the EntityManager has to execute an UPDATE instead of an
INSERT query)! | <br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="JPA-JPAComponent"></a>JPA Component</h2>

<p>The <b>jpa</b> component enables you to store and retrieve Java objects
from persistent storage using EJB 3's Java Persistence Architecture (JPA), which is a standard
interface layer that wraps Object/Relational Mapping (ORM) products such as OpenJPA, Hibernate,
TopLink, and so on.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-jpa<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="JPA-Sendingtotheendpoint"></a>Sending to the endpoint</h3>

<p>You can store a Java entity bean in a database by sending it to a JPA producer endpoint.
The body of the <em>In</em> message is assumed to be an entity bean (that is,
a POJO with an <a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/Entity.html"
class="external-link" rel="nofollow">@Entity</a> annotation on it). </p>

<p>If the body does not contain an entity bean, put a <a href="/confluence/display/CAMEL/Message+Translator"
title="Message Translator">Message Translator</a> in front of the endpoint to perform
the necessary conversion first.</p>

<h3><a name="JPA-Consumingfromtheendpoint"></a>Consuming from the endpoint</h3>

<p>Consuming messages from a JPA consumer endpoint removes (or updates) entity beans
in the database. This allows you to use a database table as a logical queue: consumers take
messages from the queue and then delete/update them to logically remove them from the queue.</p>

<p>If you do not wish to delete the entity bean when it has been processed, you can
specify <tt>consumeDelete=false</tt> on the URI. This will result in the entity
being processed each poll.</p>

<p>If you would rather perform some update on the entity to mark it as processed (such
as to exclude it from a future query) then you can annotate a method with <a href="http://activemq.apache.org/camel/maven/camel-jpa/apidocs/org/apache/camel/component/jpa/Consumed.html"
class="external-link" rel="nofollow">@Consumed</a> which will be invoked on your
entity bean when the entity bean is consumed.</p>

<h3><a name="JPA-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
jpa:[entityClassName][?options]
</pre>
</div></div>

<p>For sending to the endpoint, the <em>entityClassName</em> is optional.
If specified, it helps the <a href="/confluence/display/CAMEL/Type+Converter" title="Type
Converter">Type Converter</a> to ensure the body is of the correct type.</p>

<p>For consuming, the <em>entityClassName</em> is mandatory.</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="JPA-Options"></a>Options</h3>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>entityType</tt> </td>
<td class='confluenceTd'> <em>entityClassName</em> </td>
<td class='confluenceTd'> Overrides the <em>entityClassName</em> from the
URI. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>persistenceUnit</tt> </td>
<td class='confluenceTd'> <tt>camel</tt> </td>
<td class='confluenceTd'> The JPA persistence unit used by default. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumeDelete</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>JPA consumer only:</b> If <tt>true</tt>,
the entity is deleted after it is consumed; if <tt>false</tt>, the entity is not
deleted. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumeLockEntity</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>JPA consumer only:</b> Specifies whether or
not to set an exclusive lock on each entity bean while processing the results from polling.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>flushOnSend</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>JPA producer only:</b> Flushes the <a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html"
class="external-link" rel="nofollow">EntityManager</a> after the entity bean has
been persisted. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maximumResults</tt> </td>
<td class='confluenceTd'> <tt>-1</tt> </td>
<td class='confluenceTd'>  <b>JPA consumer only:</b> Set the maximum number
of results to retrieve on the <a href="http://java.sun.com/javaee/5/docs/api/javax/persistence/Query.html"
class="external-link" rel="nofollow">Query</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>transactionManager</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.6.1/2.0:</b> Specifies the transaction
manager to use. If none provided, Camel will use a <tt>JpaTransactionManager</tt>
by default. Can be used to set a JTA transaction manager (for integration with an EJB container).
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.delay</tt> </td>
<td class='confluenceTd'> <tt>500</tt> </td>
<td class='confluenceTd'> <b>JPA consumer only:</b> Delay in milliseconds
between each poll. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.initialDelay</tt> </td>
<td class='confluenceTd'> <tt>1000</tt> </td>
<td class='confluenceTd'> <b>JPA consumer only:</b> Milliseconds before
polling starts. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.useFixedDelay</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>JPA consumer only:</b> Set to <tt>true</tt>
to use fixed delay between polls, otherwise fixed rate is used. See <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html"
class="external-link" rel="nofollow">ScheduledExecutorService</a> in JDK for details.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxMessagesPerPoll</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> <b>Camel 2.0:</b> <b>JPA consumer only:</b>
An integer value to define the maximum number of messages to gather per poll. By default,
no maximum is set. Can be used to avoid polling many thousands of messages when starting up
the server. Set a value of 0 or negative to disable. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.query</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>JPA consumer only:</b> To use a custom query
when consuming data. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.namedQuery</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>JPA consumer only:</b> To use a named query
when consuming data. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.nativeQuery</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>JPA consumer only:</b> To use a custom native
query when consuming data. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>usePersist</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>JPA producer only:</b> Indicates to use <tt>entityManager.persist(entity)</tt>
instead of <tt>entityManager.merge(entity)</tt>. Note: <tt>entityManager.persist(entity)</tt>
doesn't work for detached entities (where the EntityManager has to execute an UPDATE instead
of an INSERT query)! </td>
</tr>
</tbody></table>
</div>



<h3><a name="JPA-MessageHeaders"></a>Message Headers</h3>
<p>Camel adds the following message headers to the exchange:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelJpaTemplate</tt> </td>
<td class='confluenceTd'> <tt>JpaTemplate</tt> </td>
<td class='confluenceTd'> <b>Camel 2.0:</b> The <tt>JpaTemplate</tt>
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. </td>
</tr>
</tbody></table>
</div>


<h3><a name="JPA-ConfiguringEntityManagerFactory"></a>Configuring EntityManagerFactory</h3>
<p>Its strongly advised to configure the JPA component to use a specific <tt>EntityManagerFactory</tt>
instance. If failed to do so each <tt>JpaEndpoint</tt> will auto create their
own instance of <tt>EntityManagerFactory</tt> which most often is not what you
want.</p>

<p>For example, you can instantiate a JPA component that references the <tt>myEMFactory</tt>
entity manager factory, as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"jpa"</span>
class=<span class="code-quote">"org.apache.camel.component.jpa.JpaComponent"</span>&gt;</span>
   <span class="code-tag">&lt;property name=<span class="code-quote">"entityManagerFactory"</span>
ref=<span class="code-quote">"myEMFactory"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>In <b>Camel 2.3</b> the <tt>JpaComponent</tt> will auto lookup
the <tt>EntityManagerFactory</tt> from the <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a> which means you do not need to configure this on the
<tt>JpaComponent</tt> as shown above. You only need to do so if there is ambiguity,
in which case Camel will log a WARN.</p>

<h3><a name="JPA-ConfiguringTransactionManager"></a>Configuring TransactionManager</h3>
<p>Its strongly advised to configure the <tt>TransactionManager</tt> instance
used by the JPA component. If failed to do so each <tt>JpaEndpoint</tt> will auto
create their own instance of <tt>TransactionManager</tt> which most often is not
what you want.</p>

<p>For example, you can instantiate a JPA component that references the <tt>myTransactionManager</tt>
transaction manager, as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"jpa"</span>
class=<span class="code-quote">"org.apache.camel.component.jpa.JpaComponent"</span>&gt;</span>
   <span class="code-tag">&lt;property name=<span class="code-quote">"entityManagerFactory"</span>
ref=<span class="code-quote">"myEMFactory"</span>/&gt;</span>
   <span class="code-tag">&lt;property name=<span class="code-quote">"transactionManager"</span>
ref=<span class="code-quote">"myTransactionManager"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>In <b>Camel 2.3</b> the <tt>JpaComponent</tt> will auto lookup
the <tt>TransactionManager</tt> from the <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a> which means you do not need to configure this on the
<tt>JpaComponent</tt> as shown above. You only need to do so if there is ambiguity,
in which case Camel will log a WARN.</p>

<h3><a name="JPA-Usingaconsumerwithanamedquery"></a>Using a consumer with
a named query</h3>
<p>For consuming only selected entities, you can use the <tt>consumer.namedQuery</tt>
URI query option. First, you have to define the named query in the JPA Entity class:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
@Entity
@NamedQuery(name = <span class="code-quote">"step1"</span>, query = <span class="code-quote">"select
x from MultiSteps x where x.step = 1"</span>)
<span class="code-keyword">public</span> class MultiSteps {
   ...
}
</pre>
</div></div>

<p>After that you can define a consumer uri like this one:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"jpa:<span class="code-comment">//org.apache.camel.examples.MultiSteps?consumer.namedQuery=step1"</span>)
</span>.to(<span class="code-quote">"bean:myBusinessLogic"</span>);
</pre>
</div></div>

<h3><a name="JPA-Usingaconsumerwithaquery"></a>Using a consumer with a query</h3>
<p>For consuming only selected entities, you can use the <tt>consumer.query</tt>
URI query option. You only have to define the query option:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"jpa:<span class="code-comment">//org.apache.camel.examples.MultiSteps?consumer.query=select
o from org.apache.camel.examples.MultiSteps o where o.step = 1"</span>)
</span>.to(<span class="code-quote">"bean:myBusinessLogic"</span>);
</pre>
</div></div>

<h3><a name="JPA-Usingaconsumerwithanativequery"></a>Using a consumer with
a native query</h3>
<p>For consuming only selected entities, you can use the <tt>consumer.nativeQuery</tt>
URI query option. You only have to define the native query option:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"jpa:<span class="code-comment">//org.apache.camel.examples.MultiSteps?consumer.nativeQuery=select
* from MultiSteps where step = 1"</span>)
</span>.to(<span class="code-quote">"bean:myBusinessLogic"</span>);
</pre>
</div></div>

<p>If you use the native query option, you will receive an object array in the message
body.</p>

<h3><a name="JPA-Example"></a>Example</h3>
<p>See <a href="/confluence/display/CAMEL/Tracer+Example" title="Tracer Example">Tracer
Example</a> for an example using <a href="/confluence/display/CAMEL/JPA" title="JPA">JPA</a>
to store traced messages into a database.</p>

<h3><a name="JPA-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Tracer+Example" title="Tracer Example">Tracer
Example</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/JPA">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=52099&revisedVersion=27&originalVersion=26">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/JPA?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message