Jump to: navigation, search

EclipseLink/Examples/JPA/MetadataSource

< EclipseLink‎ | Examples‎ | JPA
Revision as of 06:55, 13 June 2011 by Douglas.clarke.oracle.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

  1. Configure the client persistence unit
  2. 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