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/Examples/JPA/Multitenant"

(Annotation examples)
Line 1: Line 1:
EclipseLink (as of 2.3) supports a shared (striped) multitenant table using a discriminator column(s).
+
EclipseLink (as of 2.3) supports a shared (striped) multitenant table using a tenant discriminator column(s).
  
EclipseLink supports multitenant entities using its <code>@Multitenant</code> annotation or <code><multitenant></code> xml element. The <code>@Multitenant</code> annotation can be used on an <code>@Entity</code> or <code>@MappedSuperclass</code>.
+
EclipseLink supports multitenant entities using its <code>@Multitenant</code> annotation or <code><multitenant></code> xml element. The <code>@Multitenant</code> annotation can be used on an <code>@Entity</code> or <code>@MappedSuperclass</code> and is used in conjunction with the <code>@TenantDiscriminatorColumn</code> or <code><tenant-discriminator-column></code> xml element.
 +
 
 +
The tenant discriminator column defines the tenant identifying database column and there may be 1 or more such columns. These columns can be unmapped or mapped columns. When mapped, they must be marked as read-only. See the annotation and xml examples below.
 +
 
 +
When multitenancy is specified, the tenant discriminator column can default. Its default values are:
 +
 
 +
column name = <code>TENANT_ID</code>
 +
context property = <code>tenant.id</code>
 +
 
 +
Note, through annotations, specifying only a tenant discriminator column itself does not enable a multitenant entity. At a minimum, the <code>@Multitenant</code> must also be specified. Meaning the minimal configuration is:
 +
 
 +
<source lang="java">
 +
@Entity
 +
@Table(name="EMP")
 +
@Multitenant
 +
public Employee() {
 +
  ...
 +
}
 +
</source>
 +
 
 +
The following examples outline other possible configurations using annotations and XML.
  
 
==== Annotation examples ====
 
==== Annotation examples ====
Line 11: Line 31:
 
@Table(name = "CUSTOMER")
 
@Table(name = "CUSTOMER")
 
@Multitenant
 
@Multitenant
@TenantDescriminatorColumn(name = "TENANT", contextProperty = "multi-tenant.id")
+
@TenantDiscriminatorColumn(name = "TENANT", contextProperty = "multi-tenant.id")
 
public Customer() {
 
public Customer() {
 
   ...
 
   ...

Revision as of 14:54, 6 June 2011

EclipseLink (as of 2.3) supports a shared (striped) multitenant table using a tenant discriminator column(s).

EclipseLink supports multitenant entities using its @Multitenant annotation or <multitenant> xml element. The @Multitenant annotation can be used on an @Entity or @MappedSuperclass and is used in conjunction with the @TenantDiscriminatorColumn or <tenant-discriminator-column> xml element.

The tenant discriminator column defines the tenant identifying database column and there may be 1 or more such columns. These columns can be unmapped or mapped columns. When mapped, they must be marked as read-only. See the annotation and xml examples below.

When multitenancy is specified, the tenant discriminator column can default. Its default values are:

column name = TENANT_ID context property = tenant.id

Note, through annotations, specifying only a tenant discriminator column itself does not enable a multitenant entity. At a minimum, the @Multitenant must also be specified. Meaning the minimal configuration is:

@Entity
@Table(name="EMP")
@Multitenant
public Employee() {
  ...
}

The following examples outline other possible configurations using annotations and XML.

Annotation examples

/** Single discriminator tenant column **/
 
@Entity
@Table(name = "CUSTOMER")
@Multitenant
@TenantDiscriminatorColumn(name = "TENANT", contextProperty = "multi-tenant.id")
public Customer() {
  ...
}
 
/** Multiple tenant discriminator columns using multiple tables **/
 
@Entity
@Table(name = "EMPLOYEE")
@SecondaryTable(name = "RESPONSIBILITIES")
@Multitenant(SINGLE_TABLE)
@TenantDiscriminatorColumns({
    @TenantDiscriminatorColumn(name = "TENANT_ID", contextProperty = "employee-tenant.id", length = 20)
    @TenantDiscriminatorColumn(name = "TENANT_CODE", contextProperty = "employee-tenant.code", discriminatorType = STRING, table = "RESPONSIBILITIES")
  }
)
public Employee() {
  ...
}
 
/** Tenant discriminator column mapped as part of the primary key on the database **/
 
@Entity
@Table(name = "ADDRESS")
@Multitenant
@TenantDiscriminatorColumn(name = "TENANT", contextProperty = "tenant.id", primaryKey = true)
public Address() {
  ...
}
 
/** Mapped tenant discriminator column **/
 
@Entity
@Table(name = "Player")
@Multitenant
@TenantDiscriminatorColumn(name = "AGE", contextProperty = "tenant.age")
public Player() {
  ...
 
  @Basic
  @Column(name="AGE", insertable="false", updatable="false")
  public int age;
}

XML examples

<!-- Single tenant discriminator column -->
 
<entity class="model.Customer">
  <multitenant>
    <tenant-discriminator-column name="TENANT context-property="multi-tenant.id""/>
  </multitenant>
  <table name="CUSTOMER"/>
  ...
</entity>
 
<!-- Multiple tenant discriminator columns using multiple tables -->
 
<entity class="model.Employee">
  <multitenant type="SINGLE_TABLE">
    <tenant-discriminator-column name="TENANT_ID" context-property="employee-tenant.id" length="20"/>
    <tenant-discriminator-column name="TENANT_CODE" context-property="employee-tenant.id" discriminator-type="STRING" table="RESPONSIBILITIES"/>
  </multitenant>
  <table name="EMPLOYEE"/>
  <secondary-table name="RESPONSIBILITIES"/>
  ...
</entity>
 
<!-- Tenant discriminator column mapped as part of the primary key on the database -->
 
<entity class="model.Address">
  <multitenant>
    <tenant-discriminator-column name="TENANT" context-property="multi-tenant.id" primary-key="true"/>
  </multitenant>
  <table name="ADDRESS"/>
  ...
</entity>
 
<!-- Mapped tenant discriminator column -->
 
<entity class="model.Player">
  <multi-tenant>
    <tenant-discriminator-column name="AGE" context-property="tenant.age"/>
  </multi-tenant>
  <table name="PLAYER"/>
  ...
  <attributes>
    <basic name="age" insertable="false" updatable="false">
      <column name="AGE"/>
    </basic>
    ...
  </attributes>
  ...
</entity>

Back to the top