| |
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <HTML> |
| <HEAD> |
| <!-- $PAGETITLE --> |
| <TITLE>OpenEJB - Configuring PersistenceUnits in Tests</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 --> |
| Configuring PersistenceUnits in Tests |
| </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="ConfiguringPersistenceUnitsinTests-Overridingthepersistence.xml"></A>Overriding the persistence.xml</H1> |
| |
| <P>The most common situation in EJB related testing by far is the need to alter your persistence.xml for a test environment.</P> |
| |
| <H2><A name="ConfiguringPersistenceUnitsinTests-Overridingthe%3Cjtadatasource%3Eand%3Cnonjtadatasource%3E"></A>Overriding the <jta-data-source> and <non-jta-data-source></H2> |
| |
| <P>OpenEJB will automatically use the DataSources you have setup in your test environment, we're pretty good at guessing the right DataSources you intend even if the names don't match exactly – or in some cases at all. If there is only one DataSource configured, it's very easy for us to guess the DataSource to use.</P> |
| |
| <P>This allows you to keep your persistence.xml configured for your production environment and helps eliminate the need for a "test" persistence.xml (though we do have that functionality). A log line will be printed saying if we had to adjust the DataSources of your persistence.xml.</P> |
| |
| <H2><A name="ConfiguringPersistenceUnitsinTests-Overridingthepersistenceunit%3Cproperties%3E"></A>Overriding the persistence-unit <properties></H2> |
| |
| <P>You can override any property in your test setup via either system properties or the initial context properties. The format is:</P> |
| |
| <BLOCKQUOTE><P><unit-name>.<property>=<value></P></BLOCKQUOTE> |
| |
| <P>So for example with the following persistence.xml:</P> |
| |
| <DIV class="code panel" style="border-width: 1px;"><DIV class="codeHeader panelHeader" style="border-bottom-width: 1px;"><B>persistence.xml</B></DIV><DIV class="codeContent panelContent"> |
| <PRE class="code-xml"> |
| <SPAN class="code-tag"><persistence></SPAN> |
| <SPAN class="code-tag"><persistence-unit name=<SPAN class="code-quote">"movie-unit"</SPAN>></SPAN> |
| <SPAN class="code-tag"><provider></SPAN>org.hibernate.ejb.HibernatePersistence<SPAN class="code-tag"></provider></SPAN> |
| <SPAN class="code-tag"><jta-data-source></SPAN>movieDatabase<SPAN class="code-tag"></jta-data-source></SPAN> |
| <SPAN class="code-tag"><non-jta-data-source></SPAN>movieDatabaseUnmanaged<SPAN class="code-tag"></non-jta-data-source></SPAN> |
| <SPAN class="code-tag"><properties></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"hibernate.hbm2ddl.auto"</SPAN> value=<SPAN class="code-quote">"create-drop"</SPAN>/></SPAN> |
| <SPAN class="code-tag"><property name=<SPAN class="code-quote">"hibernate.max_fetch_depth"</SPAN> value=<SPAN class="code-quote">"3"</SPAN>/></SPAN> |
| <SPAN class="code-tag"></properties></SPAN> |
| <SPAN class="code-tag"></persistence-unit></SPAN> |
| <SPAN class="code-tag"></persistence></SPAN> |
| </PRE> |
| </DIV></DIV> |
| |
| <P>You can override and add persistence unit properties in your test case. There are currently no facilities for removing them (if you have a need for that let us know – it hasn't really come up so far).</P> |
| |
| <DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> |
| <PRE class="code-java"> |
| Properties p = <SPAN class="code-keyword">new</SPAN> Properties(); |
| p.put(Context.INITIAL_CONTEXT_FACTORY,<SPAN class="code-quote">"org.apache.openejb.client.LocalInitialContextFactory"</SPAN>); |
| |
| p.put(<SPAN class="code-quote">"movie-unit.hibernate.hbm2ddl.auto"</SPAN>, <SPAN class="code-quote">"update"</SPAN>); |
| p.put(<SPAN class="code-quote">"movie-unit.hibernate.dialect"</SPAN>, <SPAN class="code-quote">"org.hibernate.dialect.HSQLDialect"</SPAN>); |
| |
| context = <SPAN class="code-keyword">new</SPAN> InitialContext(p); |
| </PRE> |
| </DIV></DIV> |
| |
| <P>The overriding order is as follows: 1 = highest, 4 = lowest.</P> |
| |
| <OL> |
| <LI>InitialContext properties</LI> |
| <LI>jndi.properties from the classpath</LI> |
| <LI>System properties</LI> |
| <LI>persistence.xml properties</LI> |
| </OL> |
| |
| |
| <P>By default there are no overrides in 1-3, so #4 is the only source of information. </P> |
| |
| <P>In the above example there would be exactly three properties for the "movie-unit" persistence unit:</P> |
| <UL class="alternate" type="square"> |
| <LI>hibernate.hbm2ddl.auto = update</LI> |
| <LI>hibernate.max_fetch_depth = 3</LI> |
| <LI>hibernate.dialect = org.hibernate.dialect.HSQLDialect</LI> |
| </UL> |
| |
| |
| <P>These properties would be passed by OpenEJB directly to the persistence provider (in this case Hibernate). With one exception OpenEJB does not understand or modify these properties. Details on that one exception below.</P> |
| |
| <H3><A name="ConfiguringPersistenceUnitsinTests-Noneedtospecifya%22transactionlookup%22property"></A>No need to specify a "transaction lookup" property</H3> |
| |
| <P>All vendors have such a property for getting a reference to the container's TransactionManager and nothing works if this is not set correctly to the OpenEJB specific class. To make the lives of users easier, OpenEJB will take the liberty of setting it for you.</P> |
| |
| <P>Here are the persistence provider classes we understand and the defaults we will set for you:</P> |
| |
| <H4><A name="ConfiguringPersistenceUnitsinTests-Providerorg.hibernate.ejb.HibernatePersistence"></A>Provider org.hibernate.ejb.HibernatePersistence</H4> |
| |
| <P>When using this provider, the <B>hibernate.transaction.manager_lookup_class</B> will be automatically set by OpenEJB to <EM>org.apache.openejb.hibernate.TransactionManagerLookup</EM>. If the property is already set in the persistence unit it will be overwritten if it starts with the standard "org.hibernate.transaction." prefix. </P> |
| |
| <P>Custom lookup implementations will never be overwritten.</P> |
| |
| <H4><A name="ConfiguringPersistenceUnitsinTests-Provideroracle.toplink.essentials.PersistenceProvider"></A>Provider oracle.toplink.essentials.PersistenceProvider</H4> |
| |
| <P>Or <EM>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</EM>.</P> |
| |
| <P>When using this provider, the <B>toplink.target-server</B> will be automatically set by OpenEJB to <EM>org.apache.openejb.toplink.JTATransactionController</EM>. If the property is already set in the persistence unit it will be overwritten if it starts with the standard "oracle.toplink.transaction." prefix. </P> |
| |
| <P>Custom transaction controller implementations will never be overwritten.</P> |
| |
| <H4><A name="ConfiguringPersistenceUnitsinTests-Providerorg.eclipse.persistence.jpa.PersistenceProvider"></A>Provider org.eclipse.persistence.jpa.PersistenceProvider</H4> |
| |
| <P>Or <EM>org.eclipse.persistence.jpa.osgi.PersistenceProvider</EM>.</P> |
| |
| <P>When using this provider, the <B>eclipselink.target-server</B> will be automatically set by OpenEJB to <EM>org.apache.openejb.eclipselink.JTATransactionController</EM>. If the property is already set in the persistence unit it will be overwritten if it starts with the standard "org.eclipse.persistence.transaction." prefix. </P> |
| |
| <P>Custom transaction controller implementations will never be overwritten.</P> |
| |
| <H4><A name="ConfiguringPersistenceUnitsinTests-Providerorg.apache.openjpa.persistence.PersistenceProviderImpl"></A>Provider org.apache.openjpa.persistence.PersistenceProviderImpl</H4> |
| |
| <P>OpenJPA is capable of discovering the correct method for locating the TransactionManager without the need for users to specify the specific strategy. Therefore no specific "magic" is required.</P> |
| |
| </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=Configuring%20PersistenceUnits%20in%20Tests">[ 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> |