views:

73

answers:

2

I'm looking for an entity detection in OpenJPA. That I don't need to declare all entities in the persistence.xml.

Edit: I'm sorry, I forgot to say that I develop a java se app.

+1  A: 

Per the JPA specification, you must list all classes explicitly in a Java SE environment:

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

(...)

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.

Now, if you don't mind being not portable, you might use a provider extension but I couldn't find something similar to Hibernate's hibernate.archive.autodetection property in OpenJPA.

Using Spring's JPA integration (which allows more flexible classpath scanning) is another option.

Pascal Thivent
It's not applicable to Java SE persistence units. I'm sorry, I forgot to say that I develop a java se app.
then use spring as suggested in the last paragraph. works in se as well
seanizer
@seanizer The comment from the OP precedes my update and the update of the question (my initial suggestion was to set `exclude-unlisted-classes` to `false`).
Pascal Thivent
I figured as much, thanks
seanizer
+3  A: 

Read the Persistent Class List section in the user manual.

Unlike many ORM products, OpenJPA does not need to know about all of your persistent classes at startup. OpenJPA discovers new persistent classes automatically as they are loaded into the JVM; in fact you can introduce new persistent classes into running applications under OpenJPA.

  • Note: There are a couple restrictions that are documented in the user manual but I left those out to keep this post brief.
Rick