Do I Need <Class> Elements in Persistence.Xml

Do I need class elements in persistence.xml?

The persistence.xml has a jar-file that you can use. From the Java EE 5 tutorial:

<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>
<jar-file>MyOrderApp.jar</jar-file>
<class>com.widgets.Order</class>
<class>com.widgets.Customer</class>
</persistence-unit>
</persistence>

This file defines a persistence unit
named OrderManagement, which uses a
JTA-aware data source jdbc/MyOrderDB. The jar-file and class elements specify managed persistence classes: entity classes, embeddable classes, and mapped superclasses. The jar-file element specifies JAR files that are visible to the packaged persistence unit that contain managed persistence classes, while the class element explicitly names managed persistence classes.

In the case of Hibernate, have a look at the Chapter2. Setup and configuration too for more details.

EDIT: Actually, If you don't mind not being spec compliant, Hibernate supports auto-detection even in Java SE. To do so, add the hibernate.archive.autodetection property:

<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<!-- This is required to be spec compliant, Hibernate however supports
auto-detection even in JSE.
<class>pl.michalmech.eventractor.domain.User</class>
<class>pl.michalmech.eventractor.domain.Address</class>
<class>pl.michalmech.eventractor.domain.City</class>
<class>pl.michalmech.eventractor.domain.Country</class>
-->

<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>

<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>

Is the order of class elements in persistence.xml important in any way?

The order of classes defined in the persistence.xml file doesn't matter.

Why do we need to specify class inside persistence-unit element?

If you didn't specify classes in the persistence.xml file your persistence manager will manage all entity classes in location where persistence.xml file exists (jar file, class directory).

Listing classes gives you flexibility to choose entities and group them in persistence unit. You can have finer control what consist a given persistent unit name - you can also include entites from other jars etc.

So, in most basic cases listing classes is unnecessary, and most of JPA impementations discover all entities for you.

define jpa entity classes outside of persistence.xml

In a Java SE environment, portable applications must list classes explicitly in the persistence.xml. From the JPA 1.0 specification:

6.2.1.6 mapping-file, jar-file, class, exclude-unlisted-classes


The following classes must be
implicitly or explicitly denoted as
managed persistence classes to be
included within a persistence unit:
entity classes; embeddable classes;
mapped superclasses.

The set of managed persistence classes
that are managed by a persistence unit
is defined by using one or more of the
following:

  • One or more object/relational mapping XML files
  • One or more jar files that will be searched for classes
  • An explicit list of the classes
  • The annotated managed persistence classes contained in the root of the
    persistence unit (unless the
    exclude-unlisted-classes element is
    specified)

(...)

A list of named managed persistence
classes may also be specified instead
of, or in addition to, the JAR files
and mapping files. Any mapping
metadata annotations found on these
classes will be processed, or they
will be mapped using the mapping
annotation defaults. The class element
is used to list a managed persistence
class. A list of all named managed
persistence classes must be specified
in Java SE environments to insure
portability. Portable Java SE
applications should not rely on the
other mechanisms described here to
specify the managed persistence
classes of a persistence unit.
Persistence providers may also require
that the set of entity classes and
classes that are to be managed must be
fully enumerated in each of the
persistence.xml files in Java SE
environments.

All classes contained in the root of
the persistence unit are also searched
for annotated managed persistence
classes and any mapping metadata
annotations found on them will be
processed, or they will be mapped
using the mapping annotation defaults.
If it is not intended that the
annotated persistence classes
contained in the root of the
persistence unit be included in the
persistence unit, the
exclude-unlisted-classes element
should be used. The
exclude-unlisted-classes element is
not intended for use in Java SE
environments.

The resulting set of entities managed
by the persistence unit is the union
of these sources, with the mapping
metadata annotations (or annotation
defaults) for any given class being
overridden by the XML mapping
information file if there are both
annotations as well as XML mappings
for that class. The minimum portable
level of overriding is at the level of
the persistent field or property.

If portability is not a concern, some provider do support entity discovery in a Java SE environment (for example, EclipseLink, Hibernate).

If portability is a concern, using a third party container like Spring would help.

Is there a way to scan JPA entities not to declare persistent classes in a persistence.xml file?

-Make sure your entities and persistence.xml end up in the same classpath when you build your jar.

The entities cannot be scanned if they are sitting in another classpath. If you need to have them sitting in different classpaths, heres one trick I've seen to make it work: No autodetection of JPA Entities in maven-verify.

If you are unsure where things are ending, you can unzip the .jar file and peak. This is an unpacked persistence web project:

Unpacked Web Persistence JAR

Notice my classes are down the com directory, and my persistence.xml is in the META-INF directory. Both are in the same 'classes' classpath, so autoscan will work.

-Set hibernate.archive.autodetection property.

<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm" />

-Add false to the persistence-unit

<persistence-unit name=...>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
...

Hopefully one of those will work for you.

Classes inside persistence.xml will be ignored

Found it! After upgrading eclipselink to V2.7.10 (store eclipselink.jar into .../wildfly-24.0.1.Final/modules/system/layers/base/org/eclipse/persistence/main/) I can deploy and start the application. Not sure which version of eclipselink was used before that (the jar file name don't contains the version information ...)

Switching to eclipselink 3.0.2 don't works because of this problem: Wildfly 21 with eclipselink 3.0 getting error as Persistence Provider not found



Related Topics



Leave a reply



Submit