Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

There is also the JPA-plugin that can do this for you. The latest version is not available in Maven Central so you will have to clone and build yourself.

JPA-Plugin

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top