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/Development/Indigo/Multi-Tenancy"

(Metadata)
(Metadata)
Line 37: Line 37:
  
 
To further configure the tenant id field, we could provide and allow the following metadata at the class level:
 
To further configure the tenant id field, we could provide and allow the following metadata at the class level:
 
  
 
<pre>
 
<pre>

Revision as of 11:06, 10 February 2011

Bug ??????

Multi-Tenancy

The goal of this feature is to allow multiple tenants on the same database schema using a tenant id column.

Requirements

Configuration

Persistence unit properties

The tenant id field and a tenant id value can be configured through EclipseLink properties within the persistence.xml file.

  • eclipselink.multi-tenant.id
  • eclipselink.multi-tenant.id-column

When a multi-tenant id is specified, the multi-tenant column will default to "TENANT_ID" if it is not specified by the user. That column is then expected to be available from the following tables of the schema:

  1. @Table
  2. @SecondaryTable

It is not expected for the following tables (which refers back to their related entity through a primary key association):

  1. @CollectionTable
  2. @JoinTable

NOTE: This assumes id generation is shared across persistence units. Otherwise, in a multi-tenant environment, the tenant id becomes part of the primary key and all tables must then have a tenant id (which becomes another join column on the relation tables)

When using DDL generation, the user need not worry about this. The DDL generation framework will be responsible for ensuring all necessary tables have a tenant id column.

Metadata

To further configure the tenant id field, we could provide and allow the following metadata at the class level:

@Target({TYPE}) 
@Retention(RUNTIME)
public @interface @TenantColumn{

    /**
     * (Optional) The name of the column. Defaults to 
     * the property or field name.
     */
    String name() default "";

    /**
     * (Optional) The SQL fragment that is used when 
     * generating the DDL for the column.
     * <p> Defaults to the generated SQL to create a
     * column of the inferred type.
     */
    String columnDefinition() default "";

    /**
     * (Optional) The name of the table that contains the column. 
     * If absent the column is assumed to be in the primary table.
     */
    String table() default "";

    /**
     * (Optional) The column length. (Applies only if a
     * string-valued column is used.)
     */
    int length() default 255;

    /**
     * (Optional) The precision for a decimal (exact numeric) 
     * column. (Applies only if a decimal column is used.)
     * Value must be set by developer if used when generating 
     * the DDL for the column.
     */
    int precision() default 0;

    /**
     * (Optional) The scale for a decimal (exact numeric) column. 
     * (Applies only if a decimal column is used.)
     */
    int scale() default 0;
}

Core

The tenant id and tenant id column will be applied in two places.

  1. We will leverage the current additional join expression from the DescriptorQueryManager to filter tenants. This is similar to the Additional Criteria feature.
  2. For inserts, we will append the tenant id column and value when building the row representation of an object. This is done in the following methods from ObjectBuilder (Note: similar thing is done for the the handling of the discriminiator column within an inheritance hierarchy)
    1. buildRow
    2. buildRowForShallowInsert
    3. buildRowForUpdate
    4. buildRowWithChangeSet
    5. buildTemplateInsertRow

Querying

The tenant id column and value will be supported through the following entity manager operations:

  • persist
  • find
  • refresh

And the following queries:

  • named queries

NOTE: EclipseLink will not modify, therefore, support multi-tenancy through named native queries. When using these types of queries within a multi-tenant environment, the user will need to be aware and handle any multi-tenancy issues themselves directly in their native query. To all intent and purpose, named native queries should be avoided in a multi-tenant environment.

DDL generation

DDL generation will need to support the generation of tenant id columns. The DDL generation of fields is based off the descriptor's fields. During pre-initialization we therefore need to ensure that our tenant id fields are built and added to this list. This should be done after the descriptor table initialization (including inheritance hierarchies) has been preformed.

if (session.hasTenantId()) {
  for (DatabaseTable table : getTables()) {
    DatabaseField tenantIdField = session.getTenantIdField();
    tenantIdField.setTable(table);
    getFields().add(buildField(tenantIdField));    
  } 
}

Back to the top