Jump to: navigation, search

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

Line 2: Line 2:
  
 
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>.
 +
 +
==== Annotation examples ====
 +
<source lang="java">
 +
 +
/** Single discriminator tenant column **/
 +
 +
@Entity
 +
@Table(name = "CUSTOMER")
 +
@Multitenant
 +
@TenantDescriminatorColumn(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;
 +
}
 +
</source>

Revision as of 14:44, 6 June 2011

EclipseLink (as of 2.3) supports a shared (striped) multitenant table using a 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.

Annotation examples

/** Single discriminator tenant column **/
 
@Entity
@Table(name = "CUSTOMER")
@Multitenant
@TenantDescriminatorColumn(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;
}