Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/sandbox/gelernter/Extensible Domain Models"

m
m
Line 11: Line 11:
 
*[[EclipseLink/Examples/JPA/Extensibility|Extensible Entities]]
 
*[[EclipseLink/Examples/JPA/Extensibility|Extensible Entities]]
 
}}
 
}}
 
+
===SANDOBOX VERSION===
===*****SANDBOX VERSION*****===
+
 
=Extensible Entities=
 
=Extensible Entities=
 
 
''This topic is currently under review.''
 
''This topic is currently under review.''
  
 
{{EclipseLink_NewIn
 
{{EclipseLink_NewIn
 
|version=2.3}}
 
|version=2.3}}
<br>
+
<br><br>
 
+
<br>
+
  
 
Use the <tt>@VirtualAccessMethods</tt> annotation to specify that an entity is extensible. By using virtual properties in an extensible entity, you can specify mappings external to the entity. This allows you to modify the mappings without modifying the entity source file and without redeploying the entity's persistence unit.  
 
Use the <tt>@VirtualAccessMethods</tt> annotation to specify that an entity is extensible. By using virtual properties in an extensible entity, you can specify mappings external to the entity. This allows you to modify the mappings without modifying the entity source file and without redeploying the entity's persistence unit.  
Line 34: Line 30:
 
* Provide an additional source of metadata to be used by an application.  
 
* Provide an additional source of metadata to be used by an application.  
  
To create an extensible entity, you must do the following:
+
To create and support an extensible entity,  
  
 
#Configure the entity. See [[#Configuring the Entity| Configuring the Entity]].  
 
#Configure the entity. See [[#Configuring the Entity| Configuring the Entity]].  
Line 42: Line 38:
  
 
===Configuring the Entity===
 
===Configuring the Entity===
 +
To configure the entity,
 +
 +
# [[#Annotate with @VirtualAccessMethods| Annotate with <tt>@VirtualAccessMethods</tt>]]
 +
#  [[#Add get() and set() Methods| Add <tt>get()</tt> and <tt>set</tt> methods]]
 +
# [[#Add a Map|Add a Map]]
 +
 +
====Annotate with @VirtualAccessMethods====
  
 
Annotate the entity with <tt>@VirtualAccessMethods</tt> to specify that it is extensible and to define virtual properties.  
 
Annotate the entity with <tt>@VirtualAccessMethods</tt> to specify that it is extensible and to define virtual properties.  
Line 62: Line 65:
 
<br>
 
<br>
 
<br>
 
<br>
====Add get and set Methods====
+
==== Add get() and set() Methods====
 
Add the <tt>get(String)</tt> and <tt>set(String, Object)</tt> methods to the entity.
 
Add the <tt>get(String)</tt> and <tt>set(String, Object)</tt> methods to the entity.
  
Line 73: Line 76:
 
}}
 
}}
  
====Add a Map====
+
==== Add a Map ====
  
 
Add a <tt>Map</tt> as the data structure to store extended attributes and values. These are then mapped to the database in the <tt>eclipselink-orm.xml</tt> mapping file. See [[Providing Additional Mappings]].
 
Add a <tt>Map</tt> as the data structure to store extended attributes and values. These are then mapped to the database in the <tt>eclipselink-orm.xml</tt> mapping file. See [[Providing Additional Mappings]].
  
When using field-based access to the entity, you should annotate the <tt>Map</tt> with <tt>@Transient</tt> so it cannot use it for another mapping. When using property-based access, <tt>@Transient'</tt> is unnecessary.
+
When using field-based access to the entity, annotate the <tt>Map</tt> with <tt>@Transient</tt> so it cannot use it for another mapping. When using property-based access, <tt>@Transient'</tt> is unnecessary.
 
====Example====
 
====Example====
  
Line 157: Line 160:
 
*Field access is used for non-extension fields.
 
*Field access is used for non-extension fields.
 
*Virtual access is used for extension fields, using defaults <tt>(get(String)</tt> and <tt>set(String, Object))</tt> .
 
*Virtual access is used for extension fields, using defaults <tt>(get(String)</tt> and <tt>set(String, Object))</tt> .
*The <tt>get(String)</tt> and <tt>set(String, Object)</tt> methods will be woven, even if no mappings use them, because of the presence of <tt>@Extensible</tt>.<br>
+
*The <tt>get(String)</tt> and <tt>set(String, Object)</tt> methods will be woven, even if no mappings use them, because of the presence of <tt>@VirtualAccessMethods</tt>.
'''''// REVIEWERS: All the examples make this point about @Extensible, but I don't see @Extensible used elsewhere in this spec, in the Javadoc, or in the [[EclipseLink/Examples/JPA/Extensibility|sample]]; and I don't see <extensible> in eclipselink-orm.xsd. Does it exist? If it does not exist, should this bullet be rewritten to say something new about weaving, or should I delete it?//'''''
+
 
+
* '''Replace "Extensible" with "VirtualAccessMethods".  I'll fix the other doc.'''--[[User:Tom.ware.oracle.com|Tom.ware.oracle.com]] 17:22, 28 June 2011 (UTC)
+
 
+
 
*Extensions are mapped in a portable way by specifying <tt>@Transient</tt>.  
 
*Extensions are mapped in a portable way by specifying <tt>@Transient</tt>.  
 
* '''Let me know if that is not covered in the original comments'''--[[User:Tom.ware.oracle.com|Tom.ware.oracle.com]] 17:22, 28 June 2011 (UTC)
 
 
  
 
Example 1
 
Example 1
Line 201: Line 197:
 
*Field access is used for non-extension fields.
 
*Field access is used for non-extension fields.
 
* The <tt>@VirtualAccessMethods</tt> annotation overrides methods to be used for getting and setting.  
 
* The <tt>@VirtualAccessMethods</tt> annotation overrides methods to be used for getting and setting.  
* The <tt>getExtension(String)</tt> and <tt>setExtension(String, Object)</tt> methods will be woven, even if no mappings use them, because of the presence of <tt>@Extensible</tt>  
+
*The <tt>get(String)</tt> and <tt>set(String, Object)</tt> methods will be woven, even if no mappings use them, because of the presence of <tt>@VirtualAccessMethods</tt>.<br>
'''''// REVIEWERS: See my question about @Extensible in example 1.//'''''
+
 
*Extensions are mapped in a portable way by specifying <tt>@Transient</tt>.  
 
*Extensions are mapped in a portable way by specifying <tt>@Transient</tt>.  
'''''// REVIEWERS: See my question about @Transient in example 1.//'''''
 
 
* The XML for extended mapping indicates which <tt>get()</tt> and <tt>set()</tt> method to use.
 
* The XML for extended mapping indicates which <tt>get()</tt> and <tt>set()</tt> method to use.
 
  
 
Example 2  
 
Example 2  
Line 248: Line 241:
 
Example 3  illustrates the following:  
 
Example 3  illustrates the following:  
  
* Property access is used for non extension fields.
+
* Property access is used for non-extension fields.
 
*Virtual access is used for extension fields, using defaults <tt>(get(String)</tt> and <tt>set(String, Object)) </tt>  
 
*Virtual access is used for extension fields, using defaults <tt>(get(String)</tt> and <tt>set(String, Object)) </tt>  
* The extensions are mapped in a portable way; no @Transient is required because property access is used.  
+
* The extensions are mapped in a portable way. No <tt>@Transient</tt> is required, because property access is used.  
'''''//REVIEWERS: Why? See also my comments on @Transient in example 1//'''''
+
*The <tt>get(String)</tt> and <tt>set(String, Object)</tt> methods will be woven, even if no mappings use them, because of the presence of <tt>@VirtualAccessMethods</tt>.<br>
* The <tt>get(String)</tt> and <tt>set(String, Object) </tt> methods will be woven, even if no mappings use them, because of the presence of <tt>@Extensible</tt>.
+
'''''//REVIEWERS: See also comments about <tt>@Extensible</tt> in example 1//'''''
+
  
 
<source lang="java">
 
<source lang="java">

Revision as of 12:28, 29 June 2011

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

SANDOBOX VERSION

Extensible Entities

This topic is currently under review.

EL NewIn.png New in version 2.3.



Use the @VirtualAccessMethods annotation to specify that an entity is extensible. By using virtual properties in an extensible entity, you can specify mappings external to the entity. This allows you to modify the mappings without modifying the entity source file and without redeploying the entity's persistence unit.

Extensible entities are useful in a multi-tenant (or Software-as-a-Service) environment where a shared, generic application can be used by multiple clients (tenants). Tenants have private access to their own data, as well as to data shared with other tenants. See also Single-Table Multi-Tenancy.

Using extensible entities, you can:

  • Build an application where some mappings are common to all users and some mappings are user-specific.
  • Add mappings to an application after it is made available to a customer (even post-deployment).
  • Use the same EntityManagerFactory to work with data after mappings have changed.
  • Provide an additional source of metadata to be used by an application.

To create and support an extensible entity,

  1. Configure the entity. See Configuring the Entity.
  2. Include flexible columns in the database table to store the additional data. See Designing the Schema.
  3. Specify extended mappings in the eclipselink-orm.xml file. See Providing Additional Mappings
  4. Configure persistence.xml. See Configuring persistence.xml.

Configuring the Entity

To configure the entity,

  1. Annotate with @VirtualAccessMethods
  2. Add get() and set methods
  3. Add a Map

Annotate with @VirtualAccessMethods

Annotate the entity with @VirtualAccessMethods to specify that it is extensible and to define virtual properties.

@VirtualAccessMethods Attributes
Attribute Description Default Required?
get The name of the getter method to use for the virtual property This method must take a single java.lang.String parameter and return a java.lang.Object. get Yes
set The name of the setter method to use for the virtual property This method must take a java.lang.String parameter and return a java.lang.Object parameter. set Yes



Add get() and set() Methods

Add the get(String) and set(String, Object) methods to the entity.

The get() method returns a value by property name and the set() method stores a value by property name. The default names for these methods are get and set, and they can be overridden with the @VirtualAccessMethods annotation.

EclipseLink weaves these methods if weaving is enabled, which provides support for lazy loading, change tracking, fetch groups, and internal optimizations. You must use the the get(String) and set(String, Object) signatures, or else weaving will not work.

Elug note icon.png

Note: Weaving is not supported when using virtual access methods with OneToOne mappings. If attempted, an exception will be thrown.

Add a Map

Add a Map as the data structure to store extended attributes and values. These are then mapped to the database in the eclipselink-orm.xml mapping file. See Providing Additional Mappings.

When using field-based access to the entity, annotate the Map with @Transient so it cannot use it for another mapping. When using property-based access, @Transient' is unnecessary.

Example

The following example shows an entity that uses property access.

@Entity
  @VirtualAccessMethods
  public class Customer{
 
    @Id
    private int id;
...
 
    @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);
    }

Designing the Schema

Provide database tables with extra columns for storing flexible mapping data. For example, the following Customer table includes two predefined columns, ID and NAME, and three flexible columns, FLEX_COL1, FLEX_COL2, FLEX_COL3:

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

You can then specify which of those flex columns should be used to persist an extended attribute, as described below, in Providing Additional Mappings.

Providing Additional Mappings

To provide additional mappings, add the mappings to the eclipselink-orm.xml file, for example:

<basic name="idNumber" attribute-type="String">
  <column name="FLEX_COL1"/>
  <access-methods get-method="get" set-method="set"/>
</basic>

//REVIEWERS: Are there any limitations on the types of mappings that support flexible mappings? Also, do you think anything more should be said about what you have to do in eclipselink-orm.xml?//

  • I think that part of this will be addressed by anything we do to document using <access-methods> to specify Virtual mappings--Tom.ware.oracle.com 16:04, 28 June 2011 (UTC)
  • The XML file simply gets treated as another XML file in the list of XML files. As long as you obey all the rules related to what can be overridden, you can use any kind of mapping. The challenge in using non-virtual mappings is how to have the data structures that support them make sense when the document is not there. e.g. if you're going to have an extension that uses an instance variable, for the instances of the application that don't use that extension file, how is that instance variable treated - JPA will likely try to use it for a mapping using its defaulting-rules --Tom.ware.oracle.com 16:04, 28 June 2011 (UTC)

Configuring persistence.xml

Configure persistence unit properties in persistence.xml to indicate that the application should retrieve the flexible mappings from the eclipselink-orm.xml file,. For example:

//REVIEWERS Did I get that intro right? Would different wording be better here? The design doc said “Use persistence unit properties to get your application to use the file."//

  • Both persistence unit propeties and persistence.xml are legitimate use cases. We should describe both. persistence.xml allows either a default, or a single-user file that can be changed. persistence unit properties allow specification of the file at runtime and provides a more dynamic experience.--Tom.ware.oracle.com 16:06, 28 June 2011 (UTC)
<property name="eclipselink.metadata-source" value="XML"/>
<property name="eclipselink.metadata-source.xml.url" value="foo://bar"/>

//REVIEWERS What more can be said about these? See my related questions below, under Configuring the EntityManagerFactory and the Metadata Repository.//

  • Maybe the two sections should go together. We could mention that by default we support using a file at a URL, but it is possible to also override how the repository works and then go into details.--Tom.ware.oracle.com 17:20, 28 June 2011 (UTC)

Examples

The following examples illustrate variations on configuring extensible entities.

Example 1

Example 1 illustrates the following:

  • Field access is used for non-extension fields.
  • Virtual access is used for extension fields, using defaults (get(String) and set(String, Object)) .
  • The get(String) and set(String, Object) methods will be woven, even if no mappings use them, because of the presence of @VirtualAccessMethods.
  • Extensions are mapped in a portable way by specifying @Transient.

Example 1

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

Example 2

Example 2 illustrates the following:

  • Field access is used for non-extension fields.
  • The @VirtualAccessMethods annotation overrides methods to be used for getting and setting.
  • The get(String) and set(String, Object) methods will be woven, even if no mappings use them, because of the presence of @VirtualAccessMethods.
  • Extensions are mapped in a portable way by specifying @Transient.
  • The XML for extended mapping indicates which get() and set() method to use.

Example 2

@Entity
  @VirtualAccessMethods(get="getExtension", set="setExtension")
  public class Address {
 
    @Id
    private int id;
 
    @Transient
    private Map<String, Object> extensions;
 
    public int getId(){
        return id;
    }
 
    public <T> T getExtension(String name) {
        return (T) extensions.get(name);
    }
 
    public Object setExtension(String name, Object value) {
        return extensions.put(name, value);
    }
 
...
<basic name="name" attribute-type="String">
      <column name="FLEX_1"/>
      <access-methods get-method="getExtension" set-method="setExtension"/>
    </basic>

Example 3

Example 3 illustrates the following:

  • Property access is used for non-extension fields.
  • Virtual access is used for extension fields, using defaults (get(String) and set(String, Object))
  • The extensions are mapped in a portable way. No @Transient is required, because property access is used.
  • The get(String) and set(String, Object) methods will be woven, even if no mappings use them, because of the presence of @VirtualAccessMethods.
@Entity
  @VirtualAccessMethods
  public class Address {
 
    private int id;
 
    private Map<String, Object> extensions;
 
    @Id
    public int getId(){
        return id;
    }
 
    public <T> T get(String name) {
        return (T) extensions.get(name);
    }
 
    public Object set(String name, Object value) {
        return extensions.put(name, value);
    }
 
...

Configuring the EntityManagerFactory and the Metadata Repository

//REVIEWERS: I'm not sure about this section. Are these mostly implementation details whose user-facing information is already discussed above? Or should we retain this section to delve deeper into these subjects? If so, please advise what to say here.//

  • I think think this section can be fairly brief. A quick intro to what the repository does and then an indication that it is possible to provide an overriding class and a pointer to the XML that provides the classname--Tom.ware.oracle.com 17:30, 28 June 2011 (UTC)

Extensions are added at bootstrap time through access to a metadata repository. The metadata repository is accessed through a class that provides methods to retrieve the metadata it holds.

Specify the class to use and any configuration information for the metadata repository through persistence unit properties. The entity manager factory checks the metadata repository while bootstrapping for additional mapping information. If additional mapping information is found, the entity manager factory integrates the into the metadata it uses to bootstrap.

Two types of metadata repository are supported: XML and database.

  • There is no "database" at the moment. I'll fix the other doc--Tom.ware.oracle.com 17:30, 28 June 2011 (UTC)

You can provide your own implementation of the class to access the metadata repository. Each metadata repository access class must specify an individual set of properties to use to connect to the repository. //REVIEWERS: Should we provide an example here?//

  • Brief example of how to use the XML. You can use "User-Specified Example" below. And explain that the propeties that start with "com.foo" are implementer-defined.--Tom.ware.oracle.com 17:30, 28 June 2011 (UTC)

<property name="eclipselink.metadata-source" value="myPackage.MyClass"/>

  • And a note that says you can subclass either org.eclipse.persistence.internal.jpa.extensions.MetadataRepository or org.eclipse.persistence.internal.jpa.extensions.XMLMetadataRepository. --Tom.ware.oracle.com 17:30, 28 June 2011 (UTC)


Examples

//REVIEWERS: Can we say something more about these examples to explain them? What? And should all this information be moved up to Configuring persistence.xml?//

  • XML File examples is covered above and could possibly be removed here.--Tom.ware.oracle.com 17:32, 28 June 2011 (UTC)

XML File Example

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


User-Specified Example

<property name="eclipselink.metadata-source" value="com.foo.MetadataRepository"/>
<property name="com.foo.MetadataRepository.location" value="foo://bar"/>
<property name="com.foo.MetadataRepository.extra-data" value="foo-bar"/>

Elug note icon.png

Note: Use [RefreshMetadata()] to refresh the metadata repository.

'''//REVIERWERS: Should that note on refresh metadata be included? Should something more be said about it?//

  • refreshMetadata() is important. We should indicate that if you change the metadata and you want an EntityManager based on the new metadata, you call refreshMetadata on the EntityManagerFactory and then the next EntityManager you get will be based on the new metadata. Additionally, refreshMetadata takes a Map of properties and that map of properties can be used to override the properties previously defined for the metadata-source.--Tom.ware.oracle.com 17:36, 28 June 2011 (UTC)


Eclipselink-logo.gif
Version: DRAFT
Other versions...

Back to the top