In my project I need to be able to create a dynamic entity which extends a regular entity. The dynamic/virtual entity definition is loaded at startup from a configuration database (not using eclipselink). For this I've created a XMLMetadataSource which provides eclipselink with the dynamic entity definition, however startup fails b/c eclipselink is using the real classloader instead of the dynamic classloader. I'm using EclipseLink 2.4.0.
Code from Metadatasource:
EntityAccessor accessor = new EntityAccessor();
accessor.setName("SubProject");
//dynamic entity
accessor.setClassName("com.foo.SubProject");
//regular entity, see com/foo/Project.java
accessor.setParentClassName("com.foo.Project");
accessor.setAccess("VIRTUAL");
accessor.setDiscriminatorValue("SP");
List<SecondaryTableMetadata> secondaryTables = new ArrayList<SecondaryTableMetadata>();
SecondaryTableMetadata table = new SecondaryTableMetadata();
table.setName("pln_sub_project");
secondaryTables.add(table);
accessor.setSecondaryTables(secondaryTables);
//based on Metadatasource example
addExtension(accessor, new Extension("com.foo.SubProject", "java.lang.String", "foo"));
mappings.getEntities().add(accessor);
The problem seems to be here in EntityManagerSetupImpl#deploy(Classloader, Map)
processor.setClassLoader(realClassLoader);
processor.createDynamicClasses();
The processor ends up using the realClassLoader instead of the dynamic class loader supplied in PersistenceUnit properties resulting in the following error:
Caused by: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [p] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-7328] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: When using VIRTUAL access a DynamicClassLoader must be provided when creating the entity manager factory using the eclipselink property [eclipselink.classloader]. That is, createEntityManagerFactory(String persistenceUnitName, Map properties) and add a new DynamicClassLoader() to the Map properties.
at org.eclipse.persistence.exceptions.EntityManagerSetupException.deployFailed(EntityManagerSetupException.java:229)
... 62 more
Caused by: Exception [EclipseLink-7328] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: When using VIRTUAL access a DynamicClassLoader must be provided when creating the entity manager factory using the eclipselink property [eclipselink.classloader]. That is, createEntityManagerFactory(String persistenceUnitName, Map properties) and add a new DynamicClassLoader() to the Map properties.
at org.eclipse.persistence.exceptions.ValidationException.invalidClassLoaderForDynamicPersistence(ValidationException.java:1070)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.createDynamicClasses(MetadataProject.java:861)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.createDynamicClasses(MetadataProcessor.java:153)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:463)
... 60 more
[EL Severe]: ejb: 2012-11-06 17:17:42.19--ServerSession(29272897)--Thread(Thread[http-bio-8080-exec-3,5,main])--Local Exception Stack:
Exception [EclipseLink-7328] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: When using VIRTUAL access a DynamicClassLoader must be provided when creating the entity manager factory using the eclipselink property [eclipselink.classloader]. That is, createEntityManagerFactory(String persistenceUnitName, Map properties) and add a new DynamicClassLoader() to the Map properties.
at org.eclipse.persistence.exceptions.ValidationException.invalidClassLoaderForDynamicPersistence(ValidationException.java:1070)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.createDynamicClasses(MetadataProject.java:861)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.createDynamicClasses(MetadataProcessor.java:153)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:463)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:186)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getMetamodel(EntityManagerFactoryDelegate.java:602)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:516)