Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/Extensibility"

(Learn More)
 
Line 59: Line 59:
 
== Learn More ==
 
== Learn More ==
  
 +
* [[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Extensible_Entities | Extensible Entities (EclipseLink User Guide)]]
 
* [[EclipseLink/Examples/MOXy/Extensible | Extensible Beans with MOXy Example]]
 
* [[EclipseLink/Examples/MOXy/Extensible | Extensible Beans with MOXy Example]]

Latest revision as of 15:40, 15 December 2011

EclipseLink Extensible Entities

Starting in EclipseLink 2.3.0 developers now have the ability to define and use extensible entities where mappings can be added on the fly. This involves the entity storing extended attributes within a map instead of static attributes and then defining how values from this map are mapped to the database using an eclipselink-orm.xml mapping file. In addition to now being able to more dynamically define mappings EclipseLink has gone further to allow these extended mappings to be stored and managed externally. This external storage allows your extended mappings to be defined while the application is running.

Getting Started

The follow steps will help you configure your persistence unit so that it can have a set of mappings that are changeable after your application is deployed.

1. Design a standard JPA Application

2. Decide which Entities will allow flexible mappings, annotate them as such and provide facilities to store the addional data.

@Entity
@VirtualAccessMethods
public class Customer{
 
...
 
    @Transient
    private Map<String, Object> extensions;
 
    public <T> T get(String name) {
        return (T) extentions.get(name);
    }
 
    public Object set(String name, Object value) {
        return extensions.put(name, value);
    }


3. When you design your schema, provide enough extra columns in your tables to accomodate the number of flexible mappings you will allow. e.g. The following table has 3 predefined columns and 3 columns designed to accomodate mappings added after design (FLEX_COL1, FLEX_COL2, FLEX_COL3)

  • CUSTOMER
    • INTEGER ID
    • VARCHAR NAME
    • VARCHAR FLEX_COL1
    • VARCHAR FLEX_COL2
    • VARCHAR FLEX_CO31

4. Deploy your application

5. To provide additional mappings, provide an eclipselink-orm.xml file that contains the additional mappings.

<basic name="idNumber" attribute-type="String">
  <column name="FLEX_COL1"/>
</basic>

6. Use persistence unit properties to get your application to use the file:

<property name="eclipselink.metadata-source" value="XML"/>
<property name="eclipselink.metadata-source.xml.url" value="foo://bar"/>

Learn More