blob: 3bfe1ff4b8259a4b211f232d8393bb6d49452f87 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<!-- $PAGETITLE -->
<TITLE>OpenEJB - persistence-context ref</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">
<SCRIPT language="JavaScript" src="http://cwiki.apache.org/confluence/pages/viewpage.action?spaceKey=OPENEJB&title=functions.js" type="text/javascript"></SCRIPT>
<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">&nbsp;</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" rel="nofollow">Issues</A>
</TD>
<TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5">&nbsp;</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="../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>
</UL>
<H3><A name="Navigation-General"></A>General</H3>
<UL class="alternate" type="square">
<LI><A href="deploy-tool.html" title="Deploy Tool">Deployment</A></LI>
<LI><A href="startup.html" title="Startup">Startup</A></LI>
</UL>
<H3><A name="Navigation-Configuration"></A>Configuration</H3>
<UL class="alternate" type="square">
<LI><A href="../OPENEJB/configuration.html" title="Configuration">General</A></LI>
<LI><A href="deployments.html" title="Deployments">Deployments</A></LI>
<LI><A href="../OPENEJB/containers.html" title="Containers">Containers</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="../OPENEJB/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="../OPENEJB/annoyances.html" title="Annoyances">Annoyances</A></LI>
<LI><A href="irc://irc.freenode.net/#openejb" 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/" rel="nofollow">ActiveMQ</A></LI>
<LI><A href="http://openjpa.apache.org/" rel="nofollow">OpenJPA</A></LI>
<LI><A href="http://cxf.apache.org/" 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">&nbsp;</TD>
<TD class="Col3">
<TABLE id="PageHeader" border="0" width="100%">
<TR>
<TD>
<A href="http://openejb.apache.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 -->
persistence-context ref
</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">
<H1><A name="persistence-contextref-Viaannotation"></A>Via annotation</H1>
<P>Both lookup and injection of an EntityManager can be configured via the @PersistenceContext annotation.</P>
<STYLE type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</STYLE>
<!--[if IE]>
<style type="text/css">
.code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shCore.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></SCRIPT>
<SCRIPT class="javascript" src="http://cwiki.apache.org/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></SCRIPT>
<DIV class="code">
<TEXTAREA name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">package org.superbiz;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
@Stateless
@PersistenceContext(name = "myFooEntityManager", unitName = "foo-unit")
public class MyBean implements MyInterface {
@PersistenceContext(unitName = "bar-unit")
private EntityManager myBarEntityManager;
public void someBusinessMethod() throws Exception {
if (myBarEntityManager == null) throw new NullPointerException("myBarEntityManager not injected");
// Both can be looked up from JNDI as well
InitialContext context = new InitialContext();
EntityManager fooEntityManager = (EntityManager) context.lookup("java:comp/env/myFooEntityManager");
EntityManager barEntityManager = (EntityManager) context.lookup("java:comp/env/org.superbiz.MyBean/myBarEntityManager");
}
}</TEXTAREA>
<SCRIPT class="javascript">
if(!window.newcodemacro_initialised)
{
window.newcodemacro_initialised = true;
window.oldonloadmethod = window.onload;
window.onload = function(){
dp.SyntaxHighlighter.HighlightAll('newcodemacro');
if(window.oldonloadmethod)
{
window.oldonloadmethod();
}
}
}
</SCRIPT>
</DIV>
<H1><A name="persistence-contextref-Viaxml"></A>Via xml</H1>
<P>The above @PersistenceContext annotation usage is 100% equivalent to the following xml.</P>
<DIV class="code">
<TEXTAREA name="newcodemacro" class="xml:nocontrols:nogutter" rows="10" readonly="readonly">&lt;persistence-context-ref&gt;
&lt;persistence-context-ref-name&gt;myFooEntityManager&lt;/persistence-context-ref-name&gt;
&lt;persistence-unit-name&gt;foo-unit&lt;/persistence-unit-name&gt;
&lt;persistence-context-type&gt;Transaction&lt;/persistence-context-type&gt;
&lt;/persistence-context-ref&gt;
&lt;persistence-context-ref&gt;
&lt;persistence-context-ref-name&gt;org.superbiz.calculator.MyBean/myBarEntityManager&lt;/persistence-context-ref-name&gt;
&lt;persistence-unit-name&gt;bar-unit&lt;/persistence-unit-name&gt;
&lt;persistence-context-type&gt;Transaction&lt;/persistence-context-type&gt;
&lt;injection-target&gt;
&lt;injection-target-class&gt;org.superbiz.calculator.MyBean&lt;/injection-target-class&gt;
&lt;injection-target-name&gt;myBarEntityManager&lt;/injection-target-name&gt;
&lt;/injection-target&gt;
&lt;/persistence-context-ref&gt;</TEXTAREA>
<SCRIPT class="javascript">
if(!window.newcodemacro_initialised)
{
window.newcodemacro_initialised = true;
window.oldonloadmethod = window.onload;
window.onload = function(){
dp.SyntaxHighlighter.HighlightAll('newcodemacro');
if(window.oldonloadmethod)
{
window.oldonloadmethod();
}
}
}
</SCRIPT>
</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">&nbsp;</TD>
<TD class="Col2">&nbsp;</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) - <A href="http://openejb.apache.org/privacy-policy.html">Website Privacy Policy</A> -
</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="http://cwiki.apache.org/confluence/pages/editpage.action?spaceKey=OPENEJBx30&title=persistence-context%20ref">[ edit ]</A>
</TD>
</TR>
</TABLE>
<BR>
</TD>
<TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5">&nbsp;</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>
</SPAN>
<SCRIPT type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</SCRIPT>
<SCRIPT type="text/javascript">
var pageTracker = _gat._getTracker("UA-2717626-1");
pageTracker._initData();
pageTracker._trackPageview();
</SCRIPT>
</BODY>
</HTML>