tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] OpenEJB 3.0.x documentation > Spring EJB and JPA
Date Fri, 11 Sep 2009 14:28:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=OPENEJBx30&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/OPENEJBx30/Spring+EJB+and+JPA">Spring
EJB and JPA</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~jean-louis.monteiro@atosorigin.com">Jean-Louis
MONTEIRO</a>
    </h4>
     Add the mandatory &lt;annotation-config /&gt; tag
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     Add the mandatory &lt;annotation-config /&gt; tag<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <div class='panelMacro'><table class='noteMacro'><colgroup><col
width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/warning.gif"
width="16" height="16" align="absmiddle" alt="" border="0"></td><td>OpenEJB
3.1 or later required</td></tr></table></div>
<p>This example shows how to combine Spring, OpenEJB and Hibernate using the integration
code provided by OpenEJB.  Here, OpenEJB is used as an embeddable EJB container inside of
Spring.  See the <a href="/confluence/display/OPENEJBx30/Spring" title="Spring">Spring</a>
page for details.</p>

<p>We use the basic Movie example and expand it to include more objects to demonstrate
both Spring beans, EJB Session beans, and JPA persistent objects in one application.  The
premise of the example is a Cineplex that has a number of Theaters (viewing screens), each
playing a number of Movies.  The basic object layout is as follows:</p>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Object </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/spring-integration/src/main/java/org/superbiz/spring/CineplexImpl.java"
rel="nofollow">CineplexImpl</a> </td>
<td class='confluenceTd'> @Stateless </td>
<td class='confluenceTd'> Shows the use of @Resource to have Spring beans injected.
Specifically, the <em>Theaters</em> Spring bean </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/spring-integration/src/main/java/org/superbiz/spring/Theaters.java"
rel="nofollow">Theaters</a> </td>
<td class='confluenceTd'> Spring bean </td>
<td class='confluenceTd'> Simple wrapper object injected into <em>CineplexImpl</em>
</td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/spring-integration/src/main/java/org/superbiz/spring/Theater.java"
rel="nofollow">Theater</a> </td>
<td class='confluenceTd'> Spring bean </td>
<td class='confluenceTd'> Shows that EJBs can be injected into Spring beans.  Uses both
the <em>Movies</em> EJB and the <em>Movie</em> JPA objects </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/spring-integration/src/main/java/org/superbiz/spring/MoviesImpl.java"
rel="nofollow">MoviesImpl</a> </td>
<td class='confluenceTd'> @Stateful </td>
<td class='confluenceTd'> Wraps a JPA EntityManager and provides transactional access
to the persistent <em>Movie</em> objects </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/spring-integration/src/main/java/org/superbiz/spring/Movie.java"
rel="nofollow">Movie</a> </td>
<td class='confluenceTd'> @Entity </td>
<td class='confluenceTd'> Basic JPA bean that is used both by Spring beans and EJBs.
 The same <em>Movie</em> object as in all the other persistence related examples.
</td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/spring-integration/src/main/java/org/superbiz/spring/AvailableMovies.java"
rel="nofollow">AvailableMovies</a> </td>
<td class='confluenceTd'> Spring bean </td>
<td class='confluenceTd'> Simple object used as a clever way to seed the EntityManager
(and really, the database) with persistent <em>Movie</em> objects </td>
</tr>
</tbody></table>

<h1><a name="SpringEJBandJPA-Requiredjars"></a>Required jars</h1>

<p>To setup the integration you'll need:</p>

<ol>
	<li>The standard OpenEJB 3.1 libraries</li>
	<li>The <a href="http://people.apache.org/repo/m2-ibiblio-rsync-repository/org/apache/openejb/openejb-spring/3.1/openejb-spring-3.1.jar"
rel="nofollow">openejb-spring-3.1.jar</a> or later</li>
	<li>Spring 2.5 or other (any version should work)</li>
</ol>


<p>In Maven2 this can be done by adding the following dependencies to your pom.xml</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.openejb<span
class="code-tag">&lt;/groupId&gt;</span>
  <span class="code-tag">&lt;artifactId&gt;</span>openejb-core<span
class="code-tag">&lt;/artifactId&gt;</span>
  <span class="code-tag">&lt;version&gt;</span>3.1.2-SNAPSHOT<span
class="code-tag">&lt;/version&gt;</span>
<span class="code-tag">&lt;/dependency&gt;</span>
<span class="code-tag">&lt;dependency&gt;</span>
  <span class="code-tag">&lt;groupId&gt;</span>org.apache.openejb<span
class="code-tag">&lt;/groupId&gt;</span>
  <span class="code-tag">&lt;artifactId&gt;</span>openejb-spring<span
class="code-tag">&lt;/artifactId&gt;</span>
  <span class="code-tag">&lt;version&gt;</span>3.1.2-SNAPSHOT<span
class="code-tag">&lt;/version&gt;</span>
<span class="code-tag">&lt;/dependency&gt;</span>
<span class="code-tag">&lt;dependency&gt;</span>
  <span class="code-tag">&lt;groupId&gt;</span>org.springframework<span
class="code-tag">&lt;/groupId&gt;</span>
  <span class="code-tag">&lt;artifactId&gt;</span>spring<span class="code-tag">&lt;/artifactId&gt;</span>
  <span class="code-tag">&lt;version&gt;</span>2.5.5<span class="code-tag">&lt;/version&gt;</span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>
<p>For other environments, you can simply <a href="/confluence/display/OPENEJB/Download"
title="Download">download an openejb-3.1.zip</a> or later and include all the jars
under the lib/ directory in your classpath.  Then download and add the <a href="http://people.apache.org/repo/m2-ibiblio-rsync-repository/org/apache/openejb/openejb-spring/3.1/openejb-spring-3.1.jar"
rel="nofollow">openejb-spring-3.1.jar</a> along with your Spring jars.</p>

<h1><a name="SpringEJBandJPA-TheSpringxml"></a>The Spring xml</h1>

<p>Bootstrapping and Configuring OpenEJB is fairly simple.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;!--
Required:  Set up a TransactionManager for use by OpenEJB or Spring.
The Spring PlatformTransactionManager may be used instead.

In future versions this will not be a required step.
--&gt;
<span class="code-tag">&lt;bean name=<span class="code-quote">"TransactionManager"</span>
class=<span class="code-quote">"org.apache.geronimo.transaction.manager.GeronimoTransactionManager"</span>/&gt;</span>


&lt;!--
Required:  Set up a SecurityService for use by OpenEJB.
In future versions this will not be a required step.
--&gt;
<span class="code-tag">&lt;bean name=<span class="code-quote">"SecurityService"</span>
class=<span class="code-quote">"org.apache.openejb.core.security.SecurityServiceImpl"</span>/&gt;</span>


&lt;!--
Loads the EJBs from the classpath just as when embedding OpenEJB via
the org.apache.openejb.client.LocalInitialContextFactory.  All the discovered
EJBs are imported into this context and available for injection here.
--&gt;
<span class="code-tag">&lt;bean name=<span class="code-quote">"classPathApplication"</span>
class=<span class="code-quote">"org.apache.openejb.spring.ClassPathApplication"</span>/&gt;</span>

</pre>
</div></div>
<p>As well, you can optionally declare any resources or containers.  Anything declarable
as a &lt;Resource&gt; or &lt;Container&gt; in the openejb.xml can instead
be declared in the Spring xml file as shown here.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean name=<span class="code-quote">"MovieDatabase"</span>
class=<span class="code-quote">"org.apache.openejb.spring.Resource"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"type"</span>
value=<span class="code-quote">"DataSource"</span>/&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"properties"</span>&gt;</span>
    <span class="code-tag">&lt;props&gt;</span>
      <span class="code-tag">&lt;prop key=<span class="code-quote">"JdbcDriver"</span>&gt;</span>org.hsqldb.jdbcDriver<span
class="code-tag">&lt;/prop&gt;</span>
      <span class="code-tag">&lt;prop key=<span class="code-quote">"JdbcUrl"</span>&gt;</span>jdbc:hsqldb:mem:moviedb<span
class="code-tag">&lt;/prop&gt;</span>
    <span class="code-tag">&lt;/props&gt;</span>
  <span class="code-tag">&lt;/property&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean name=<span class="code-quote">"MovieDatabaseUnmanaged"</span>
class=<span class="code-quote">"org.apache.openejb.spring.Resource"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"type"</span>
value=<span class="code-quote">"DataSource"</span>/&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"properties"</span>&gt;</span>
    <span class="code-tag">&lt;props&gt;</span>
      <span class="code-tag">&lt;prop key=<span class="code-quote">"JdbcDriver"</span>&gt;</span>org.hsqldb.jdbcDriver<span
class="code-tag">&lt;/prop&gt;</span>
      <span class="code-tag">&lt;prop key=<span class="code-quote">"JdbcUrl"</span>&gt;</span>jdbc:hsqldb:mem:moviedb<span
class="code-tag">&lt;/prop&gt;</span>
      <span class="code-tag">&lt;prop key=<span class="code-quote">"JtaManaged"</span>&gt;</span>false<span
class="code-tag">&lt;/prop&gt;</span>
    <span class="code-tag">&lt;/props&gt;</span>
  <span class="code-tag">&lt;/property&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

</pre>
</div></div>
<p>And finally our Spring beans.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;bean name=<span class="code-quote">"AvailableMovies"</span>
class=<span class="code-quote">"org.superbiz.spring.AvailableMovies"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"movies"</span>&gt;</span>
    <span class="code-tag">&lt;list&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.superbiz.spring.Movie"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"title"</span>
value=<span class="code-quote">"Fargo"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"director"</span>
value=<span class="code-quote">"Joel Coen"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"year"</span>
value=<span class="code-quote">"1996"</span>/&gt;</span>
      <span class="code-tag">&lt;/bean&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.superbiz.spring.Movie"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"title"</span>
value=<span class="code-quote">"Reservoir Dogs"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"director"</span>
value=<span class="code-quote">"Quentin Tarantino"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"year"</span>
value=<span class="code-quote">"1992"</span>/&gt;</span>
      <span class="code-tag">&lt;/bean&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.superbiz.spring.Movie"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"title"</span>
value=<span class="code-quote">"The Big Lebowski"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"director"</span>
value=<span class="code-quote">"Joel Coen"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"year"</span>
value=<span class="code-quote">"1998"</span>/&gt;</span>
      <span class="code-tag">&lt;/bean&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.superbiz.spring.Movie"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"title"</span>
value=<span class="code-quote">"You, Me and Dupree"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"director"</span>
value=<span class="code-quote">"Anthony Russo"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"year"</span>
value=<span class="code-quote">"2006"</span>/&gt;</span>
      <span class="code-tag">&lt;/bean&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.superbiz.spring.Movie"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"title"</span>
value=<span class="code-quote">"Wedding Crashers"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"director"</span>
value=<span class="code-quote">"David Dobkin"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"year"</span>
value=<span class="code-quote">"2005"</span>/&gt;</span>
      <span class="code-tag">&lt;/bean&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.superbiz.spring.Movie"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"title"</span>
value=<span class="code-quote">"Zoolander"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"director"</span>
value=<span class="code-quote">"Ben Stiller"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"year"</span>
value=<span class="code-quote">"2001"</span>/&gt;</span>
      <span class="code-tag">&lt;/bean&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.superbiz.spring.Movie"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"title"</span>
value=<span class="code-quote">"Shanghai Noon"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"director"</span>
value=<span class="code-quote">"Tom Dey"</span>/&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"year"</span>
value=<span class="code-quote">"2000"</span>/&gt;</span>
      <span class="code-tag">&lt;/bean&gt;</span>
    <span class="code-tag">&lt;/list&gt;</span>
  <span class="code-tag">&lt;/property&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean name=<span class="code-quote">"theater1"</span>
class=<span class="code-quote">"org.superbiz.spring.Theater"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"nowPlaying"</span>&gt;</span>
    <span class="code-tag">&lt;list&gt;</span>
      <span class="code-tag">&lt;value&gt;</span>Fargo<span class="code-tag">&lt;/value&gt;</span>
      <span class="code-tag">&lt;value&gt;</span>Reservoir Dogs<span
class="code-tag">&lt;/value&gt;</span>
      <span class="code-tag">&lt;value&gt;</span>The Big Lebowski<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">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean name=<span class="code-quote">"theater2"</span>
class=<span class="code-quote">"org.superbiz.spring.Theater"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"nowPlaying"</span>&gt;</span>
    <span class="code-tag">&lt;list&gt;</span>
      <span class="code-tag">&lt;value&gt;</span>You, Me and Dupree<span
class="code-tag">&lt;/value&gt;</span>
      <span class="code-tag">&lt;value&gt;</span>Wedding Crashers<span
class="code-tag">&lt;/value&gt;</span>
      <span class="code-tag">&lt;value&gt;</span>Zoolander<span class="code-tag">&lt;/value&gt;</span>
      <span class="code-tag">&lt;value&gt;</span>Shanghai Noon<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">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean name=<span class="code-quote">"theaters"</span>
class=<span class="code-quote">"org.superbiz.spring.Theaters"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"theaters"</span>&gt;</span>
    <span class="code-tag">&lt;list&gt;</span>
      <span class="code-tag">&lt;ref bean=<span class="code-quote">"theater1"</span>/&gt;</span>
      <span class="code-tag">&lt;ref bean=<span class="code-quote">"theater2"</span>/&gt;</span>
    <span class="code-tag">&lt;/list&gt;</span>
  <span class="code-tag">&lt;/property&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>
<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Don't forget</b><br
/><div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;context:annotation-config/&gt;</span>

</pre>
</div></div>
<p>It allows various annotations to be detected in bean classes: Spring's @Required
and @Autowired, as well as JSR 250's @PostConstruct, @PreDestroy and @Resource (if available),
JAX-WS's  @WebServiceRef (if available), EJB3's @EJB (if available), and JPA's @PersistenceContext
and @PersistenceUnit (if available). Alternatively, you may choose to activate the individual
BeanPostProcessors for those annotations.</p></td></tr></table></div>

<h1><a name="SpringEJBandJPA-TheCode"></a>The Code</h1>

<p>In efforts to keep the example page somewhat short, we'll show just three beans,
each demonstrating a particular relationship.</p>

<p>The first is the CineplexImpl EJB which shows EJB &#45;&gt; Spring.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@Stateless
<span class="code-keyword">public</span> class CineplexImpl <span class="code-keyword">implements</span>
Cineplex {

    /**
     * The Theaters Spring bean will be injected
     */
    @Resource
    <span class="code-keyword">private</span> Theaters theaters;

    <span class="code-keyword">public</span> List&lt;Theater&gt; getTheaters()
{
        <span class="code-keyword">return</span> theaters.getTheaters();
    }
}
</pre>
</div></div>

<p>The second is the Theater Spring bean which shows Spring &#45;&gt; EJB.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">/**
 * Spring bean that references the Movies EJB and the Movie JPA bean.
 * &lt;p/&gt;
 * This bean shows that Spring beans can have references to EJBs.
 */
<span class="code-keyword">public</span> class Theater {

    /**
     * The Movies @Stateless EJB
     */
    <span class="code-keyword">private</span> <span class="code-keyword">final</span>
Movies movies;

    <span class="code-keyword">private</span> <span class="code-keyword">final</span>
List&lt;Movie&gt; nowPlaying = <span class="code-keyword">new</span> ArrayList&lt;Movie&gt;();

    /**
     * The Movies EJB is passed in on the constructor which
     * guarantees we can use it in the setNowPlaying method.
     *
     * @param movies
     */
    @Autowired
    <span class="code-keyword">public</span> Theater(Movies movies) {
        <span class="code-keyword">this</span>.movies = movies;
    }

    /**
     * For every title in the list we will use the Movies EJB
     * to lookup the actual Movie JPA object.
     *
     * @param nowPlaying
     * @<span class="code-keyword">throws</span> Exception
     */
    <span class="code-keyword">public</span> void setNowPlaying(List&lt;<span
class="code-object">String</span>&gt; nowPlaying) <span class="code-keyword">throws</span>
Exception {
        <span class="code-keyword">for</span> (<span class="code-object">String</span>
title : nowPlaying) {
            <span class="code-keyword">this</span>.nowPlaying.add(movies.getMovieByTitle(title));
        }
    }

    <span class="code-keyword">public</span> List&lt;Movie&gt; getMovies()
<span class="code-keyword">throws</span> Exception {
        <span class="code-keyword">return</span> nowPlaying;
    }
}
</pre>
</div></div>

<p>The last is the AvailableMovies Spring bean which Shows Spring &#45;&gt;
EJB &#45;&gt; JPA</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">/**
 * This is a simple Spring bean that we use as an easy way
 * to seed the example with a list of persistent Movie objects
 * &lt;p/&gt;
 * The individual Movie objects are constructed by Spring, then
 * passed into the Movies EJB where they are transactionally
 * persisted with the EntityManager.
 */
<span class="code-keyword">public</span> class AvailableMovies {

    @EJB
    <span class="code-keyword">private</span> Movies moviesEjb;

    <span class="code-keyword">private</span> List&lt;Movie&gt; movies;

    @PostConstruct
    <span class="code-keyword">public</span> void construct() <span class="code-keyword">throws</span>
Exception {
        <span class="code-keyword">for</span> (Movie movie : movies) {
            moviesEjb.addMovie(movie);
        }
    }

    <span class="code-keyword">public</span> List&lt;Movie&gt; getMovies()
{
        <span class="code-keyword">return</span> movies;
    }

    <span class="code-keyword">public</span> void setMovies(List&lt;Movie&gt;
movies) {
        <span class="code-keyword">this</span>.movies = movies;
    }
}
</pre>
</div></div>

<h1><a name="SpringEJBandJPA-TheTestCase"></a>The TestCase</h1>

<p>The JUnit TestCase uses a ClassPathXmlApplicationContext to load the Spring ApplicationContext.
 Anything that loads your Spring xml file should work fine.  The following code would work
a plain java app as well.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class MoviesTest
<span class="code-keyword">extends</span> TestCase {

    <span class="code-keyword">public</span> void test() <span class="code-keyword">throws</span>
Exception {
        ClassPathXmlApplicationContext context = <span class="code-keyword">new</span>
ClassPathXmlApplicationContext(<span class="code-quote">"movies.xml"</span>);

        <span class="code-comment">// Can I lookup the Cineplex EJB via the Spring ApplicationContext
</span>        Cineplex cineplex = (Cineplex) context.getBean(<span class="code-quote">"CineplexImplLocal"</span>);
        assertNotNull(cineplex);

        <span class="code-comment">// Does the Cineplex EJB have a reference to the
Theaters Spring bean?
</span>        List&lt;Theater&gt; theaters = cineplex.getTheaters();
        assertNotNull(theaters);

        assertEquals(2, theaters.size());

        Theater theaterOne = theaters.get(0);
        Theater theaterTwo = theaters.get(1);


        <span class="code-comment">// Were the Theater Spring beans able to use the
</span>        <span class="code-comment">// Movies EJB to get references to the
Movie JPA objects?
</span>        List&lt;Movie&gt; theaterOneMovies = theaterOne.getMovies();
        assertNotNull(theaterOneMovies);

        List&lt;Movie&gt; theaterTwoMovies = theaterTwo.getMovies();
        assertNotNull(theaterTwoMovies);

        <span class="code-comment">// The first Theater should have used the Movies
EJB
</span>        <span class="code-comment">// to get a reference to three Movie
JPA objects
</span>        assertEquals(3, theaterOneMovies.size());

        assertEquals(<span class="code-quote">"Fargo"</span>, theaterOneMovies.get(0).getTitle());
        assertEquals(<span class="code-quote">"Reservoir Dogs"</span>, theaterOneMovies.get(1).getTitle());
        assertEquals(<span class="code-quote">"The Big Lebowski"</span>, theaterOneMovies.get(2).getTitle());

        <span class="code-comment">// The second Theater should have used the Movies
EJB
</span>        <span class="code-comment">// to get a reference to four Movie
JPA objects
</span>
        assertEquals(4, theaterTwoMovies.size());

        assertEquals(<span class="code-quote">"You, Me and Dupree"</span>, theaterTwoMovies.get(0).getTitle());
        assertEquals(<span class="code-quote">"Wedding Crashers"</span>, theaterTwoMovies.get(1).getTitle());
        assertEquals(<span class="code-quote">"Zoolander"</span>, theaterTwoMovies.get(2).getTitle());
        assertEquals(<span class="code-quote">"Shanghai Noon"</span>, theaterTwoMovies.get(3).getTitle());
    }
}
</pre>
</div></div>

<h1><a name="SpringEJBandJPA-Running"></a>Running</h1>

<p>The source for this example can be downloaded from svn via:</p>

<p>$ svn co <a href="http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/spring-integration"
rel="nofollow">http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/spring-integration</a></p>

<p>Then, in the "spring-integration" directory, run:</p>

<p>$ mvn clean install</p>

<p>Which should create output like the following.</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.superbiz.spring.MoviesTest
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Apache OpenEJB 3.1    build: 20081009-03:31
http://openejb.apache.org/
INFO - openejb.home = /Users/dblevins/work/openejb3/examples/spring-integration
INFO - openejb.base = /Users/dblevins/work/openejb3/examples/spring-integration
INFO - Configuring Service(id=Default JDK 1.3 ProxyFactory, type=ProxyFactory, provider-id=Default
JDK 1.3 ProxyFactory)
INFO - Configuring Service(id=MovieDatabase, type=Resource, provider-id=Default JDBC Database)
INFO - Configuring Service(id=MovieDatabaseUnmanaged, type=Resource, provider-id=Default JDBC
Database)
INFO - Found EjbModule in classpath: /Users/dblevins/work/openejb3/examples/spring-integration/target/classes
INFO - Beginning load: /Users/dblevins/work/openejb3/examples/spring-integration/target/classes
INFO - Configuring enterprise application: classpath.ear
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default
Stateless Container)
INFO - Auto-creating a container for bean CineplexImpl: Container(type=STATELESS, id=Default
Stateless Container)
INFO - Auto-linking resource-ref 'org.superbiz.spring.CineplexImpl/theaters' in bean CineplexImpl
to Resource(id=theaters)
INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default
Stateful Container)
INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=Default Stateful
Container)
INFO - Configuring PersistenceUnit(name=movie-unit, provider=org.hibernate.ejb.HibernatePersistence)
INFO - Enterprise application "classpath.ear" loaded.
INFO - Assembling app: classpath.ear
INFO - PersistenceUnit(name=movie-unit, provider=org.hibernate.ejb.HibernatePersistence)
INFO - Jndi(name=CineplexImplLocal) --&gt; Ejb(deployment-id=CineplexImpl)
INFO - Jndi(name=MoviesLocal) --&gt; Ejb(deployment-id=Movies)
INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
INFO - Created Ejb(deployment-id=CineplexImpl, ejb-name=CineplexImpl, container=Default Stateless
Container)
INFO - Deployed Application(path=classpath.ear)
INFO - Exported EJB Movies with interface org.superbiz.spring.Movies to Spring bean MoviesLocal
INFO - Exported EJB CineplexImpl with interface org.superbiz.spring.Cineplex to Spring bean
CineplexImplLocal
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.141 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
</pre>
</div></div>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/OPENEJBx30/Spring+EJB+and+JPA">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=100439&revisedVersion=5&originalVersion=4">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/OPENEJBx30/Spring+EJB+and+JPA?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message