开发者

define jpa entity classes outside of persistence.xml

开发者 https://www.devze.com 2023-01-17 18:18 出处:网络
Is there a way to define jpa entity classes outside开发者_如何学Python of persistence.xml (i.e. in a separate file)?

Is there a way to define jpa entity classes outside开发者_如何学Python 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).


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消