« - »

Two EJB jars; One persistence.xml

16 October 2010

One of the main reasons that I wanted to figure out how to put the persistence.xml file outside of my EJB jar file, aside from my desire to externalize the datasource reference, was because I wanted to share the persistence.xml across multiple EJB jars. One of the EJB jars contained a collection of shared components designed to be used by multiple applications. The other contained application-specific components that were built solely for the use of the application itself.

The database supporting the application contained all of the tables for both the common components and the application-specific components, so one datasource, and one persistence.xml file could support both EJB jars. Getting the persistence.xml file out of the individual jars and into the .ear file made that possible. With the inclusion of both EBJ jar files, the project structure now looked like this:

.ear file
+- /lib directory
|  +- persistence.jar file
|     +- /META-INF directory
|        +- persistence.xml file
+- webapp.war file
+- shared-ejb.jar file
+- application-specific-ejb.jar file

All that was then necessary to make the lone persistence.xml support both EJB jars was to both name each jar specifically and then name all entity classes from both jars:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
		http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	<persistence-unit name="puName" transaction-type="JTA">
		<provider>oracle.toplink.essentials.PersistenceProvider</provider>
		<jta-data-source>jdbc/yourDatasourceName</jta>
		<jar-file>shared-ejb.jar</jar>
		<jar-file>application-specific-ejb.jar</jar>
		<class>shared.entity.bean.package.SomeClass</class>
		<class>shared.entity.bean.package.SomeOtherClass</class>
		<class>other.ejb.jar.package.SomeClass</class>
		<class>other.ejb.jar.package.AnotherClass</class>
		<properties>
			<property name="some.property" value="true"/>
			<property name="some.other.property" value="4"/>
		</properties>
	</persistence-unit>
</persistence>

Everything else remains pretty much the same as it did in my earlier example, and as far as I can tell (… so far, at least!), everything seems to work just fine.


http://blog.restafarian.org/2010/10/two-ejb-jars-one-persistence-xml/

Leave a reply

You must be logged in to post a comment.