deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r922514 [7/11] - in /websites/staging/deltaspike/trunk/content: ./ documentation/ retired/
Date Wed, 17 Sep 2014 12:22:53 GMT
Added: websites/staging/deltaspike/trunk/content/retired/jpa.html
==============================================================================
--- websites/staging/deltaspike/trunk/content/retired/jpa.html (added)
+++ websites/staging/deltaspike/trunk/content/retired/jpa.html Wed Sep 17 12:22:52 2014
@@ -0,0 +1,520 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+    <meta charset="utf-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <meta name="description" content="deltaspike-generate-pages">
+    <meta name="author" content="chm">
+
+    <title>Apache DeltaSpike - JPA Module</title>
+
+    
+
+    
+    <!-- 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 &quot;License&quot;); 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 &quot;AS IS&quot; 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. -->
+
+    <!-- Styles -->
+    
+    <link href="./../resources/css/bootstrap.css" rel="stylesheet">    
+    <!--<link href="./../resources/css/prettify.css" rel="stylesheet" /> -->
+    <link href="./../resources/css/codehilite.css" rel="stylesheet" />
+    <link href="./../resources/css/bootstrap-responsive.css" rel="stylesheet">
+    <style type="text/css">
+        body {
+            padding-top: 60px;
+            padding-bottom: 40px;
+        }
+    </style>
+	<script type="text/javascript">
+
+	  var _gaq = _gaq || [];
+	  _gaq.push(['_setAccount', 'UA-36103647-1']);
+	  _gaq.push(['_trackPageview']);
+	
+	  (function() {
+		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+	  })();
+	
+	</script>
+</head>
+
+<body>
+    <div class="navbar navbar-fixed-top">
+        <div class="navbar-inner">
+            <div class="container">
+                <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
+                    <span class="icon-bar"></span>
+                    <span class="icon-bar"></span>
+                    <span class="icon-bar"></span>
+                </a>
+                <a class="brand logocolor" href="/index.html">Apache DeltaSpike</a>
+                <div class="nav-collapse">
+                    <ul class="nav">
+                        <li class="active"><a href="./../index.html">Home</a></li>
+                        <li><a href="./../documentation.html">Documentation</a></li>
+                        <li><a href="./../source.html">Source</a></li>
+                        <li><a href="./../download.html">Download</a></li>
+                        <li><a href="./../community.html">Community</a></li>
+                        <!-- <li><a href="./../support.html">Support</a></li>
 -->
+                        <li><a href="./../news.html">News</a></li>
+                        <li><a href="./../migration-guide.html">Migration</a></li>
+                    </ul>
+                </div><!--/.nav-collapse -->
+                <form id="search-form" action="http://www.google.com/search" method="get"
 class="navbar-search pull-right" >
+                    <input value="deltaspike.apache.org" name="sitesearch" type="hidden"/>
+                    <input class="search-query" name="q" id="query" type="text" />
+                </form>
+            </div>
+        </div>
+    </div>
+
+    <div class="container">
+      <div class="row">
+          <div class="span12">
+              <div class="page-title">
+                <h1>JPA Module</h1>
+              </div>
+              <div class="toc">
+<ul>
+<li><a href="#transactional">@Transactional</a></li>
+<li><a href="#transactionscoped">@TransactionScoped</a></li>
+<li><a href="#extended-persistence-contexts">Extended Persistence Contexts</a></li>
+<li><a href="#jta-support">JTA Support</a></li>
+</ul>
+</div>
+<hr />
+<h1 id="transactional">@Transactional</h1>
+<p>This annotation is an alternative to transactional EJBs which allows to execute
a method within a transaction.
+Before it's possible to start using the annotation, it's required to implement a CDI producer
for an <code>EntityManager</code> and it's needed to inject the <code>EntityManager</code>
in the bean which uses <code>@Transactional</code>. As shown later on it's also
possible to use multiple qualifiers for using different <code>EntityManager</code>s.</p>
+<p><strong>Hint:</strong>
+If you are using features described by this page and the CDI container you are using is Weld
(or OpenWebBeans in BDA mode), you have to enable the transaction interceptor in your beans.xml
file:</p>
+<div class="codehilite"><pre><span class="nt">&lt;beans&gt;</span>
+    <span class="nt">&lt;interceptors&gt;</span>
+        <span class="nt">&lt;class&gt;</span>org.apache.deltaspike.jpa.impl.transaction.TransactionalInterceptor<span
class="nt">&lt;/class&gt;</span>
+    <span class="nt">&lt;/interceptors&gt;</span>
+<span class="nt">&lt;/beans&gt;</span>
+</pre></div>
+
+
+<p>The following example shows a simple producer for an <code>EntityManager</code>
and the corresponding dispose-method.
+Producing it as request scoped bean means that the dispose method will be called on finishing
the request.
+As an alternative it's possible to use a special scope called <code>@TransactionScoped</code>
provided by the same DeltaSpike module.</p>
+<p>Producer for the default EntityManager (<strong>no EE-Server</strong>):</p>
+<div class="codehilite"><pre><span class="c1">//...</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">EntityManagerProducer</span>
+<span class="o">{</span>
+    <span class="c1">//or manual bootstrapping</span>
+    <span class="nd">@PersistenceContext</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">;</span>
+
+    <span class="nd">@Produces</span>
+    <span class="nd">@RequestScoped</span>
+    <span class="kd">protected</span> <span class="n">EntityManager</span>
<span class="nf">createEntityManager</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">return</span> <span class="k">this</span><span
class="o">.</span><span class="na">entityManager</span><span class="o">;</span>
+    <span class="o">}</span>
+
+    <span class="kd">protected</span> <span class="kt">void</span>
<span class="nf">closeEntityManager</span><span class="o">(</span><span
class="nd">@Disposes</span> <span class="n">EntityManager</span> <span
class="n">entityManager</span><span class="o">)</span>
+    <span class="o">{</span>
+        <span class="k">if</span> <span class="o">(</span><span
class="n">entityManager</span><span class="o">.</span><span class="na">isOpen</span><span
class="o">())</span>
+        <span class="o">{</span>
+            <span class="n">entityManager</span><span class="o">.</span><span
class="na">close</span><span class="o">();</span>
+        <span class="o">}</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>Producer for the default EntityManager (<strong>EE-Server</strong>):</p>
+<div class="codehilite"><pre><span class="nd">@ApplicationScoped</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">EntityManagerProducer</span>
+<span class="o">{</span>
+    <span class="nd">@PersistenceUnit</span>
+    <span class="kd">private</span> <span class="n">EntityManagerFactory</span>
<span class="n">entityManagerFactory</span><span class="o">;</span>
+
+    <span class="nd">@Produces</span>
+    <span class="nd">@Default</span>
+    <span class="nd">@RequestScoped</span>
+    <span class="kd">public</span> <span class="n">EntityManager</span>
<span class="nf">create</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">return</span> <span class="k">this</span><span
class="o">.</span><span class="na">entityManagerFactory</span><span
class="o">.</span><span class="na">createEntityManager</span><span
class="o">();</span>
+    <span class="o">}</span>
+
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">dispose</span><span class="o">(</span><span class="nd">@Disposes</span>
<span class="nd">@Default</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">)</span>
+    <span class="o">{</span>
+        <span class="k">if</span> <span class="o">(</span><span
class="n">entityManager</span><span class="o">.</span><span class="na">isOpen</span><span
class="o">())</span>
+        <span class="o">{</span>
+            <span class="n">entityManager</span><span class="o">.</span><span
class="na">close</span><span class="o">();</span>
+        <span class="o">}</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>The following examples show how to use the <code>EntityManager</code>
produced by the example above.</p>
+<p>Beans with transactional method:</p>
+<div class="codehilite"><pre><span class="c1">//...</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">TransactionalBean</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">;</span>
+
+    <span class="nd">@Transactional</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">executeInTransaction</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="c1">//...</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>Simple transactional bean (all methods transactional):</p>
+<div class="codehilite"><pre><span class="c1">//...</span>
+<span class="nd">@Transactional</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">TransactionalBean</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">;</span>
+
+    <span class="c1">//...</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>As illustrated in the following example it's also possible to use <code>@Transactional</code>
for stereotypes.</p>
+<p>Stereotype for transactional beans (+ usage):</p>
+<div class="codehilite"><pre><span class="nd">@Stereotype</span>
+<span class="nd">@Transactional</span>
+<span class="nd">@ApplicationScoped</span>
+<span class="kd">public</span> <span class="nd">@interface</span>
<span class="n">Repository</span>
+<span class="o">{</span>
+<span class="o">}</span>
+
+<span class="c1">//...</span>
+<span class="nd">@Repository</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">TransactionalBean</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">;</span>
+
+    <span class="c1">//...</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>Besides such simple usages, it's also supported to use qualifiers to access multiple
persistence-units in parallel.
+The default qualifier for <code>@Transactional</code> is <code>@Any</code>.
Therefore a transaction for every injected entity manager will be started.
+The example afterwards shows how to change this default behaviour.</p>
+<p>Producer for multiple entity managers (+ usage):</p>
+<div class="codehilite"><pre><span class="c1">//...</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">EntityManagerProducer</span>
+<span class="o">{</span>
+    <span class="nd">@PersistenceContext</span><span class="o">(</span><span
class="n">unitName</span> <span class="o">=</span> <span class="s">&quot;firstDB&quot;</span><span
class="o">)</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">firstEntityManager</span><span class="o">;</span>
+
+    <span class="nd">@PersistenceContext</span><span class="o">(</span><span
class="n">unitName</span> <span class="o">=</span> <span class="s">&quot;secondDB&quot;</span><span
class="o">)</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">secondEntityManager</span><span class="o">;</span>
+
+    <span class="nd">@Produces</span>
+    <span class="nd">@First</span>
+    <span class="nd">@RequestScoped</span>
+    <span class="kd">protected</span> <span class="n">EntityManager</span>
<span class="nf">createFirstEntityManager</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">return</span> <span class="k">this</span><span
class="o">.</span><span class="na">firstEntityManager</span><span
class="o">;</span>
+    <span class="o">}</span>
+
+    <span class="kd">protected</span> <span class="kt">void</span>
<span class="nf">closeFirstEntityManager</span><span class="o">(</span><span
class="nd">@Disposes</span> <span class="nd">@First</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">)</span>
+    <span class="o">{</span>
+        <span class="k">if</span> <span class="o">(</span><span
class="n">entityManager</span><span class="o">.</span><span class="na">isOpen</span><span
class="o">())</span>
+        <span class="o">{</span>
+            <span class="n">entityManager</span><span class="o">.</span><span
class="na">close</span><span class="o">();</span>
+        <span class="o">}</span>
+    <span class="o">}</span>
+
+    <span class="nd">@Produces</span>
+    <span class="nd">@Second</span>
+    <span class="nd">@RequestScoped</span>
+    <span class="kd">protected</span> <span class="n">EntityManager</span>
<span class="nf">createSecondEntityManager</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">return</span> <span class="k">this</span><span
class="o">.</span><span class="na">secondEntityManager</span><span
class="o">;</span>
+    <span class="o">}</span>
+
+    <span class="kd">protected</span> <span class="kt">void</span>
<span class="nf">closeSecondEntityManager</span><span class="o">(</span><span
class="nd">@Disposes</span> <span class="nd">@Second</span> <span
class="n">EntityManager</span> <span class="n">entityManager</span><span
class="o">)</span>
+    <span class="o">{</span>
+        <span class="k">if</span> <span class="o">(</span><span
class="n">entityManager</span><span class="o">.</span><span class="na">isOpen</span><span
class="o">())</span>
+        <span class="o">{</span>
+            <span class="n">entityManager</span><span class="o">.</span><span
class="na">close</span><span class="o">();</span>
+        <span class="o">}</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+
+<span class="c1">//...</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">FirstLevelTransactionBean</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="nd">@First</span>
<span class="n">EntityManager</span> <span class="n">firstEntityManager</span><span
class="o">;</span>
+
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">NestedTransactionBean</span>
<span class="n">nestedTransactionBean</span><span class="o">;</span>
+
+    <span class="nd">@Transactional</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">executeInTransaction</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="c1">//...</span>
+        <span class="k">this</span><span class="o">.</span><span
class="na">nestedTransactionBean</span><span class="o">.</span><span
class="na">executeInTransaction</span><span class="o">();</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+
+<span class="c1">//...</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">NestedTransactionBean</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="nd">@Second</span>
<span class="n">EntityManager</span> <span class="n">secondEntityManager</span><span
class="o">;</span>
+
+    <span class="nd">@Transactional</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">executeInTransaction</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="c1">//...</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>The following example shows how to use only the specified <code>EntityManager</code>/s</p>
+<p>Activating entity managers manually:</p>
+<div class="codehilite"><pre><span class="kd">public</span> <span
class="kd">class</span> <span class="nc">MultiTransactionBean</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">defaultEntityManager</span><span class="o">;</span>
+
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="nd">@First</span>
<span class="n">EntityManager</span> <span class="n">firstEntityManager</span><span
class="o">;</span>
+
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="nd">@Second</span>
<span class="n">EntityManager</span> <span class="n">secondEntityManager</span><span
class="o">;</span>
+
+    <span class="nd">@Transactional</span><span class="o">(</span><span
class="n">qualifier</span> <span class="o">=</span> <span class="n">Default</span><span
class="o">.</span><span class="na">class</span><span class="o">)</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">executeInDefaultTransaction</span><span class="o">()</span>
+    <span class="o">{</span>
+    <span class="o">}</span>
+
+    <span class="nd">@Transactional</span><span class="o">(</span><span
class="n">qualifier</span> <span class="o">=</span> <span class="n">First</span><span
class="o">.</span><span class="na">class</span><span class="o">)</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">executeInFirstTransaction</span><span class="o">()</span>
+    <span class="o">{</span>
+    <span class="o">}</span>
+
+    <span class="nd">@Transactional</span><span class="o">(</span><span
class="n">qualifier</span> <span class="o">=</span> <span class="n">Second</span><span
class="o">.</span><span class="na">class</span><span class="o">)</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">executeInSecondTransaction</span><span class="o">()</span>
+    <span class="o">{</span>
+    <span class="o">}</span>
+
+    <span class="nd">@Transactional</span><span class="o">(</span><span
class="n">qualifier</span> <span class="o">=</span> <span class="o">{</span><span
class="n">First</span><span class="o">.</span><span class="na">class</span><span
class="o">,</span> <span class="n">Second</span><span class="o">.</span><span
class="na">class</span><span class="o">})</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">executeInFirstAndSecondTransaction</span><span class="o">()</span>
+    <span class="o">{</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>All examples also work with nested calls. In the following example the transaction
handling is done on the entry point (after FirstLevelTransactionBean#executeInTransaction).</p>
+<p>Joining existing transaction in nested call:</p>
+<div class="codehilite"><pre><span class="c1">//...</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">FirstLevelTransactionBean</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">;</span>
+
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">NestedTransactionBean</span>
<span class="n">nestedTransactionBean</span><span class="o">;</span>
+
+    <span class="nd">@Transactional</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">executeInTransaction</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">this</span><span class="o">.</span><span
class="na">nestedTransactionBean</span><span class="o">.</span><span
class="na">executeInTransaction</span><span class="o">();</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+
+<span class="c1">//...</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">NestedTransactionBean</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">;</span>
+
+    <span class="nd">@Transactional</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">executeInTransaction</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="c1">//...</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>The final transaction handling for all <code>EntityManager</code> s
is also done after the outermost transactional method if <code>NestedTransactionBean</code>
uses a different <code>EntityManager</code>.
+So it's possible to catch an exception in <code>FirstLevelTransactionBean</code>
e.g. to try an optional path instead of an immediate rollback.</p>
+<h1 id="transactionscoped">@TransactionScoped</h1>
+<p><code>@Transactional</code> also starts a context which is available
as long as the transaction started by <code>@Transactional</code>. Besides other
beans you can use this scope for the <code>EntityManager</code> itself. That means
the <code>EntityManager</code> will be closed after leaving the method annotated
with <code>@Transactional</code>.</p>
+<p>Producer for the default EntityManager which should be used only for one transaction:</p>
+<div class="codehilite"><pre><span class="c1">//...</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">EntityManagerProducer</span>
+<span class="o">{</span>
+    <span class="c1">//or manual bootstrapping</span>
+    <span class="nd">@PersistenceContext</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">;</span>
+
+    <span class="nd">@Produces</span>
+    <span class="nd">@TransactionScoped</span>
+    <span class="kd">protected</span> <span class="n">EntityManager</span>
<span class="nf">createEntityManager</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">return</span> <span class="k">this</span><span
class="o">.</span><span class="na">entityManager</span><span class="o">;</span>
+    <span class="o">}</span>
+
+    <span class="kd">protected</span> <span class="kt">void</span>
<span class="nf">closeEntityManager</span><span class="o">(</span><span
class="nd">@Disposes</span> <span class="n">EntityManager</span> <span
class="n">entityManager</span><span class="o">)</span>
+    <span class="o">{</span>
+        <span class="k">if</span> <span class="o">(</span><span
class="n">entityManager</span><span class="o">.</span><span class="na">isOpen</span><span
class="o">())</span>
+        <span class="o">{</span>
+            <span class="n">entityManager</span><span class="o">.</span><span
class="na">close</span><span class="o">();</span>
+        <span class="o">}</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<h1 id="extended-persistence-contexts">Extended Persistence Contexts</h1>
+<p>Frameworks like MyFaces Orchestra provide a feature which allows keeping an <code>EntityManager</code>
across multiple requests. That means it isn't required to call <code>EntityManager#merge</code>
to add detached entities to the context. However, several application architectures don't
allow such an approach (due to different reasons like scalability). In theory that sounds
nice and it works pretty well for small to medium sized projects esp. if an application doesn't
rely on session replication in clusters. That also means that such an approach restricts your
target environment from the very beginning. One of the base problems is that an <code>EntityManager</code>
isn't serializable. Beans which are scoped in a normal-scoped CDI context have to be serializable.
So by default it isn't allowed by CDI to provide a producer-method which exposes e.g. a conversation
scoped <code>EntityManager</code> as it is. We <strong>don't</strong>
recommend to use this approach and therefore it 
 isn't available out-of-the-box. However, if you really need this approach to avoid calling
<code>#merge</code> for your detached entities, it's pretty simple to add this
functionality.</p>
+<p>Usage of a simple <code>ExtendedEntityManager</code></p>
+<div class="codehilite"><pre><span class="nd">@Inject</span>
+<span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">;</span>
+</pre></div>
+
+
+<p>As you see the usage is the same. You <strong>don't</strong> have to
use <code>ExtendedEntityManager</code> at the injection point. It's just needed
in the producer-method:</p>
+<p>Producer for the default Extended-<code>EntityManager</code> (<strong>no
EE-Server</strong>):</p>
+<div class="codehilite"><pre><span class="c1">//...</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">ExtendedEntityManagerProducer</span>
+<span class="o">{</span>
+    <span class="c1">//or manual bootstrapping</span>
+    <span class="nd">@PersistenceContext</span>
+    <span class="kd">private</span> <span class="n">EntityManager</span>
<span class="n">entityManager</span><span class="o">;</span>
+
+    <span class="nd">@Produces</span>
+    <span class="nd">@RequestScoped</span>
+    <span class="kd">protected</span> <span class="n">ExtendedEntityManager</span>
<span class="nf">createEntityManager</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">return</span> <span class="k">new</span>
<span class="nf">ExtendedEntityManager</span><span class="o">(</span><span
class="k">this</span><span class="o">.</span><span class="na">entityManager</span><span
class="o">);</span>
+    <span class="o">}</span>
+
+    <span class="kd">protected</span> <span class="kt">void</span>
<span class="nf">closeEntityManager</span><span class="o">(</span><span
class="nd">@Disposes</span> <span class="n">ExtendedEntityManager</span>
<span class="n">entityManager</span><span class="o">)</span>
+    <span class="o">{</span>
+        <span class="k">if</span> <span class="o">(</span><span
class="n">entityManager</span><span class="o">.</span><span class="na">isOpen</span><span
class="o">())</span>
+        <span class="o">{</span>
+            <span class="n">entityManager</span><span class="o">.</span><span
class="na">close</span><span class="o">();</span>
+        <span class="o">}</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>Producer for the default Extended-<code>EntityManager</code> (<strong>EE-Server</strong>):</p>
+<div class="codehilite"><pre><span class="nd">@ApplicationScoped</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">ExtendedEntityManagerProducer</span>
+<span class="o">{</span>
+    <span class="nd">@PersistenceUnit</span>
+    <span class="kd">private</span> <span class="n">EntityManagerFactory</span>
<span class="n">entityManagerFactory</span><span class="o">;</span>
+
+    <span class="nd">@Produces</span>
+    <span class="nd">@Default</span>
+    <span class="nd">@RequestScoped</span>
+    <span class="kd">public</span> <span class="n">ExtendedEntityManager</span>
<span class="nf">create</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">return</span> <span class="k">new</span>
<span class="nf">ExtendedEntityManager</span><span class="o">(</span><span
class="k">this</span><span class="o">.</span><span class="na">entityManagerFactory</span><span
class="o">.</span><span class="na">createEntityManager</span><span
class="o">());</span>
+    <span class="o">}</span>
+
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">dispose</span><span class="o">(</span><span class="nd">@Disposes</span>
<span class="nd">@Default</span> <span class="n">ExtendedEntityManager</span>
<span class="n">entityManager</span><span class="o">)</span>
+    <span class="o">{</span>
+        <span class="k">if</span> <span class="o">(</span><span
class="n">entityManager</span><span class="o">.</span><span class="na">isOpen</span><span
class="o">())</span>
+        <span class="o">{</span>
+            <span class="n">entityManager</span><span class="o">.</span><span
class="na">close</span><span class="o">();</span>
+        <span class="o">}</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>Implementation of a simple <code>ExtendedEntityManager</code>:</p>
+<div class="codehilite"><pre><span class="nd">@Typed</span><span
class="o">()</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">ExtendedEntityManager</span> <span class="kd">implements</span>
<span class="n">EntityManager</span><span class="o">,</span> <span
class="n">Serializable</span>
+<span class="o">{</span>
+    <span class="kd">private</span> <span class="kd">static</span>
<span class="kd">final</span> <span class="kt">long</span> <span
class="n">serialVersionUID</span> <span class="o">=</span> <span class="mi">3770954229283539616L</span><span
class="o">;</span>
+
+    <span class="kd">private</span> <span class="kd">transient</span>
<span class="n">EntityManager</span> <span class="n">wrapped</span><span
class="o">;</span>
+
+    <span class="kd">protected</span> <span class="nf">ExtendedEntityManager</span><span
class="o">()</span>
+    <span class="o">{</span>
+    <span class="o">}</span>
+
+    <span class="kd">public</span> <span class="nf">ExtendedEntityManager</span><span
class="o">(</span><span class="n">EntityManager</span> <span class="n">wrapped</span><span
class="o">)</span>
+    <span class="o">{</span>
+        <span class="k">this</span><span class="o">.</span><span
class="na">wrapped</span> <span class="o">=</span> <span class="n">wrapped</span><span
class="o">;</span>
+    <span class="o">}</span>
+
+    <span class="cm">/*</span>
+<span class="cm">     * generated</span>
+<span class="cm">     */</span>
+    <span class="c1">//delegate all calls to this.wrapped - most IDEs allow to generate
it</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>This approach just works if it <strong>doesn't come to serialization</strong>
of this wrapper e.g. in case of session-replication.
+If those beans get serialized, you have to overcome this restriction by storing the persistence-unit-name
and recreate the <code>EntityManager</code> via <code>Persistence.createEntityManagerFactory(this.persistenceUnitName).createEntityManager();</code>
and sync it with the database before closing it on serialization.
+Furthermore, you have to intercept some methods of the <code>EntityManager</code>
to merge detached entities automatically if those entities get serialized as well. However,
as mentioned before <strong>we don't recommend</strong> such an approach.</p>
+<h1 id="jta-support">JTA Support</h1>
+<p>Per default the transaction-type used by <code>@Transactional</code>is
'RESOURCE_LOCAL'. If you configure <code>transaction-type="JTA"</code>in the persistence.xml,
you have to enable an alternative <code>TransactionStrategy</code> in the beans.xml
which is called <code>org.apache.deltaspike.jpa.impl.transaction.BeanManagedUserTransactionStrategy</code>.</p>
+<div class="codehilite"><pre><span class="nt">&lt;beans&gt;</span>
+    <span class="nt">&lt;alternatives&gt;</span>
+        <span class="nt">&lt;class&gt;</span>org.apache.deltaspike.jpa.impl.transaction.BeanManagedUserTransactionStrategy<span
class="nt">&lt;/class&gt;</span>
+    <span class="nt">&lt;/alternatives&gt;</span>
+<span class="nt">&lt;/beans&gt;</span>
+</pre></div>
+
+
+<p>If you have multiple persistence-units and you have to use both transaction-types
or the settings for development have to be different than the production settings, you can
use <code>org.apache.deltaspike.jpa.impl.transaction.EnvironmentAwareTransactionStrategy</code>
instead.</p>
+<p><strong>Hint:</strong></p>
+<p>In case of some versions of Weld (or OpenWebBeans in BDA mode), you have to configure
it as global-alternative instead of an <code>&lt;alternatives&gt;</code>
in beans.xml. That means you have to add e.g.:
+<code>globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy=org.apache.deltaspike.jpa.impl.transaction.BeanManagedUserTransactionStrategy</code>
+to /META-INF/apache-deltaspike.properties</p>
+          </div>
+      </div>
+
+      <hr>
+
+      <footer>
+        <p>Copyright © 2011-2014 The Apache Software Foundation, Licensed under
the Apache License, Version 2.0.</p>
+        <p>Apache and the Apache feather logo are trademarks of The Apache Software
Foundation.</p>
+      </footer>
+
+    </div> <!-- /.container -->
+
+    <!-- Javascript
+    ================================================== -->
+    <!-- Placed at the end of the document so the pages load faster -->
+    <!--<script src="./../resources/js/prettyfy.js"></script> -->
+    <script src="./../resources/js/prettyprint.js"></script>
+    <script src="./../resources/js/jquery.js"></script>
+    <script src="./../resources/js/bootstrap-transition.js"></script>
+    <script src="./../resources/js/bootstrap-alert.js"></script>
+    <script src="./../resources/js/bootstrap-modal.js"></script>
+    <script src="./../resources/js/bootstrap-dropdown.js"></script>
+    <script src="./../resources/js/bootstrap-scrollspy.js"></script>
+    <script src="./../resources/js/bootstrap-tab.js"></script>
+    <script src="./../resources/js/bootstrap-tooltip.js"></script>
+    <script src="./../resources/js/bootstrap-popover.js"></script>
+    <script src="./../resources/js/bootstrap-button.js"></script>
+    <script src="./../resources/js/bootstrap-collapse.js"></script>
+    <script src="./../resources/js/bootstrap-carousel.js"></script>
+    <script src="./../resources/js/bootstrap-typeahead.js"></script>
+</body>
+</html>



Mime
View raw message