« - »

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"
	<persistence-unit name="puName" transaction-type="JTA">
			<property name="some.property" value="true"/>
			<property name="some.other.property" value="4"/>

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.


Leave a reply

You must be logged in to post a comment.