Identify correct and incorrect statements or examples about persistence units, how persistence units are packaged, and the use of the persistence.xml file.

[Note]

A persistence unit is a logical grouping that includes:

Persistence Unit Packaging

Within Java EE environments, an EJB-JAR, WAR, EAR, or application client JAR can define a persistence unit. Any number of persistence units may be defined within these scopes.

A persistence unit may be packaged within one or more jar files contained within a WAR or EAR, as a set of classes within an EJB-JAR file or in the WAR classes directory, or as a combination of these as defined below.

A persistence unit is defined by a persistence.xml file. The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit. In Java EE, the root of a persistence unit may be one of the following:

It is not required that an EJB-JAR or WAR containing a persistence unit be packaged in an EAR unless the persistence unit contains persistence classes in addition to those contained in the EJB-JAR or WAR.

A persistence unit MUST have a name. Only one persistence unit of any given name may be defined within a single EJB-JAR file, within a single WAR file, within a single application client jar, or within an EAR (in the EAR root or lib directory).

The persistence.xml file may be used to designate MORE THAN ONE persistence unit within the same scope.

All persistence classes defined at the level of the Java EE EAR must be accessible to all other Java EE components in the application - i.e. loaded by the application classloader - such that if the same entity class is referenced by two different Java EE components (which may be using different persistence units), the referenced class is the same identical class.

In Java SE environments, the metadata mapping files, jar files, and classes can be used. To insure the portability of a Java SE application, it is necessary to explicitly list the managed persistence classes that are included in the persistence unit.

persistence.xml file

A persistence.xml file defines a persistence unit. It may be used to specify managed persistence classes included in the persistence unit, object/relational mapping information for those classes, and other configuration information for the persistence unit and for the entity manager(s) and entity manager factory for the persistence unit. The persistence.xml file is located in the META-INF directory of the root of the persistence unit. This information may be defined by containment or by reference, as described below.

The object/relational mapping information may take the form of annotations on the managed persistence classes included in the persistence unit, one or more XML files contained in the root of the persistence unit, one or more XML files outside the root of the persistence unit on the classpath and referenced from the persistence.xml, or a combination of these.

The managed persistence classes may either be contained within the root of the persistence unit; or they may be specified by reference - i.e., by naming the classes, class archives, or mapping XML files (which in turn reference classes) that are accessible on the application classpath; or they may be specified by some combination of these means.

The persistence element consists of one or more persistence-unit elements.

The persistence-unit element consists of the name and transaction-type attributes and the following sub-elements:

The name attribute is REQUIRED; the other attributes and elements are OPTIONAL.

Examples:


<persistence>
	<persistence-unit name="OrderManagement">
		<description>
			This unit manages orders and customers.
			It does not rely on any vendor-specific features and can
			therefore be deployed to any persistence provider.
		</description>
		<jta-data-source>jdbc/MyOrderDB</jta-data-source>
		<mapping-file>ormap.xml</mapping-file>
		<jar-file>MyOrderApp.jar</jar-file>
		<class>com.widgets.Order</class>
		<class>com.widgets.Customer</class>
	</persistence-unit>
</persistence>

					


<persistence>
	<persistence-unit name="OrderManagement2">
		<description>
			This unit manages inventory for auto parts.
			It depends on features provided by the
			com.acme.persistence implementation.
		</description>
		<provider>com.acme.AcmePersistence</provider>
		<jta-data-source>jdbc/MyPartDB</jta-data-source>
		<mapping-file>ormap2.xml</mapping-file>
		<jar-file>MyPartsApp.jar</jar-file>
		<properties>
			<property name="com.acme.persistence.sql-logging" value="on"/>
		</properties>
	</persistence-unit>
</persistence>

					

The following are sample contents of a persistence.xml file.

  1. 
    <persistence-unit name="OrderManagement"/>
    								
    								

    A persistence unit named OrderManagement is created. Any annotated managed persistence classes found in the root of the persistence unit are added to the list of managed persistence classes. If a META-INF/orm.xml file exists, any classes referenced by it and mapping information contained in it are used as specified above. Because no provider is specified, the persistence unit is assumed to be portable across providers. Because the transaction type is not specified, JTA is assumed. The container must provide the data source (it may be specified at application deployment, for example); in Java SE environments, the data source may be specified by others means.

  2. 
    <persistence-unit name="OrderManagement2">
    	<mapping-file>mappings.xml</mapping-file>
    </persistence-unit>
    								
    								

    A persistence unit named OrderManagement2 is created. Any annotated managed persistence classes found in the root of the persistence unit are added to the list of managed persistence classes. The mappings.xml resource exists on the classpath and any classes and mapping information contained in it are used as specified above. If a META-INF/orm.xml file exists, any classes and mapping information contained in it are used as well. The transaction type, data source, and provider are as described above.

  3. 
    <persistence-unit name="OrderManagement3">
    	<jar-file>order.jar</jar-file>
    	<jar-file>order-supplemental.jar</jar-file>
    </persistence-unit>
    
    								

    A persistence unit named OrderManagement3 is created. Any annotated managed persistence classes found in the root of the persistence unit are added to the list of managed persistence classes. If a META-INF/orm.xml file exists, any classes and mapping information contained in it are used as specified above. The order.jar and order-supplemental.jar files are searched for managed persistence classes and any annotated managed persistence classes found in them and/or any classes specified in the orm.xml files of these jar files are added. The transaction-type, data source and provider are as described above.

  4. 
    <persistence-unit name="OrderManagement4" transaction-type=RESOURCE_LOCAL>
    	<non-jta-data-source>jdbc/MyDB</jta-data-source>
    	<mapping-file>order-mappings.xml</mapping-file>
    	<exclude-unlisted-classes/>
    	<class>com.acme.Order</class>
    	<class>com.acme.Customer</class>
    	<class>com.acme.Item</class>
    </persistence-unit>
    
    								

    A persistence unit named OrderManagement4 is created. The order-mappings.xml is read as a resource and any classes referenced by it and mapping information contained in it are used. The annotated Order, Customer and Item classes are loaded and are added. No (other) classes contained in the root of the persistence unit are added to the list of managed persistence classes. The persistence unit is portable across providers. A entity manager factory supplying resource-local entity managers will be created. The data source jdbc/MyDB must be used.

  5. 
    <persistence-unit name="OrderManagement5">
    	<provider>com.acme.AcmePersistence</provider>
    	<mapping-file>order1.xml</mapping-file>
    	<mapping-file>order2.xml</mapping-file>
    	<jar-file>order.jar</jar-file>
    	<jar-file>order-supplemental.jar</jar-file>
    </persistence-unit>
    								
    								

    A persistence unit named OrderManagement5 is created. Any annotated managed persistence classes found in the root of the persistence unit are added to the list of managed classes. The order1.xml and order2.xml files are read as resources and any classes referenced by them and mapping information contained in them are also used as specified above. The order.jar is a jar file on the classpath containing another persistence unit, while order-supplemental.jar is just a library of classes. Both of these jar files are searched for annotated managed persistence classes and any annotated managed persistence classes found in them and/or any classes specified in the orm.xml files (if any) of these jar files are added. The provider com.acme.AcmePersistence must be used.

    Note that the persistence.xml file contained in order.jar is not used to augment the persistence unit OrderManagement5 with the classes of the persistence unit whose root is order.jar.

Persistence Unit Scope

An EJB-JAR, WAR, application client jar, or EAR can define a persistence unit.

When referencing a persistence unit using the unitName annotation element or persistence-unit-name deployment descriptor element, the visibility scope of the persistence unit is determined by its point of definition. 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.

However, if a persistence unit of the same name is defined by an EJB-JAR, WAR, or application jar file within the EAR, the persistence unit of that name defined at EAR level will not be visible to the components defined by that EJB-JAR, WAR, or application jar file unless the persistence unit reference uses the persistence unit name # syntax to specify a path name to disambiguate the reference. When the # syntax is used, the path name is relative to the referencing application component jar file. For example, the syntax ../lib/persistenceUnitRoot.jar#myPersistenceUnit refers to a persistence unit whose name, as specified in the name element of the persistence.xml file, is myPersistenceUnit and for which the relative path name of the root of the persistence unit is ../lib/persistenceUnitRoot.jar. The # syntax may be used with both the unitName annotation element or persistence-unit-name deployment descriptor element to reference a persistence unit defined at EAR level.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine