| |
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <HTML> |
| <HEAD> |
| <!-- $PAGETITLE --> |
| <TITLE>OpenEJB - Spring EJB and JPA</TITLE> |
| <LINK href="http://openejb.apache.org/all.css" rel="stylesheet" type="text/css"> |
| <!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" href="openejb.apache.org/ie.css"><![endif]--> |
| |
| <LINK rel="SHORTCUT ICON" href="http://openejb.apache.org/images/favicon.ico"> |
| <META http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
| </HEAD> |
| <BODY> |
| |
| <!-- Delay the loading of the external javascript file needed for labels (as it takes too long to load and visibly holds loading of the page body) --> |
| <!-- To do this without javascript errors over undefined functions, we need to declare stubs here (that are overrided later by the proper implementations) --> |
| <SCRIPT language="JavaScript" type="text/javascript"> |
| function doAddLabel(hideTextfieldAfterAddParam) |
| { |
| // stub |
| } |
| |
| function onAddLabel() |
| { |
| // stub |
| } |
| |
| function showLabelsInput() |
| { |
| // stub |
| } |
| </SCRIPT> |
| |
| <A name="top"></A> |
| <TABLE class="frameTable" cellpadding="0" cellspacing="0" border="0"> |
| <TR class="Row1"> |
| <TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col2"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col3"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| </TR> |
| <TR class="Row2"> |
| <TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col2"> </TD> |
| <TD class="Col3" id="breadcrumbs"> |
| <!-- $TOP_NAV_BAR --> |
| <A href="../OPENEJB/index.html" title="Index">Home</A> | <A href="../OPENEJB/download.html" title="Download">Download</A> | <A href="../OPENEJB/mailing-lists.html" title="Mailing Lists">Lists</A> | <A href="http://issues.apache.org/jira/browse/OPENEJB" class="external-link" rel="nofollow">Issues</A> |
| |
| </TD> |
| <TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"> </TD> |
| </TR> |
| <TR class="Row3"> |
| <TD class="Col1"><IMG alt="" class="Row3Img" id="thinLine" src="http://openejb.apache.org/images/line_sm.gif"></TD> |
| <TD class="Col2"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col3"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| </TR> |
| <TR class="Row4"> |
| <TD class="Col1"> |
| <SPAN id="Navigation"> |
| |
| <H3><A name="Navigation-Overview"></A>Overview</H3> |
| |
| <UL class="alternate" type="square"> |
| <LI><A href="../OPENEJB/index.html" title="Index">Home</A></LI> |
| <LI><A href="../OPENEJB/news.html" title="News">News</A></LI> |
| <LI><A href="../OPENEJB/faq.html" title="FAQ">FAQ</A></LI> |
| <LI><A href="../OPENEJB/download.html" title="Download">Download</A></LI> |
| <LI><A href="index.html" title="Index">Documentation</A></LI> |
| <LI><A href="../OPENEJB/examples.html" title="Examples">Examples</A></LI> |
| <LI><A href="http://cwiki.apache.org/confluence/display/OPENEJB/Lightening%20Demos" class="external-link" rel="nofollow">Lightning Demos</A></LI> |
| <LI><A href="../OPENEJB/mailing-lists.html" title="Mailing Lists">Mailing Lists</A></LI> |
| <LI><A href="../OPENEJB/source-code.html" title="Source Code">Source Code</A></LI> |
| <LI><A href="http://blogs.apache.org/openejb" class="external-link" rel="nofollow">Project Blog</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Servers"></A>Servers</H3> |
| |
| <UL class="alternate" type="square"> |
| <LI><A href="../OPENEJB/local-server.html" title="Local Server">Local</A></LI> |
| <LI><A href="../OPENEJB/remote-server.html" title="Remote Server">Remote</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Integrations"></A>Integrations</H3> |
| |
| <UL class="alternate" type="square"> |
| <LI><A href="tomcat.html" title="Tomcat">Tomcat</A></LI> |
| <LI><A href="../OPENEJB/geronimo.html" title="Geronimo">Geronimo</A></LI> |
| <LI><A href="../OPENEJB/webobjects.html" title="WebObjects">WebObjects</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Community"></A>Community</H3> |
| |
| <UL class="alternate" type="square"> |
| <LI><A href="../OPENEJB/team.html" title="Team">Team</A></LI> |
| <LI><A href="../OPENEJB/articles.html" title="Articles">Articles</A></LI> |
| <LI><A href="http://webchat.freenode.net/?channels=openejb" class="external-link" rel="nofollow">IRC</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-RelatedProjects"></A>Related Projects</H3> |
| |
| <UL class="alternate" type="square"> |
| <LI><A href="http://activemq.apache.org/" class="external-link" rel="nofollow">ActiveMQ</A></LI> |
| <LI><A href="http://openjpa.apache.org/" class="external-link" rel="nofollow">OpenJPA</A></LI> |
| <LI><A href="http://cxf.apache.org/" class="external-link" rel="nofollow">CXF</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Index"></A>Index</H3> |
| <UL class="alternate" type="square"> |
| <LI><A href="../OPENEJB/space-index.html" title="Space Index">Site Index</A></LI> |
| <LI><A href="space-index.html" title="Space Index">Doc Index</A></LI> |
| </UL> |
| |
| <H3> |
| <A name="Navigation-Feeds"></A> |
| Feeds |
| </H3> |
| |
| <UL class="feeds"> |
| <LI> |
| <A href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false"> |
| <IMG src="http://openejb.apache.org/images/rss.gif"></A> |
| <A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false">Site</A> |
| </LI> |
| |
| <LI><A href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB"> |
| <IMG src="http://openejb.apache.org/images/rss.gif"></A> |
| <A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB">News</A> |
| </LI> |
| </UL> |
| </SPAN> |
| </TD> |
| <TD class="Col2"> </TD> |
| <TD class="Col3"> |
| <TABLE id="PageHeader" border="0" width="100%"> |
| <TR> |
| <TD> |
| <A href="http://openejb.org/"> |
| <IMG hspace="0" src="http://openejb.apache.org/images/logo_openejb.gif" vspace="0"> |
| </A> |
| </TD> |
| <TD align="right"> |
| <A href="http://www.apache.org/"> |
| <IMG src="http://www.apache.org/images/asf-logo.gif" width="258" height="66"> |
| </A> |
| </TD> |
| </TR> |
| <TR> |
| <TD id="page_title"> |
| <!-- $TITLE --> |
| Spring EJB and JPA |
| </TD> |
| |
| <TD align="right"> |
| <BR><BR> |
| <!-- Google CSE Search Box Begins --> |
| <FORM id="searchbox_010475492895890475512:_t4iqjrgx90" action="http://www.google.com/cse"> |
| <INPUT type="hidden" name="cx" value="010475492895890475512:_t4iqjrgx90"> |
| <INPUT type="hidden" name="cof" value="FORID:0"> |
| <INPUT name="q" type="text" size="25"> |
| <INPUT type="submit" name="sa" value="Search"> |
| </FORM> |
| <SCRIPT type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010475492895890475512:_t4iqjrgx90"></SCRIPT> |
| <!-- Google CSE Search Box Ends --> |
| |
| </TD> |
| </TR> |
| </TABLE> |
| <P> |
| <!-- $BODY --> |
| <DIV id="PageContent"> |
| <DIV class="panelMacro"><TABLE class="noteMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="https://cwiki.apache.org/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="spring.html" 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> |
| |
| <DIV class="table-wrap"> |
| <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" class="external-link" 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" class="external-link" 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" class="external-link" 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" class="external-link" 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" class="external-link" 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" class="external-link" 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> |
| </DIV> |
| |
| |
| <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="https://repository.apache.org/content/groups/public/org/apache/openejb/openejb-spring/3.1.2/openejb-spring-3.1.2.jar" class="external-link" 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"><dependency></SPAN> |
| <SPAN class="code-tag"><groupId></SPAN>org.apache.openejb<SPAN class="code-tag"></groupId></SPAN> |
| <SPAN class="code-tag"><artifactId></SPAN>openejb-core<SPAN class="code-tag"></artifactId></SPAN> |
| <SPAN class="code-tag"><version></SPAN>3.1.4<SPAN class="code-tag"></version></SPAN> |
| <SPAN class="code-tag"></dependency></SPAN> |
| <SPAN class="code-tag"><dependency></SPAN> |
| <SPAN class="code-tag"><groupId></SPAN>org.apache.openejb<SPAN class="code-tag"></groupId></SPAN> |
| <SPAN class="code-tag"><artifactId></SPAN>openejb-spring<SPAN class="code-tag"></artifactId></SPAN> |
| <SPAN class="code-tag"><version></SPAN>3.1.4<SPAN class="code-tag"></version></SPAN> |
| <SPAN class="code-tag"></dependency></SPAN> |
| <SPAN class="code-tag"><dependency></SPAN> |
| <SPAN class="code-tag"><groupId></SPAN>org.springframework<SPAN class="code-tag"></groupId></SPAN> |
| <SPAN class="code-tag"><artifactId></SPAN>spring<SPAN class="code-tag"></artifactId></SPAN> |
| <SPAN class="code-tag"><version></SPAN>2.5.5<SPAN class="code-tag"></version></SPAN> |
| <SPAN class="code-tag"></dependency></SPAN> |
| </PRE> |
| </DIV></DIV> |
| <P>For other environments, you can simply <A href="../OPENEJB/download.html" 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" class="external-link" 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"> |
| <!-- |
| 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. |
| --> |
| <SPAN class="code-tag"><bean name=<SPAN class="code-quote">"TransactionManager"</SPAN> class=<SPAN class="code-quote">"org.apache.geronimo.transaction.manager.GeronimoTransactionManager"</SPAN>/></SPAN> |
| |
| |
| <!-- |
| Required: Set up a SecurityService for use by OpenEJB. |
| In future versions this will not be a required step. |
| --> |
| <SPAN class="code-tag"><bean name=<SPAN class="code-quote">"SecurityService"</SPAN> class=<SPAN class="code-quote">"org.apache.openejb.core.security.SecurityServiceImpl"</SPAN>/></SPAN> |
| |
| |
| <!-- |
| 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. |
| --> |
| <SPAN class="code-tag"><bean name=<SPAN class="code-quote">"classPathApplication"</SPAN> class=<SPAN class="code-quote">"org.apache.openejb.spring.ClassPathApplication"</SPAN>/></SPAN> |
| |
| </PRE> |
| </DIV></DIV> |
| <P>As well, you can optionally declare any resources or containers. Anything declarable as a <Resource> or <Container> 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"><bean name=<SPAN class="code-quote">"MovieDatabase"</SPAN> class=<SPAN class="code-quote">"org.apache.openejb.spring.Resource"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"type"</SPAN> value=<SPAN class="code-quote">"DataSource"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"properties"</SPAN>></SPAN> |
| <SPAN class="code-tag"><props></SPAN> |
| <SPAN class="code-tag"><prop key=<SPAN class="code-quote">"JdbcDriver"</SPAN>></SPAN>org.hsqldb.jdbcDriver<SPAN class="code-tag"></prop></SPAN> |
| <SPAN class="code-tag"><prop key=<SPAN class="code-quote">"JdbcUrl"</SPAN>></SPAN>jdbc:hsqldb:mem:moviedb<SPAN class="code-tag"></prop></SPAN> |
| <SPAN class="code-tag"></props></SPAN> |
| <SPAN class="code-tag"></property></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| |
| <SPAN class="code-tag"><bean name=<SPAN class="code-quote">"MovieDatabaseUnmanaged"</SPAN> class=<SPAN class="code-quote">"org.apache.openejb.spring.Resource"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"type"</SPAN> value=<SPAN class="code-quote">"DataSource"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"properties"</SPAN>></SPAN> |
| <SPAN class="code-tag"><props></SPAN> |
| <SPAN class="code-tag"><prop key=<SPAN class="code-quote">"JdbcDriver"</SPAN>></SPAN>org.hsqldb.jdbcDriver<SPAN class="code-tag"></prop></SPAN> |
| <SPAN class="code-tag"><prop key=<SPAN class="code-quote">"JdbcUrl"</SPAN>></SPAN>jdbc:hsqldb:mem:moviedb<SPAN class="code-tag"></prop></SPAN> |
| <SPAN class="code-tag"><prop key=<SPAN class="code-quote">"JtaManaged"</SPAN>></SPAN>false<SPAN class="code-tag"></prop></SPAN> |
| <SPAN class="code-tag"></props></SPAN> |
| <SPAN class="code-tag"></property></SPAN> |
| <SPAN class="code-tag"></bean></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"><bean name=<SPAN class="code-quote">"AvailableMovies"</SPAN> class=<SPAN class="code-quote">"org.superbiz.spring.AvailableMovies"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"movies"</SPAN>></SPAN> |
| <SPAN class="code-tag"><list></SPAN> |
| <SPAN class="code-tag"><bean class=<SPAN class="code-quote">"org.superbiz.spring.Movie"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"title"</SPAN> value=<SPAN class="code-quote">"Fargo"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"director"</SPAN> value=<SPAN class="code-quote">"Joel Coen"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"year"</SPAN> value=<SPAN class="code-quote">"1996"</SPAN>/></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| <SPAN class="code-tag"><bean class=<SPAN class="code-quote">"org.superbiz.spring.Movie"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"title"</SPAN> value=<SPAN class="code-quote">"Reservoir Dogs"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"director"</SPAN> value=<SPAN class="code-quote">"Quentin Tarantino"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"year"</SPAN> value=<SPAN class="code-quote">"1992"</SPAN>/></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| <SPAN class="code-tag"><bean class=<SPAN class="code-quote">"org.superbiz.spring.Movie"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"title"</SPAN> value=<SPAN class="code-quote">"The Big Lebowski"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"director"</SPAN> value=<SPAN class="code-quote">"Joel Coen"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"year"</SPAN> value=<SPAN class="code-quote">"1998"</SPAN>/></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| <SPAN class="code-tag"><bean class=<SPAN class="code-quote">"org.superbiz.spring.Movie"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"title"</SPAN> value=<SPAN class="code-quote">"You, Me and Dupree"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"director"</SPAN> value=<SPAN class="code-quote">"Anthony Russo"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"year"</SPAN> value=<SPAN class="code-quote">"2006"</SPAN>/></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| <SPAN class="code-tag"><bean class=<SPAN class="code-quote">"org.superbiz.spring.Movie"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"title"</SPAN> value=<SPAN class="code-quote">"Wedding Crashers"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"director"</SPAN> value=<SPAN class="code-quote">"David Dobkin"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"year"</SPAN> value=<SPAN class="code-quote">"2005"</SPAN>/></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| <SPAN class="code-tag"><bean class=<SPAN class="code-quote">"org.superbiz.spring.Movie"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"title"</SPAN> value=<SPAN class="code-quote">"Zoolander"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"director"</SPAN> value=<SPAN class="code-quote">"Ben Stiller"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"year"</SPAN> value=<SPAN class="code-quote">"2001"</SPAN>/></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| <SPAN class="code-tag"><bean class=<SPAN class="code-quote">"org.superbiz.spring.Movie"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"title"</SPAN> value=<SPAN class="code-quote">"Shanghai Noon"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"director"</SPAN> value=<SPAN class="code-quote">"Tom Dey"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"year"</SPAN> value=<SPAN class="code-quote">"2000"</SPAN>/></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| <SPAN class="code-tag"></list></SPAN> |
| <SPAN class="code-tag"></property></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| |
| <SPAN class="code-tag"><bean name=<SPAN class="code-quote">"theater1"</SPAN> class=<SPAN class="code-quote">"org.superbiz.spring.Theater"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"nowPlaying"</SPAN>></SPAN> |
| <SPAN class="code-tag"><list></SPAN> |
| <SPAN class="code-tag"><value></SPAN>Fargo<SPAN class="code-tag"></value></SPAN> |
| <SPAN class="code-tag"><value></SPAN>Reservoir Dogs<SPAN class="code-tag"></value></SPAN> |
| <SPAN class="code-tag"><value></SPAN>The Big Lebowski<SPAN class="code-tag"></value></SPAN> |
| <SPAN class="code-tag"></list></SPAN> |
| <SPAN class="code-tag"></property></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| |
| <SPAN class="code-tag"><bean name=<SPAN class="code-quote">"theater2"</SPAN> class=<SPAN class="code-quote">"org.superbiz.spring.Theater"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"nowPlaying"</SPAN>></SPAN> |
| <SPAN class="code-tag"><list></SPAN> |
| <SPAN class="code-tag"><value></SPAN>You, Me and Dupree<SPAN class="code-tag"></value></SPAN> |
| <SPAN class="code-tag"><value></SPAN>Wedding Crashers<SPAN class="code-tag"></value></SPAN> |
| <SPAN class="code-tag"><value></SPAN>Zoolander<SPAN class="code-tag"></value></SPAN> |
| <SPAN class="code-tag"><value></SPAN>Shanghai Noon<SPAN class="code-tag"></value></SPAN> |
| <SPAN class="code-tag"></list></SPAN> |
| <SPAN class="code-tag"></property></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| |
| <SPAN class="code-tag"><bean name=<SPAN class="code-quote">"theaters"</SPAN> class=<SPAN class="code-quote">"org.superbiz.spring.Theaters"</SPAN>></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"theaters"</SPAN>></SPAN> |
| <SPAN class="code-tag"><list></SPAN> |
| <SPAN class="code-tag"><ref bean=<SPAN class="code-quote">"theater1"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><ref bean=<SPAN class="code-quote">"theater2"</SPAN>/></SPAN> |
| <SPAN class="code-tag"></list></SPAN> |
| <SPAN class="code-tag"></property></SPAN> |
| <SPAN class="code-tag"></bean></SPAN> |
| </PRE> |
| </DIV></DIV> |
| <DIV class="panelMacro"><TABLE class="noteMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="https://cwiki.apache.org/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"><context:annotation-config/></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 -> 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<Theater> getTheaters() { |
| <SPAN class="code-keyword">return</SPAN> theaters.getTheaters(); |
| } |
| } |
| </PRE> |
| </DIV></DIV> |
| |
| <P>The second is the Theater Spring bean which shows Spring -> 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. |
| * <p/> |
| * 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<Movie> nowPlaying = <SPAN class="code-keyword">new</SPAN> ArrayList<Movie>(); |
| |
| /** |
| * 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<<SPAN class="code-object">String</SPAN>> 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<Movie> 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 -> EJB -> 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 |
| * <p/> |
| * 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<Movie> 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<Movie> getMovies() { |
| <SPAN class="code-keyword">return</SPAN> movies; |
| } |
| |
| <SPAN class="code-keyword">public</SPAN> void setMovies(List<Movie> 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<Theater> 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<Movie> theaterOneMovies = theaterOne.getMovies(); |
| assertNotNull(theaterOneMovies); |
| |
| List<Movie> 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" class="external-link" 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) --> Ejb(deployment-id=CineplexImpl) |
| INFO - Jndi(name=MoviesLocal) --> 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> |
| </P> |
| </TD> |
| <TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"> |
| |
| |
| </TD> |
| </TR> |
| <TR class="Row5"> |
| <TD class="Col1"> </TD> |
| <TD class="Col2"> </TD> |
| <TD class="Col3"> |
| <BR> |
| <BR> |
| <IMG width="100%" height="1" src="http://openejb.apache.org/images/line_light.gif"> |
| <TABLE width="100%"> |
| <TR> |
| <TD> |
| <SPAN class="bodyGrey"> |
| <SMALL> |
| <NOTICE><!-- $FOOTER --> |
| Apache OpenEJB is an project of The Apache Software Foundation (ASF) |
| </NOTICE> |
| <BR> |
| Site Powered by |
| <A href="http://atlassian.com/">Atlassian</A> |
| <A href="http://atlassian.com/confluence/">Confluence</A> |
| . |
| </SMALL> |
| </SPAN> |
| </TD> |
| <TD align="right"> |
| <A style="color:#999;font-size:small;font-weight:normal;" href="https://cwiki.apache.org/confluence/pages/editpage.action?spaceKey=OPENEJBx30&title=Spring%20EJB%20and%20JPA">[ edit ]</A> |
| </TD> |
| </TR> |
| </TABLE> |
| <BR> |
| </TD> |
| <TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"> </TD> |
| </TR> |
| </TABLE> |
| |
| <!-- Needed for composition plugin --> |
| <!-- delay the loading of large javascript files to the end so that they don't interfere with the loading of page content --> |
| <SPAN style="display: none"> |
| <SCRIPT type="text/javascript" language="JavaScript" src="http://cwiki.apache.org/confluence/labels-javascript"></SCRIPT> |
| |
| <SCRIPT src="http://www.google-analytics.com/urchin.js" type="text/javascript"> |
| </SCRIPT> |
| <SCRIPT type="text/javascript"> |
| _uacct = "UA-2717626-1"; |
| urchinTracker(); |
| </SCRIPT> |
| </SPAN> |
| |
| </BODY> |
| </HTML> |