Search code examples
javajpapersistenceeclipselink

JPA EclipseLink and dynamic Jar-Files


I have an Application using JPA. In The application i have a class

@Entity
public class Root{
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private long id;
  ...
}

in my persistence.xml i specified

<persistence ...>
  <persistence-unit ...>
    <exclude-unlisted-classes>false</exlude-unlisted-classes>
  </persistence-unit>
</persistence>

Additional to that i have several Jar-Files which are loaded as Plugins at Runtime. These Plugins may have Classes extending the Root class, and which are also Entities.

Loading the files works fine, but on persisting i get an IllegalArgumentException with the message Subclass is not a known Entity type. I read about the CompositePersistenceUnits, which are not really a solution, because i have to specify all the jar-files beforehand, which is not really possible with a plugin-based system.

My Question is now: Can i dynamically add Jar-Files to a composite persistence unit? Or can i even add new Entities to a persistence unit which is not composite?


Solution

  • It is probably best to have all of your persistent classes in the same component, or have each component have its own independent persistence unit.

    You may wish to investigate EclipseLink external meta-data support that allows refreshing/extending the persistence unit at runtime,

    http://www.eclipse.org/eclipselink/documentation/2.5/solutions/metadatasource.htm

    You could also use the EclipseLink Session API to add descriptors or projects at runtime.