@Multitenant(TABLE_PER_TENANT) - separate tables using league id as table name suffix

Extensibility: Each League's divisions, teams, and players can be extended dynamically to store and support querying of additional administrator defined properties.

External Metadata: Using the JPA and JAXB MetadataSource capabilities the tenant specific extensions and mapping definitions are stored in the MySports Admin application enabling dynamic league provisioning and customization.

EMF per Tenant: Extends EclipseLink to construct a separate EMF for each hosted league.

JPA-RS: Excposes both the MySposrts and MySports Admin apps using REST

Example Overview

The MySports example includes the following projects:
MySports: A dynamic web project which deploys the primary application, exposing a JSF and JAX-RS (REST) front-end.

MySports Admin: Administration console for the project.

MySports Tests: A Java project containing Junit tests that verifies the functionality of the example and creates the necessary database tables

Installation and Configuration

In order to setup and run this example the following install and configure steps must be followed carefully.

The MySports application is developed within its own . Using any GIT tools the example can be checked out anonymously.

Customize MySports Tests/eclipselink-example-mysports.properties to point to the same MySQL database as connection pool in GlassFish

Run MySports AllTests run target. This will setup the table and populate the initial leagues

Example Details

MySports Domain (example.mysports.model)

The domain model is that of any arbitrary sports league. The intent is to build an application that captures the state of an arbitrary league and use it in a multitenant application where multiple leagues (tenants) can be hosted. The domain model is intentionally unaware of the potential support for multiple leagues and therefore only models entities that exist within an individual league.

/**
* In the MySports demo a Player entity represents an individual member of a
* team. The Player entity is both multitenant and extensible.
*/
@Entity
@Table(name ="mys_player")
@Multitenant(MultitenantType.SINGLE_TABLE)
@TenantDiscriminatorColumn(name ="LEAGUE_ID", contextProperty = LEAGUE_CONTEXT)
@VirtualAccessMethods
publicclass Player implements Extensible {

Extensible

An application interface used to indicate extensible types and enable their use in the presentation tier. This interface is not required or used by EclipseLink.

Divisions

A non-entity container class used for returning multiple divisions from a JAX-RS call to enable the MOXy marshalling into XML.

Persistence

The persistence layer is the main purpose of this example application. The persistence layer makes use of application bootstrapped persistence contexts with support for an EntityManagerFactory and backing ServerSession (with Cache) per league (tenant). This all bootstrapped off of a single persistence-unit definition.

Runtime Architecture

LeagueRepository

The LeagueRepository is the primary persistence abstraction responsible for league (tenant) specific persistence access managing both JPA persistence units and the MOXy JAXBContext.

EMF per Tenant: EMF created per tenant with a single shared persistence unit definition

Multi-Tenancy

The configuration that enables multi-tenancy usage is the @Multitenant annotation that is specified on one or more entities. In this example application Player, Team, and Division are multitenant and are each configured using:

This specifies that the entities for multiple tenants are stored in a shared table and that the access to this table must limit its results to a specific tenant using the LEAGUE_ID column and the context property value specified by the LEAGUE_CONTEXT ("mysports.league").

Oracle VPD
By default this example application will augment all generated SQL for SELECT, UPDATE, and DELETE to add in an additional comparison for the LEAGU_ID column. With INSERT operations the LEAGUE_ID column will be populated. Alternatively the example application can be configured to use Oracle VPD to handle the SELECT, UPDATE, DELETE SQL additions within the database. To setup and configure the demo application to use Oracle VPD you must be using an Oracle database and must set the mysports.vpd=true in the application's src folder and test's src folder.

Extensible Entities

The entities (Player, Team, and Division) in this example are extensible. This means additional attribute mappings can be dynamically defined. In this case the extensions are tenant specific and the definitions are maintained within the admin application. To make an entity extensible you can use annotations like:

@VirtualAccessMethods
publicclass Player implements Extensible {

This indicates that the default get and set methods will be used. The Extensible interface is not required by EclipseLink but is used in the application to identify classes that support extensions. Within the entity classes the get and set methods along with a dat structure to store the extended values must be provided.

External Metadata Source

The extensions definitions are maintained externally in the Admin application which is accessed using JAX-RS calls. The example application provides an implementation of MetadataSource for both JPA and MOXy. For JPA this source is configured using a persistence unit property in the persistence.xml.

EMF per Tenant

When developing an application that can handle requests from multiple tenants you can choose to have a single EntityManager Factory (EMF) with tenant scoped EntityManagers (EM) or you can choose to have a EMF per tenant. The decision really comes down to the number of tenants and the value of maintaining a cache that can be shared upon user requests for the same tenant. In this example application where there are a limited number of tenants the decision was made to use an EMF per tenant.

The SESSION_NAME property is what uniquely identifies the shared session and cache used for the EMF. If multiple create requests are issues with the same value on the same class-loader they will each be given new EMF instances but will share the same shared session and cache. The LEAGUE_CONTEXT property is the value that is set on the EMF to identify the tenant required by @Multitenant on the entities. The third EMF property is simply a way to cache the MYSportsConfigf instance within the shared session.