tags:

views:

75

answers:

2

Is there a way to define jpa entity classes outside of persistence.xml (i.e. in a separate file)?

Being able to not have persistence.xml as an external file would also suffice.

Thanks in advance, Steven

Edit: Sorry I was not clear. This is in a Java SE environment. Also, I would like to not have a listing of some.class.AClass in my persistence.xml file. This is because I would like to create this list of classes dynamically and reference a file containing this list.

Edit2: Managed to solve this by writing a persistence.xml file at build time before it is packaged. If anyone is interested, I used scannotations to discover all the classes annotated with @Entity and wrote them to the persistence.xml file in target/classes/META-INF (using maven).

+3  A: 

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.

Pascal Thivent
@Pascal Thivent Nice(+1)
Arthur Ronald F D Garcia
A: 

If you are willing and able to package your entity classes in a JAR file along with the persistence.xml file, then you don't need to list each entity in a <class> element. When a JAR is deployed with a persistence.xml file in the META-INF directory the JAR will be searched at runtime for any classes that have the @Entity annotation.

Jim Tough
Only in a Java EE environment. But the question is really not clear.
Pascal Thivent