File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JPA Certification (OCEJPA) and the fly likes Persistent Unit at EAR level Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » JPA Certification (OCEJPA)
Bookmark "Persistent Unit at EAR level" Watch "Persistent Unit at EAR level" New topic

Persistent Unit at EAR level

Ann Basso
Ranch Hand

Joined: Jul 26, 2007
Posts: 48

This is regarding the statement in JPA specification,
A persistence unit must have a name. Only one persistence unit of any given name must be defined
within a single EJB-JAR file, within a single WAR file, within a single application client jar, or within an EAR. See Section 8.2.2, “Persistence Unit Scope”.
A persistence unit that is defined at the level of an EJB-JAR, WAR, or application client jar is scoped to that EJB-JAR, WAR, or application jar respectively and is visible to the components defined in that jar or war.
A persistence unit that is defined at the level of the EAR is generally visible to all components in the application.

I am trying to understand how do you define a persistent unit at the ear level as opposed to a PU at application jar level. As I understand, an ear can contain jars and wars. So a PU defined in a jar file that is contained in an ear (e.g. /lib/somejar.jar ), is considered as defined as application jar level or ear level?

I think am confused between application jar, ejb-jar and a regular jar. Is there a difference between these? A jar file containing ejbs is ejb-jar, isn't it? so if a PU is defined in an ejb-jar, would it be considered as "ear" level? What would be a regular jar?

Sorry, I am totally confused about this and would appreciate any help !

Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3634
1. If you have jar at the root level containing META-INF/ejb-jar.xml, then that's an ejb-jar. ejb-jar.xml is now optional (because of annotations). So pretty much any jar sitting at the root of the ear is an ejb-jar, unless it is an application-client jar.
2. If you have jar at the root level containing META-INF/application-client.xml, then that's an application client jar.
3. If you have war at the root level, that's your webapp jar.

PUs in above jars are not accessible to code in other jars.

4. If you have a jar file in the "library directory" of your ear [which, by default, is /lib], it is a shared jar and is accessible to all the other jars. This is your EAR level jar and thus EAR level PU (if it contains a PU), which can be accessed by code in any other jar. It can be accessed using the relative path from the jar of the accessing component to the shared jar. For example, if you have mywebapp.war that is trying to access a PU named allentitiesPU, contained in /lib/allentities.jar, it can do, ../lib/allentities.jar#allentitiesPU


Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
I agree. Here's the link:
subject: Persistent Unit at EAR level
It's not a secret anymore!