blob: 022493e749b99cbff515068eb9386e74e30de762 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>Using Table-Per-Tenant Multi-Tenancy | EclipseLink 2.7 Understanding EclipseLink</title>
<meta name="generator" content="Oracle DARB XHTML Converter (Mode = document) - Version 1.0.22 Build 1" />
<meta name="date" content="2014-06-10T10:35:14Z" />
<meta name="robots" content="noarchive" />
<meta name="doctitle" content="Using Table-Per-Tenant Multi-Tenancy" />
<meta name="relnum" content="Release 2.7" />
<link rel="stylesheet" type="text/css" href="../../dcommon/style.css" media="screen" />
<link rel="copyright" href="../../dcommon/html/cpyr.htm" title="Copyright" type="text/html" />
<link rel="start" href="../../index.htm" title="Home" type="text/html" />
<link rel="contents" href="toc.htm" title="Contents" type="text/html" />
<link rel="prev" href="multitenancy002.htm" title="Previous" type="text/html" />
<link rel="next" href="multitenancy004.htm" title="Next" type="text/html" />
<!-- START: Disqus --><script type="text/javascript"> var disqus_developer = 0; </script><!-- END: Disqus --><!-- START: Sharethis --><script type="text/javascript">var switchTo5x=true;</script><script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script><script type="text/javascript" src="http://s.sharethis.com/loader.js"></script> <!-- END: Sharethis --></head>
<body bgcolor="#FFFFFF"><iframe id="docheader" frameborder="0" framemargin="0" scrolling="no" src="../../dcommon/header.html"></iframe><script src="http://www.google.com/jsapi" type="text/javascript"></script><script type="text/javascript"> google.load('search', '1', {language : 'en'}); google.setOnLoadCallback(function() { var customSearchOptions = {}; var googleAnalyticsOptions = {}; googleAnalyticsOptions['queryParameter'] = 'q'; googleAnalyticsOptions['categoryParameter'] = ''; customSearchOptions['googleAnalyticsOptions'] = googleAnalyticsOptions; var customSearchControl = new google.search.CustomSearchControl( '016171230611334810008:enkkfjmsqju', customSearchOptions); customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET); var options = new google.search.DrawOptions(); options.setSearchFormRoot('cse-search-form'); customSearchControl.draw('cse', options); }, true);</script><link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" /><div id="cse" style="width:100%;"></div>
<div class="header"><a id="top" name="top"></a>
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td align="left" valign="top"><font face="helvetica, arial, sans-serif"><b>EclipseLink Solutions Guide for EclipseLink</b><br />
Release 2.7</div></td>
<td valign="bottom" align="right" width="144">
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td>&nbsp;</td>
<td align="center" valign="top"><a href="toc.htm"><img src="../../dcommon/images/contents.png" alt="Go To Table Of Contents" border="0" height="16" width="16" /><br />
</td><td>&nbsp;</td><td align="center"><a href="../../" target="_top" class="external text" title="Search" rel="nofollow"><img src="../../dcommon/images/search.png" alt="Search" style="border:0;" /><br /><span class="mini"></span></a></td><td>&nbsp;</td><td align="center"><a href="../eclipselink_otlcg.pdf" title="PDF" target="_blank"><img src="../../dcommon/images/pdf_icon.png" style="padding-right:5px;border:0" alt="PDF"></a></td>
</tr>
</table>
</td>
</tr>
</table>
<hr />
<table class="navigation simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100" align="center">
<tr>
<td align="center"><a href="multitenancy002.htm"><img src="../../dcommon/images/larrow.png" alt="Previous" border="0" height="16" width="16" /></a></td>
<td align="center"><a href="multitenancy004.htm"><img src="../../dcommon/images/rarrow.png" alt="Next" border="0" height="16" width="16" /></a></td>
<td>&nbsp;</td>
</tr>
</table>
</div>
<!-- class="header" -->
<div class="ind"><!-- End Header --><a id="A1235913" name="A1235913"></a><a id="TLADG491" name="TLADG491"></a>
<div class="sect1"><!-- infolevel="all" infotype="General" -->
<h1 class="sect1"><font face="arial, helvetica, sans-serif" color="#330099">Using Table-Per-Tenant Multi-Tenancy</font></h1>
<p>Table-per-tenant multi-tenancy allows multiple tenants of an application to isolate their data in one or more tenant-specific tables. Multiple tenants' tables can be in a shared schema, identified using a prefix or suffix naming pattern; or they can be in separate, tenant-specific schemas. Table-per-tenant entities can be mixed with other multi-tenant type entities within the same persistence unit.</p>
<p>The table-per-tenant multi-tenant type is used in conjunction with:</p>
<ul>
<li>
<p>A tenant table discriminator that specifies the type of discriminator (schema or name with prefix or suffix)</p>
</li>
<li>
<p>A tenant ID to identify the user (configured per entity manager or at the entity manager factory, if isolating the table-per-tenant per persistence unit.)</p>
</li>
</ul>
<p>A single application instance with a shared <code>EntityManagerFactory</code> for a persistence unit can be responsible for handling requests from multiple tenants.</p>
<p>Alternatively, separate <code>EntityManagerFactory</code> instances can be used for each tenant. (This is required when using extensions per tenant.) In this case, tenant-specific schema and table names are defined in an <code>eclipselink-orm.xml</code> configuration file. A <code>MetadataSource</code> must be registered with a persistence unit. The <code>MetadataSource</code> is used to support additional persistence unit metadata provided from outside the application.</p>
<p>For information about <code>MetadataSource</code>, see <a href="metadatasource.htm#BABGDEGB">Chapter 13, "Using an External MetaData Source."</a> See also <code>metadata-source</code> in <em>Java Persistence API (JPA) Extensions Reference for EclipseLink</em>.</p>
<p>The table-per-tenant multi-tenant type enables individual tenant table(s) to be used at the entity level. A tenant context property must be provided on each entity manager after a transaction has started.</p>
<ul>
<li>
<p>The table(s) (<code>Table</code> and <code>SecondaryTable</code>) for the entity are individual tenant tables based on the tenant context. Relationships within an entity that uses a join or a collection table are also assumed to exist within the table-per-tenant context.</p>
</li>
<li>
<p>Multi-tenant metadata can only be applied at the root level of the inheritance hierarchy when using a <code>SINGLE_TABLE</code> or <code>JOINED</code> inheritance strategy. Multi-tenant metadata can be specified in a <code>TABLE_PER_CLASS</code> inheritance hierarchy</p>
</li>
</ul>
<a id="TLADG492" name="TLADG492"></a>
<div class="sect2"><a id="sthref90" name="sthref90"></a>
<h2 class="sect2"><font face="arial, helvetica, sans-serif" color="#330099">Main Tasks for Using Table-Per-Tenant Multi-Tenancy</font></h2>
<p>The following tasks provide instructions for using table-per-tenant multi-tenancy:</p>
<ul>
<li>
<p><a href="#CHDIGFFI">Task 1: Prerequisites</a></p>
</li>
<li>
<p><a href="#CHDGCIGF">Task 2: Enable Table-Per-Tenant Multi-Tenancy</a></p>
</li>
<li>
<p><a href="#CHDGBJBE">Task 3: Specify Tenant Table Discriminator</a></p>
</li>
<li>
<p><a href="#CHDDCDHI">Task 4: Specify a Context Property at Runtime</a></p>
</li>
</ul>
<a id="CHDIGFFI" name="CHDIGFFI"></a><a id="TLADG1128" name="TLADG1128"></a>
<div class="sect3">
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Task 1: Prerequisites</font></h3>
<p>To implement and use table-per-tenant multi-tenancy, you need:</p>
<ul>
<li>
<p>EclipseLink 2.4 or later.</p>
<p>Download EclipseLink from <code><a href="http://www.eclipse.org/eclipselink/downloads/">http://www.eclipse.org/eclipselink/downloads/</a></code>.</p>
</li>
<li>
<p>Any compliant Java Database Connectivity (JDBC) database, including Oracle Database, Oracle Database Express Edition (Oracle Database XE), or MySQL. These instructions are based on Oracle Database XE 11g Release 2.</p>
<p>For the certification matrix, see</p>
</li>
</ul>
</div>
<!-- class="sect3" -->
<a id="CHDGCIGF" name="CHDGCIGF"></a><a id="TLADG493" name="TLADG493"></a>
<div class="sect3">
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Task 2: Enable Table-Per-Tenant Multi-Tenancy</font></h3>
<p>Table-per-tenant multi-tenancy can be enabled declaratively using the <code>@Multitenant</code> annotation; or in an Object Relational Mapping (ORM) XML file using the <code>&lt;multitenant&gt;</code> element, or using annotations and XML together.</p>
<a id="TLADG494" name="TLADG494"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref91" name="sthref91"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Using the @Multitenant and @TenantTableDiscriminator Annotations</font></h4>
<p>To use the <code>@Multitenant</code> annotation, include the annotation with an <code>@Entity</code> or <code>@MappedSuperclass</code> annotation and include the <code>TABLE_PER_TENANT</code> attribute.</p>
<p>For example:</p>
<pre xml:space="preserve" class="oac_no_warn">
@Entity
@Multitenant(TABLE_PER_TENANT
...)
public class Employee {
}
</pre>
<p>The <code>TABLE_PER_TENANT</code> attribute states that clients have a dedicated table or tables (<code>Table</code> and <code>SecondaryTable</code>) associated with the entity.</p>
</div>
<!-- class="sect4" -->
<a id="TLADG495" name="TLADG495"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref92" name="sthref92"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Using the &lt;multitenant&gt; Element</font></h4>
<p>To use the <code>&lt;multitenant&gt;</code> element, include the element within an <code>&lt;entity&gt;</code> element. For example:</p>
<pre xml:space="preserve" class="oac_no_warn">
&lt;entity class="model.Employee"&gt;
&lt;multitenant type="TABLE_PER_TENANT"&gt;
...
&lt;/multitenant&gt;
...
&lt;/entity&gt;
</pre></div>
<!-- class="sect4" --></div>
<!-- class="sect3" -->
<a id="CHDGBJBE" name="CHDGBJBE"></a><a id="TLADG497" name="TLADG497"></a>
<div class="sect3">
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Task 3: Specify Tenant Table Discriminator</font></h3>
<p>The tenant table discriminator describes the type of table discriminator to use in a table-per-tenant multi-tenancy strategy. The tenant table discriminator is identified by a property. You can define your own identifier or use the default property: <code>org.eclipse.persistence.config.PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT = "eclipselink.tenant-id"</code></p>
<p>The tenant table discriminator can be specified at the entity or mapped superclass level, and it must always be accompanied with a <code>Multitenant(TABLE_PER_TENANT)</code> specification. It is not sufficient to specify only a tenant table discriminator.</p>
<p>The tenant table discriminator is used together with an associated application context to indicate which table or tables an application tenant can access.</p>
<a id="TLADG496" name="TLADG496"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref93" name="sthref93"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Using the @TenantTableDiscriminator Annotation</font></h4>
<p>Use the <code>@TenantTableDiscriminator</code> annotation to specify which tables are associated with which tenants. The tenant table discriminator must include a type and a context property:</p>
<ul>
<li>
<p>Use the <code>type</code> attribute to identify what type of discriminator to use:</p>
<ul>
<li>
<p>Use <code>PREFIX</code> to apply the tenant table discriminator as a prefix to all multi-tenant tables.</p>
</li>
<li>
<p>Use <code>SUFFIX</code> to apply the tenant table discriminator as a suffix to all multi-tenant tables.</p>
</li>
<li>
<p>Use <code>SCHEMA</code> to apply the tenant table discriminator as a schema to all multi-tenant tables. This strategy requires appropriate database provisioning.</p>
</li>
</ul>
</li>
<li>
<p>Use the <code>contextProperty</code> attributes to identify the user. The value of the context property is a tenant ID that identifies the user. This can be configured for an entity manager or, if you want to isolate the table-per-tenant per persistence unit, an entity manager factory.</p>
</li>
</ul>
<p>For example:</p>
<pre xml:space="preserve" class="oac_no_warn">
@Entity
@Table(name=&rdquo;EMP&rdquo;)
@Multitenant(TABLE_PER_TENANT)
@TenantTableDiscriminator(type=SCHEMA, contextProperty="eclipselink-tenant.id")
public class Employee {
...
}
</pre></div>
<!-- class="sect4" -->
<a id="TLADG498" name="TLADG498"></a>
<div class="sect4"><!-- infolevel="all" infotype="General" --><a id="sthref94" name="sthref94"></a>
<h4 class="sect4"><font face="arial, helvetica, sans-serif" color="#330099">Using the &lt;tenant-table-discriminator&gt; Element</font></h4>
<p>To use the <code>&lt;tenant-table-discriminator&gt;</code> element, include the element within a <code>&lt;multitenant&gt;</code> element and include the <code>name</code> and <code>context-property</code> attributes. For example:</p>
<pre xml:space="preserve" class="oac_no_warn">
&lt;entity class="Employee"&gt;
&lt;multitenant type="TABLE_PER_TENANT"&gt;
&lt;tenant-table-discriminator type="SCHEMA"
context-property="eclipselink-tenant.id"/&gt;
&lt;/multitenant&gt;
&lt;table name="EMP"&gt;
...
&lt;/entity&gt;
</pre></div>
<!-- class="sect4" --></div>
<!-- class="sect3" -->
<a id="CHDDCDHI" name="CHDDCDHI"></a><a id="TLADG499" name="TLADG499"></a>
<div class="sect3">
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Task 4: Specify a Context Property at Runtime</font></h3>
<p>At runtime, specify the context property using a persistence unit definition passed to an entity manager factory or set on an individual entity manager. For example:</p>
<pre xml:space="preserve" class="oac_no_warn">
&lt;persistence-unit name="multitenant"&gt;
...
&lt;properties&gt;
&lt;property name="tenant.id" value="707"/&gt;
...
&lt;/properties&gt;
&lt;/persistence-unit&gt;
</pre>
<p>To specify a context property at runtime programmatically:</p>
<pre xml:space="preserve" class="oac_no_warn">
HashMap properties = new HashMap();
properties.put(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT, "707");
EntityManager em = Persistence.createEntityManagerFactory("multitenant-pu",
properties).createEntityManager();
</pre>
<p>An entity manager property definition follows:</p>
<pre xml:space="preserve" class="oac_no_warn">
EntityManager em =
Persistence.createEntityManagerFactory("multitenant-pu").createEntityManager();
em.beginTransaction();
em.setProperty("other.tenant.id.property", "707");
em.setProperty(EntityManagerProperties.MULTITENANT_PROPERTY_DEFAULT, "707");
...
</pre></div>
<!-- class="sect3" -->
<a id="TLADG1129" name="TLADG1129"></a>
<div class="sect3"><a id="sthref95" name="sthref95"></a>
<h3 class="sect3"><font face="arial, helvetica, sans-serif" color="#330099">Task 5: Perform Operations and Queries</font></h3>
<p>The tenant discriminator column is used at runtime through entity manager operations and querying. The tenant discriminator column and value are supported through the following entity manager operations:</p>
<ul>
<li>
<p><code>persist()</code></p>
</li>
<li>
<p><code>find()</code></p>
</li>
<li>
<p><code>refresh()</code></p>
</li>
</ul>
<p>The tenant discriminator column and value are supported through the following queries:</p>
<ul>
<li>
<p>Named queries</p>
</li>
<li>
<p>Update all</p>
</li>
<li>
<p>Delete all</p>
</li>
</ul>
<div align="center">
<div class="inftblnote"><br />
<table class="Note oac_no_warn" summary="" border="1" width="80%" frame="hsides" rules="groups" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<td align="left">
<p class="note"><img src="../../dcommon/images/note_icon.png" width="16" height="16" alt="Note" style="vertical-align:middle;padding-right:5px;" />Note:</p>
<p>Multi-tenancy is not supported through named native queries. To use named native queries in a multi-tenant environment, manually handle any multi-tenancy issues directly in the query. In general, it is best to avoid named native queries in a multi-tenant environment.</p>
</td>
</tr>
</tbody>
</table>
<br /></div>
<!-- class="inftblnote" --></div>
</div>
<!-- class="sect3" --></div>
<!-- class="sect2" --></div>
<!-- class="sect1" --></div>
<!-- class="ind" -->
<!-- Start Footer -->
<div class="footer">
<hr />
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%">
<col width="33%" />
<col width="*" />
<col width="33%" />
<tr>
<td valign="bottom">
<table class="navigation simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100" align="center">
<col width="*" />
<col width="48%" />
<col width="48%" />
<tr>
<td>&nbsp;</td>
<td align="center"><a href="multitenancy002.htm"><img src="../../dcommon/images/larrow.png" alt="Previous" border="0" height="16" width="16" /></a></td>
<td align="center"><a href="multitenancy004.htm"><img src="../../dcommon/images/rarrow.png" alt="Next" border="0" height="16" width="16" /></a></td>
</tr>
</table>
</td>
<td align="center" width="34%"><a href="http://www.eclipse.org/eclipselink/" title="EclipseLink home"><img src="../../dcommon/images/ellogo.png" alt="EclipseLink" width="150" border="0" /></a><br />
<font size="-2">Copyright&nbsp;&copy;&nbsp;2014,&nbsp;Oracle&nbsp;and/or&nbsp;its&nbsp;affiliates.&nbsp;All&nbsp;rights&nbsp;reserved.</font> <a href="../../dcommon/html/cpyr.htm"><br />
<td valign="bottom" align="right">
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="225">
<tr>
<td>&nbsp;</td>
<td align="center" valign="top"><a href="toc.htm"><img src="../../dcommon/images/contents.png" alt="Go To Table Of Contents" border="0" height="16" width="16" /><br />
</td><td>&nbsp;</td><td align="center"><a href="../../" target="_top" class="external text" title="Search" rel="nofollow"><img src="../../dcommon/images/search.png" alt="Search" style="border:0;" /><br /><span class="mini"></span></a></td><td>&nbsp;</td><td align="center"><a href="../eclipselink_otlcg.pdf" title="PDF" target="_blank"><img src="../../dcommon/images/pdf_icon.png" style="padding-right:5px;border:0" alt="PDF"></a></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<!-- class="footer" -->
<div id="copyright">Copyright &copy; 2014 by The Eclipse Foundation under the <a href="http://www.eclipse.org/org/documents/epl-v10.php">Eclipse Public License (EPL)</a><br /> <script type="text/javascript">var LastUpdated = document.lastModified;document.writeln ("Updated: " + LastUpdated);</script> </div><!-- START: Analytics --><script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-1608008-2']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script><!-- END: Analytics --><!-- START: Sharethis --><script>var options={ "publisher": "e2fe9e07-fab6-4f84-83ea-0991b429842c", "position": "right", "ad": { "visible": false, "openDelay": 5, "closeDelay": 0}};var st_hover_widget = new sharethis.widgets.hoverbuttons(options);</script><!-- END: Sharethis --></body>
</html>