Java Reference
In-Depth Information
The persistence.xml file must go under /classes/META-INF/ . The JPA entities are
copied as *.class files in the subdirectory of their package as they usually are for a
WAR. Now that you know the structure of a persistence module, we'll teach you a little
more about persistence.xml.
Persistence unit scoping
You can define a persistence unit in a WAR, EJB-JAR, or JAR at the EAR level. If you
define a persistence unit in a module, it's visible only to that specific module. But if you
define the unit by placing a JAR file in the root or lib directory of the EAR, the persistence
unit will automatically be visible to all modules in the EAR. For this to work, you must re-
member the restriction that if the same name is used by a persistence unit in the EAR level
and at the module level, the persistence unit in the module level will win.
Assume you have an EAR file structure like this:
lib/actionBazaar-common.jar
actionBazaar-ejb.jar
actionBazaar-web.war
The actionBazaar-common.jar has a persistence unit with the name actionBazaar and
actionBazaar-ejb.jar also has a persistence unit with the name actionBazaar .
The actionBazaar persistence unit is automatically visible to the web module, and you
can use it as follows:
@PersistenceUnit(unitName = "actionBazaar")
private EntityManagerFactory emf;
But if you use this code in the EJB module, the local persistence unit will be accessed be-
cause the local persistence unit has precedence. If you want to access the persistence unit
defined at the EAR level, you have to reference it with the specific name as follows:
PersistenceUnit(unitName ="lib/actionBazaar-common.jar#actionBazaar")
private EntityManagerFactory emf;
Search WWH ::




Custom Search