| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd";> |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| <html> |
| <head> |
| <link rel="shortcut icon" href="http://aries.apache.org/images/favicon.ico"></link> |
| <link type="text/css" rel="stylesheet" href="http://aries.apache.org/resources/site.css"></link> |
| </script><script src="http://aries.apache.org/resources/menus.js" language="javascript" type="text/javascript"></script> |
| <meta name="keywords" content="..."/> |
| <meta name="description" content="..." /> |
| <title> |
| Apache Aries - JPAProject |
| </title> |
| </head> |
| <body onload="SetMenu()"> |
| |
| <table width="100%" cellpadding="0" cellspacing="0"> |
| <tr width="100%"> |
| <td id="cell-0-0" colspan="2"> </td> |
| <td id="cell-0-1"> </td> |
| <td id="cell-0-2" colspan="2"> </td> |
| </tr> |
| <tr width="100%"> |
| <td id="cell-1-0"> </td> |
| <td id="cell-1-1"> </td> |
| <td id="cell-1-2"> |
| <div style="padding: 5px;"> |
| <div id="banner"> |
| <!-- Banner --> |
| <table border="0" cellpadding="0" cellspacing="0" width="100%"> |
| <tr> |
| <td align="left" class="topbardiv" nowrap=""> |
| <a href="http://aries.apache.org/" title="Apache Aries"> <img border="0" src="http://aries.apache.org/images/Arieslogo_Horizontal.gif"> </a> |
| </td> |
| <td align="right" nowrap=""> |
| <a href="http://www.apache.org/" title="The Apache Software Foundation"> <img border="0" src="http://aries.apache.org/images/apache_feather.png"> </a> |
| </td> |
| </tr> |
| </table> |
| <!-- Banner --> |
| </div> |
| </div> |
| <div id="top-menu"> |
| <table border="0" cellpadding="1" cellspacing="0" width="100%"> |
| <tr> |
| <td> |
| <div align="left"> |
| <!-- Breadcrumbs --> |
| <!-- Breadcrumbs --> |
| </div> |
| </td> |
| <td> |
| <div align="right"> |
| <!-- Quicklinks --> |
| <DIV style="padding: 5px 5px 0px 25px;"> |
| <FORM action="http://www.google.com/search" method="get" style="font-size: 10px;"> |
| <A href="http://www.apache.org/licenses/LICENSE-2.0.html" class="external-link" rel="nofollow">License</A> |
| <INPUT name="ie" type="hidden" value="UTF-8"></INPUT> |
| <INPUT name="oe" type="hidden" value="UTF-8"></INPUT> |
| <INPUT maxlength="255" name="q" size="15" type="text" value></INPUT> |
| <INPUT name="btnG" type="submit" value="Search"></INPUT> |
| <INPUT name="domains" type="hidden" value="aries.apache.org"></INPUT> |
| <INPUT name="sitesearch" type="hidden" value="aries.apache.org"></INPUT> |
| </FORM> |
| </DIV> |
| <!-- Quicklinks --> |
| </div> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </td> |
| <td id="cell-1-3"> </td> |
| <td id="cell-1-4"> </td> |
| </tr> |
| <tr width="100%"> |
| <td id="cell-2-0" colspan="2"> </td> |
| <td id="cell-2-1"> |
| <table> |
| <tr height="100%" valign="top"> |
| <td height="100%"> |
| <div id="wrapper-menu-page-right"> |
| <div id="wrapper-menu-page-top"> |
| <div id="wrapper-menu-page-bottom"> |
| <div id="menu-page"> |
| <!-- NavigationBar --> |
| <style type="text/css"> |
| /* The following code is added by mdx_elementid.py |
| It was originally lifted from http://subversion.apache.org/style/site.css */ |
| /* |
| * Hide class="elementid-permalink", except when an enclosing heading |
| * has the :hover property. |
| */ |
| .headerlink, .elementid-permalink { |
| visibility: hidden; |
| } |
| h2:hover > .headerlink, h3:hover > .headerlink, h1:hover > .headerlink, h6:hover > .headerlink, h4:hover > .headerlink, h5:hover > .headerlink, dt:hover > .elementid-permalink { visibility: visible }</style> |
| <div onclick="SwitchMenu('documentation')" id="documentationTitle" class="menutitle">Documentation</div> |
| |
| <div id="documentation" class="menuitemgroup"> |
| <div class="menuitem"> |
| <a href="/documentation/integrators-guide.html">Integrators Guide</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/documentation/articles.html">Articles</a> |
| </div> |
| <div class="menuitem"> |
| <a href="https://svn.apache.org/repos/asf/aries/slides/">Slides</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/documentation/tutorials.html">Tutorials</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/documentation/tools.html">Tools</a> |
| </div> |
| </div> |
| |
| <div onclick="SwitchMenu('modules')" id="modulesTitle" class="menutitle">Modules</div> |
| |
| <div id="modules" class="menuitemgroup"> |
| <div class="menuitem"> |
| <a href="/modules/samples.html">Samples</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/async-svcs.html">Asynchronous Services</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/blueprint.html">Blueprint</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/blueprint-maven-plugin.html">Blueprint-maven-plugin</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/blueprintannotation.html">Blueprint Annotations</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/blueprintnoosgi.html">Blueprint No-OSGi</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/blueprintweb.html">Blueprint Web</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/containers.html">Containers</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/esaanttask.html">ESA Ant Task </a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/ebamavenpluginproject.html">EBA Maven Plugin </a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/esamavenpluginproject.html">ESA Maven Plugin </a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/jmx.html">JMX</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/jndiproject.html">JNDI</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/jpaproject.html">JPA</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/transactioncontrol.html">Transaction Control Service</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/transactionsproject.html">Transactions</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/rsa.html">Remote Service Admin (RSA)</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/spi-fly.html">SPI Fly</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/subsystems.html">Subsystems</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/modules/applications.html">Applications (obsolete)</a> |
| </div> |
| </div> |
| |
| <div onclick="SwitchMenu('downloads')" id="downloadsTitle" class="menutitle">Downloads</div> |
| |
| <div id="downloads" class="menuitemgroup"> |
| <div class="menuitem"> |
| <a href="/downloads/currentreleases.html">Current Releases</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/downloads/testresults.html">Compliance Tests</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/downloads/archived-releases.html">Archived Releases</a> |
| </div> |
| </div> |
| |
| <div onclick="SwitchMenu('community')" id="communityTitle" class="menutitle">Community</div> |
| |
| <div id="community" class="menuitemgroup"> |
| <div class="menuitem"> |
| <a href="/community/resources.html">Community Resources</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/community/gettinginvolved.html">Getting Involved</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/community/people.html">Who we are</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/community/boardreports.html">Board Reports</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/community/logos.html">Logos for Users</a> |
| </div> |
| </div> |
| |
| <div onclick="SwitchMenu('development')" id="developmentTitle" class="menutitle">Development</div> |
| |
| <div id="development" class="menuitemgroup"> |
| <div class="menuitem"> |
| <a href="/development/buildingaries.html">Building Aries </a> |
| </div> |
| <div class="menuitem"> |
| <a href="/development/guidelines.html">Cording Guidelines</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/development/architecture.html">Architecture</a> |
| </div> |
| <div class="menuitem"> |
| <a href="/development/releasingaries.html">Releasing Aries </a> |
| </div> |
| <div class="menuitem"> |
| <a href="/development/compliancetesting.html">OSGi Compliance Tests </a> |
| </div> |
| <div class="menuitem"> |
| <a href="/development/maintainingthewebpages.html">Web Site Maintenance </a> |
| </div> |
| </div> |
| |
| <div onclick="SwitchMenu('sponsorship')" id="sponsorshipTitle" class="menutitle">Sponsorship</div> |
| |
| <div id="sponsorship" class="menuitemgroup"> |
| <div class="menuitem"> |
| <a href="http://www.apache.org/foundation/thanks.html">Thanks</a> |
| </div> |
| <div class="menuitem"> |
| <a href="http://www.apache.org/foundation/sponsorship.html">Sponsoring Apache</a> |
| </div> |
| </div> |
| |
| <div class="promotion"> |
| <a href="http://www.apache.org/events/current-event.html"> |
| <img src="http://www.apache.org/events/current-event-125x125.png" width="125" height="125"/> |
| </a> |
| </div> |
| <!-- NavigationBar --> |
| </div> |
| </div> |
| </div> |
| </div> |
| </td> |
| <td height="100%" width="100%"> |
| <!-- Content --> |
| <div class="wiki-content"><style type="text/css"> |
| /* The following code is added by mdx_elementid.py |
| It was originally lifted from http://subversion.apache.org/style/site.css */ |
| /* |
| * Hide class="elementid-permalink", except when an enclosing heading |
| * has the :hover property. |
| */ |
| .headerlink, .elementid-permalink { |
| visibility: hidden; |
| } |
| h2:hover > .headerlink, h3:hover > .headerlink, h1:hover > .headerlink, h6:hover > .headerlink, h4:hover > .headerlink, h5:hover > .headerlink, dt:hover > .elementid-permalink { visibility: visible }</style> |
| <h1 id="aries-jpa">Aries JPA<a class="headerlink" href="#aries-jpa" title="Permanent link">¶</a></h1> |
| <p>The Aries JPA project allows allows to use container managed persistence in OSGi in a modular and clean way.</p> |
| <p>The core is the jpa.container module. It implements the "JPA Service Specification Version 1.0" from the OSGi |
| Enterprise Specification available for public download from the <a href="http://www.osgi.org/Download/Release4V42">OSGi Alliance</a>. |
| The jpa.blueprint module implements container managed persistence for Aries Blueprint using a programming model like in JEE. |
| The jpa.support module provides support for container managed persistence for DS and other injection frameworks using the |
| JPATemplate interface.</p> |
| <p>See how it looks like in the See the <a href="https://github.com/apache/aries-jpa/tree/master/examples">Aries JPA examples</a>.</p> |
| <h2 id="source-repository">Source repository<a class="headerlink" href="#source-repository" title="Permanent link">¶</a></h2> |
| <p><a href="https://git-wip-us.apache.org/repos/asf/aries-jpa.git">Git repository aries-jpa</a> at apache. <a href="https://github.com/apache/aries-jpa">Mirror of aries-jpa at github</a>.</p> |
| <h2 id="persistence-bundles">Persistence bundles<a class="headerlink" href="#persistence-bundles" title="Permanent link">¶</a></h2> |
| <p>A bundle is regarded as a persistence bundle if it contains the header <strong>Meta-Persistence:</strong> in it's Manifest. |
| The value of the Meta-Persistence: header is a comma separated list of locations where JPA persistence |
| descriptors can be found. If the header value is empty then a default of META-INF/persistence.xml is used. |
| The persistence bundle typically also contains the JPA entities.</p> |
| <p>For example:</p> |
| <div class="codehilite"><pre> <span class="n">Meta</span><span class="o">-</span><span class="n">Persistence</span><span class="p">:</span> |
| </pre></div> |
| |
| |
| <p>means that META-INF/persistence.xml will be searched for. For non standard locations:</p> |
| <div class="codehilite"><pre> <span class="n">Meta</span><span class="o">-</span><span class="n">Persistence</span><span class="p">:</span> <span class="n">persistence</span><span class="o">/</span><span class="n">myPu</span><span class="p">.</span><span class="n">xml</span><span class="p">,</span> <span class="n">pUnit</span><span class="p">.</span><span class="n">jar</span>!<span class="o">/</span><span class="n">someFolder</span><span class="o">/</span><span class="n">anotherPu</span><span class="p">.</span><span class="n">xml</span> |
| </pre></div> |
| |
| |
| <p>means that the locations "persistence/myPu.xml" (relative to the root of the bundle), and |
| "someFolder/anotherPu.xml" (relative to the root of pUnit.jar, which is in the root of |
| the bundle) will be searched.</p> |
| <h2 id="the-aries-jpa-2-modules">The Aries JPA 2 modules<a class="headerlink" href="#the-aries-jpa-2-modules" title="Permanent link">¶</a></h2> |
| <p>Aries JPA consists of four bundles.</p> |
| <h3 id="aries-jpa-container-orgapacheariesjpacontainer">Aries JPA container (org.apache.aries.jpa.container)<a class="headerlink" href="#aries-jpa-container-orgapacheariesjpacontainer" title="Permanent link">¶</a></h3> |
| <p>The Aries JPA container bundle implements the OSGi JPA service specification. It tracks persistence unit bundles and |
| creates an EntityManagerFactory service as soon as all dependencies are met.</p> |
| <h4 id="specifying-the-persistenceprovider">Specifying the PersistenceProvider<a class="headerlink" href="#specifying-the-persistenceprovider" title="Permanent link">¶</a></h4> |
| <p>For each persistence unit jpa container first determines which persistence provider to use by analyzing the "provider" property of persistence.xml. |
| It will track a PersistenceProvider service that matches this name. If no such property is defined then the first |
| PersistenceProvider service found will be used.</p> |
| <h4 id="specifying-a-datasource">Specifying a DataSource<a class="headerlink" href="#specifying-a-datasource" title="Permanent link">¶</a></h4> |
| <p>The next step is to configure a DataSource. There are two ways to do this. The spec conform way is to use the database properties to determine which |
| DataSourceFactory service to use and to configure it.</p> |
| <p>Additionally aries jpa supports refering to a DataSource service using the jta-datasource or non-jta-datasource properties. The syntax is the aries jndi syntax to search for services.</p> |
| <h4 id="properties-of-the-published-entitymanagerfactory-service">Properties of the published EntityManagerFactory service<a class="headerlink" href="#properties-of-the-published-entitymanagerfactory-service" title="Permanent link">¶</a></h4> |
| <p>The EntityManagerFactory services will be registered with the following properties: </p> |
| <ul> |
| <li>osgi.unit.name: this is the name of the persistence unit</li> |
| <li>osgi.unit.provider: this is the class name of the JPA PersistenceProvider that was used to create the EntityManagerFactory</li> |
| <li>org.apache.aries.jpa.container.managed: this property will be set to true, indicating this is a managed EntityManagerFactory.</li> |
| </ul> |
| <h4 id="useful-notes">Useful notes<a class="headerlink" href="#useful-notes" title="Permanent link">¶</a></h4> |
| <ul> |
| <li>If using a JTA persistence unit keep in mind that you still have to supply a javax.sql.DataSource not an XADataSource. This DataSource must wrap an XADataSource and provide XA resource enlistment. The simplest way to achieve this is to use pax-jdbc.</li> |
| <li>As soon as PersistenceProvider and DataSource are available the EntityManagerFactory service is created. |
| Aries JPA container also supports classpath scanning and load time weaving of JPA entities.</li> |
| <li>You should never call close on the EntityManagerFactory service. This call will be made by the container when |
| the persistence bundle is removed or refreshed. If you do close the EntityManagerFactory service then it will be |
| closed for <strong>all</strong> users of the service.</li> |
| </ul> |
| <h3 id="aries-jpa-api-orgapacheariesjpaapi">Aries JPA API (org.apache.aries.jpa.api)<a class="headerlink" href="#aries-jpa-api-orgapacheariesjpaapi" title="Permanent link">¶</a></h3> |
| <p>A set of interfaces to make it easier to use JPA in OSGi. It contains two main interfaces:</p> |
| <ul> |
| <li>EmSupplier (<strong>deprecated</strong>): Allows to get a thread safe EntityManager and mark entry and exit of blocks that access the EntityManager. This |
| is rather low level and meant to be used mainly by frameworks. </li> |
| <li>JpaTemplate: Allows to write closures that can safely access an EntityManager and are executed inside a transaction.</li> |
| </ul> |
| <h3 id="aries-jpa-supportorgapacheariesjpasupport">Aries JPA support(org.apache.aries.jpa.support)<a class="headerlink" href="#aries-jpa-supportorgapacheariesjpasupport" title="Permanent link">¶</a></h3> |
| <p>For each EntityManagerFactory service this bundle provides additional services not defined in the OSGi spec that make it |
| easier to use JPA without blueprint.</p> |
| <ul> |
| <li>EmSuppler (<strong>deprectaed</strong>): Use the new thread safe EntityManager instead.</li> |
| <li>EntityManager: Thread safe EntityManager. Requires to be run inside a Coordination</li> |
| <li>JPATemplate: Is used similar to the spring JPATemplate. it takes care of the EntityManager lifecycle and transaction handling</li> |
| </ul> |
| <h4 id="thread-safe-entitymanager">Thread safe EntityManager<a class="headerlink" href="#thread-safe-entitymanager" title="Permanent link">¶</a></h4> |
| <p>The plain EntityManager provided by the PersistenceProvider may only be used by one thread at a time. This is ok for EJB |
| where there is a thread pool but not for OSGi where tpyically each class is just instantiated once and meant to be used |
| multi threaded.</p> |
| <p>So Aries JPA provides a thread safe EntityManager that can be safely used. An OSGi Coordinator service is used to bind the |
| EntityManager to a thread and to manage its lifecycle. So to use the EntityManager service you need to make sure you only |
| call it inside a Coordination. The EntityManager will be created on first access and bound to the Coordination. When the |
| outermost Coordination of the current thread exits the EntityManager will be closed. So typically the EntityManager is |
| created per Request.</p> |
| <p>Most users will not use the EntityManager service directly and instead either use the blueprint based Annotations or the JPATemplate. |
| Both of these approaches take care of the transaction and Coordination handling transparently.</p> |
| <h3 id="aries-jpa-blueprint-extension-orgapacheariesjpablueprint">Aries JPA blueprint extension (org.apache.aries.jpa.blueprint)<a class="headerlink" href="#aries-jpa-blueprint-extension-orgapacheariesjpablueprint" title="Permanent link">¶</a></h3> |
| <p>Provides a blueprint extension for @PersistenceUnit and @PersistenceContext injection. |
| To use the extension add this namespace to your blueprint </p> |
| <div class="codehilite"><pre> <span class="n">xmlns</span><span class="p">:</span><span class="n">jpa</span><span class="p">=</span>"<span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">aries</span><span class="p">.</span><span class="n">apache</span><span class="p">.</span><span class="n">org</span><span class="o">/</span><span class="n">xmlns</span><span class="o">/</span><span class="n">jpa</span><span class="o">/</span><span class="n">v2</span><span class="p">.</span>0<span class="p">.</span>0" |
| </pre></div> |
| |
| |
| <p>and enable annotation support using the element <jpa:enable /> on top level. |
| Typically this namespace is used together with the transaction namespace to also provide annoation based transactions.</p> |
| <p>For more details see the <a href="https://svn.apache.org/repos/asf/aries/trunk/jpa/examples/tasklist-blueprint/">Aries JPA blueprint example</a>.</p> |
| <h1 id="creation-of-a-jpa-project-using-maven">Creation of a JPA project using Maven<a class="headerlink" href="#creation-of-a-jpa-project-using-maven" title="Permanent link">¶</a></h1> |
| <p>The first step consist in to create a maven module and make the following modifications to allow to deploy |
| it as OSGI bundle on the platform and reference where the persistence XML file must loaded by the classpath to |
| allow to the JPA container to configure the project accordingly.</p> |
| <p><strong>Step 1 : Create a bundle </strong></p> |
| <p>OSGi bundles are mostly regular jars but they need to contain some special OSGi headers in the Manifest. The two changes make sure your maven project creates |
| a valid OSGi bundle.</p> |
| <div class="codehilite"><pre><span class="nt"><packaging></span>bundle<span class="nt"></packaging></span> |
| </pre></div> |
| |
| |
| <p>and that you must configure the maven-bundle-plugin (http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html) |
| to generate the MANIFEST.MF file required by OSGI platform.</p> |
| <div class="codehilite"><pre><span class="nt"><plugin></span> |
| <span class="nt"><groupId></span>org.apache.felix<span class="nt"></groupId></span> |
| <span class="nt"><artifactId></span>maven-bundle-plugin<span class="nt"></artifactId></span> |
| <span class="nt"><version></span>2.5.4<span class="nt"></version></span> |
| <span class="nt"><extensions></span>true<span class="nt"></extensions></span> |
| <span class="nt"><inherited></span>true<span class="nt"></inherited></span> |
| <span class="nt"><configuration></span> |
| <span class="nt"><instructions></span> |
| <span class="c"><!-- Only needed for the persistence bundle containing the jpa Entities --></span> |
| <span class="nt"><Meta-Persistence></span>META-INF/persistence.xml<span class="nt"></Meta-Persistence></span> |
| <span class="c"><!-- Needed for runtime enhancement when using hibernate --></span> |
| <span class="nt"><Dynamic-Import-Package></span>*, org.hibernate.proxy, javassist.util.proxy<span class="nt"></Dynamic-Import-Package></span> |
| <span class="nt"></instructions></span> |
| <span class="nt"></configuration></span> |
| <span class="nt"></plugin></span> |
| </pre></div> |
| |
| |
| <p><strong>Step 2 : Adapt the persistence file</strong></p> |
| <p>We will cover here how to modify a persistence.xml for OSGi usage. For the most part only the access to the DataSource has to be adapted for OSGi. |
| With J2EE applications, you simply use the jdbc key with the name of the datasource associated (jdbc/reportincidentdb). In OSGi jndi support is provided by aries jndi |
| (http://aries.apache.org/modules/jndiproject.html). It bridges jndi names to OSGi services. |
| We must define two parameters, the "osgi:service" wich will allow to lookup OSGI services, the interface "javax.sql.DataSource" and the name of the service "osgi.jndi.service.name", which is a filter property, with its jndi name associated.</p> |
| <p>To access to the datasource, you must provide within the <jta-data-source> or <non-jta-data-source> depending if you use transaction type JTA or RESOURCE_LOCAL.</p> |
| <div class="codehilite"><pre><span class="nt"><persistence-unit</span> <span class="na">name=</span><span class="s">"tasklist"</span> <span class="na">transaction-type=</span><span class="s">"JTA"</span><span class="nt">></span> |
| <span class="nt"><jta-data-source></span>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/tasklist)<span class="nt"></jta-data-source></span> |
| </pre></div> |
| |
| |
| <p>The other elements of the xml file are defined according to JPA specification. </p> |
| <p><strong>Step 3.1 : Inject EntityManager into a bean and make it transactional </strong></p> |
| <p>The goal of this step is to provide a DAO layer that looks like JEE code on the java level. For this we need to inject |
| a thread safe EntityManager and ensure the DAO code is run inside a transational context.</p> |
| <p>Aries JPA 1.x used a xml element inside each DAO bean to inject the EntityManager. This syntax is not suppoerted for Aries JPA 2.x anymore. |
| Instead simply enable standard @PesistenceContext and @PersistenceUnit annotation support with the xml element <jpa:enable/> on top level.</p> |
| <p>The transactional context is established using the xml element <tx:transaction> on the bean level. In the example below we enable transactions for |
| all DAO methods. The scope of the transaction can be defined using the attribute value.</p> |
| <p>Example blueprint follows showing the full breadth of allowable injection syntax:</p> |
| <div class="codehilite"><pre><span class="nt"><blueprint</span> <span class="na">xmlns=</span><span class="s">"http://www.osgi.org/xmlns/blueprint/v1.0.0"</span> |
| <span class="na">xmlns:tx=</span><span class="s">"http://aries.apache.org/xmlns/transactions/v1.2.0"</span> |
| <span class="na">xmlns:jpa=</span><span class="s">"http://aries.apache.org/xmlns/jpa/v2.0.0"</span><span class="nt">></span> |
| <span class="nt"><jpa:enable</span> <span class="nt">/></span> |
| <span class="nt"><service</span> <span class="na">ref=</span><span class="s">"taskService"</span> <span class="na">interface=</span><span class="s">"org.apache.aries.jpa.example.tasklist.model.TaskService"</span><span class="nt">/></span> |
| <span class="nt"><bean</span> <span class="na">class=</span><span class="s">"org.apache.aries.jpa.example.tasklist.blueprint.impl.TaskServiceImpl"</span><span class="nt">/></span> |
| <span class="nt"><tx:transaction</span> <span class="na">method=</span><span class="s">"*"</span><span class="nt">/></span> |
| <span class="nt"></bean></span> |
| <span class="nt"></blueprint></span> |
| </pre></div> |
| |
| |
| <p>Make sure you inject the EntityManager in your DAO class like this:</p> |
| <div class="codehilite"><pre><span class="p">@</span><span class="n">PersistenceContext</span><span class="p">(</span><span class="n">unitName</span><span class="p">=</span>"<span class="n">tasklist</span>"<span class="p">)</span> |
| <span class="n">EntityManager</span> <span class="n">em</span><span class="p">;</span> |
| </pre></div> |
| |
| |
| <p>See tasklist-blueprint example for details.</p> |
| <p><strong>Step 3.2 : Use JPATemplate to work with JPA in declarative services </strong></p> |
| <p>Inject the JPATemplate using a service reference:</p> |
| <div class="codehilite"><pre><span class="p">@</span><span class="n">Reference</span><span class="p">(</span><span class="n">target</span> <span class="p">=</span> "<span class="p">(</span><span class="n">osgi</span><span class="p">.</span><span class="n">unit</span><span class="p">.</span><span class="n">name</span><span class="p">=</span><span class="n">tasklist</span><span class="p">)</span>"<span class="p">)</span> |
| <span class="n">public</span> <span class="n">void</span> <span class="n">setJpaTemplate</span><span class="p">(</span><span class="n">JpaTemplate</span> <span class="n">jpa</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span> |
| </pre></div> |
| |
| |
| <p>Use the JPATemplate to work with JPA Entities inside closures.</p> |
| <div class="codehilite"><pre><span class="c1">// txExpr if you need to return an object</span> |
| <span class="k">return</span> <span class="n">jpa</span><span class="p">.</span><span class="n">txExpr</span><span class="p">(</span><span class="n">TransactionType</span><span class="p">.</span><span class="n">Required</span><span class="p">,</span> <span class="n">em</span> <span class="o">-></span> <span class="n">em</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="n">Task</span><span class="p">.</span><span class="k">class</span><span class="p">,</span> <span class="n">id</span><span class="p">));</span> |
| |
| <span class="c1">// tx if you just execute code</span> |
| <span class="n">jpa</span><span class="p">.</span><span class="n">tx</span><span class="p">(</span><span class="n">em</span> <span class="o">-></span> <span class="n">em</span><span class="p">.</span><span class="n">persist</span><span class="p">(</span><span class="k">task</span><span class="p">));</span> |
| </pre></div> |
| |
| |
| <p>See the tasklist-ds example for details.</p> |
| <p><strong>Step 4 : Package the solution</strong></p> |
| <p>To package the solution, execute a "maven clean install" instruction. Installing Aries JPA and Aries Transaction into arbitrary containers is beyond the scope of this document. </p> |
| <h2 id="example">Example<a class="headerlink" href="#example" title="Permanent link">¶</a></h2> |
| <p>To keep the installation instructions small we only cover installation into Apache Karaf 4.x. Karaf provides features for Aries JPA, |
| Aries Transaction, Hibernate and Pax-jdbc so installation is very easy.</p> |
| <p>See the <a href="https://github.com/apache/aries-jpa/tree/master/examples">README of the Aries JPA examples</a>.</p></div> |
| <!-- Content --> |
| </td> |
| </tr> |
| </table> |
| </td> |
| <td id="cell-2-2" colspan="2"> </td> |
| </tr> |
| <tr width="100%"> |
| <td id="cell-3-0"> </td> |
| <td id="cell-3-1"> </td> |
| <td id="cell-3-2"> |
| <div id="footer"> |
| <!-- Footer --> |
| <div id="site-footer"> |
| <a href="http://aries.apache.org/privacy-policy.html";>Privacy |
| Policy</a> |
| </div> |
| <!-- Footer --> |
| </div> |
| </td> |
| <td id="cell-3-3"> </td> |
| <td id="cell-3-4"> </td> |
| </tr> |
| <tr width="100%"> |
| <td id="cell-4-0" colspan="2"> </td> |
| <td id="cell-4-1"> </td> |
| <td id="cell-4-2" colspan="2"> </td> |
| </tr> |
| </table> |
| </body> |
| </html> |