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.