Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
EclipseLink/Examples/JPA/MetadataSource
EclipseLink JPA MetadataSource
Starting with EclipseLink 2.3.0 developers can now build solutions with EclipseLink JPA storing mapping information outside the application and can have it retrieved when the application's persistence unit is being created.
The Basics
In order to make use of this capability you must
- Configure the client persistence unit
- Configure the server
1. Client PU
The external storage of mappings can be as simple as making the eclipselink-orm.xml file with the additional mapping information available on a web server as a file but can be more complex involving a server process that stores the mapping information and supports dynamically updating this information.
If you simply want to access the mapping file at a fixed URL for your persistence unit you can use:
<property name="eclipselink.metadata-source" value="XML"/> <property name="eclipselink.metadata-source.xml.url" value="foo://bar"/>
If however you are building a more complex solution involving a calculated location based on application context you will need to implement the MetadataSource interface. This could be used to have tenant specific extensions in a Multitenant application. To configure this you will need to setup the properties as:
<property name="eclipselink.metadata-source" value="mypackage.MyMetadataSource"/> <property name="eclipselink.metadata-source.xml.url" value="foo://bar"/>
In the MySports example application a JAX-RS (RESTful) service is built that returns the mapping file per tenant. This is done like:
public class AdminMetadataSource extends XMLMetadataSource { @Override public XMLEntityMappings getEntityMappings(Map<String, Object> properties, ClassLoader classLoader, SessionLog log) { String leagueId = (String) properties.get(LEAGUE_CONTEXT); properties.put(PersistenceUnitProperties.METADATA_SOURCE_XML_URL, "http://foo.bar/rest/" + leagueId + "/orm"); return super.getEntityMappings(properties, classLoader, log); } }
Learn More
- eclipselink-orm.xml example: Using EclipseLInk extended mapping file.
- Extensible JPA Entities
- Multitenant Entities
- MySports Example: Integrated use of Multitenant extensible entities with a REST based admin server acting as the MetadataSource.