Multiple Persistence Units – Websphere v7, OpenJPA
Want to do something exotic like specifying two persistence units in your persistence.xml file? Perhaps one local and one JTA managed. Maybe even go whole hog and do that to write unit tests?
In Websphere v7 (RAD 7.5) when you have multiple PUs in persistence.xml it barfs like this:
1 2 3 4 5 |
[7/8/09 10:01:55:149 EDT] 00000053 servlet I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [someAppEAR] [/someAppJPAWeb] [/someAppUsersTable.jsp]: Initialization successful. [7/8/09 10:01:55:227 EDT] 00000053 JPAApplInfo E CWWJP0012E: The persistence unit name is not specified and a unique persistence unit is not found in the someAppEAR application and someAppJPAWeb.war module. [7/8/09 10:01:55:227 EDT] 00000053 JPAApplInfo E CWWJP0029E: The server cannot find the "" persistence unit in the someAppJPAWeb.war module and the someAppEAR application. [7/8/09 10:01:55:227 EDT] 00000053 InjectionBind E CWNEN0035E: The someAppJPAWeb reference of type javax.persistence.EntityManagerFactory for the <null> component in the someAppJPAWeb.war module of the someAppEAR application cannot be resolved. [7/8/09 10:01:55:242 EDT] 00000053 annotation E com.ibm.ws.webcontainer.annotation.WASAnnotationHelper inject exception while injecting resource |
Here is IBM’s “help”:
http://publib.boulder.ibm.com/infocenter/dmndhelp/v6r2mx/index.jsp?topic=com.ibm.websphere.wbpm.messages.620.doc/messages/com.ibm.ws.jpa.jpa.html
1 2 3 |
CWWJP0012E: The persistence unit name is not specified and a unique persistence unit is not found in the {0} application and {1} module. Problem The application has defined a persistence unit or persistence context reference without a persistence unit name and a unique persistence unit cannot be found. User response Change the application to specify a persistence unit name for the reference, or provide a unique persistence. |
Yeah, huh? What’s that mean? A reference in web.xml? and what name for it?
Can’t find the official answer anywhere. Let’s waste time searching and looking at logs and enjoying the wonderful WAS admin console.
OK, here is my answer.
Let’s say the JTA PU you want to use in your web app is named
1 2 |
<persistence-unit name="someAppJPAWeb" transaction-type="JTA"> etc. |
Then in web.xml create a PU reference:
1 2 3 4 5 6 7 |
<persistence-unit-ref> <description> Persistence unit for the someApp application. </description> <persistence-unit-ref-name>persistence/someAppJPAWeb</persistence-unit-ref-name> <persistence-unit-name>someAppJPAWeb</persistence-unit-name> </persistence-unit-ref> |
In your injected class use the reference name:
1 2 |
@PersistenceUnit(name="persistence/someAppJPAWeb") private EntityManagerFactory emf; |
If you have just one PU then you don’t need the name attribute nor the persistence-unit-ref.
Of course when you are looking at persistence.xml RAD will helpfully tell you that it doesn’t like multiple persistence units.