Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/JPA/A additionalcriteria"
m |
m |
||
Line 1: | Line 1: | ||
− | < | + | <!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>@AdditionalCriteria</title> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | - | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | < | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | <meta name="date" content="2012-05-17T9:17:7Z" /> | |
− | + | <meta name="robots" content="noarchive" /> | |
− | + | <meta name="doctitle" content="@AdditionalCriteria" /> | |
− | + | ||
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <link rel="contents" href="toc.htm" title="Contents" type="text/html" /> | |
− | + | <link rel="prev" href="a_additionalcriteria.htm" title="Previous" type="text/html" /> | |
− | + | <link rel="next" href="a_array.htm" title="Next" type="text/html" /> | |
− | + | </head> | |
− | The | + | <body bgcolor="#FFFFFF"> |
− | + | <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 JPA Extensions Reference Guide</b><br /> | ||
+ | <b>Version 2.4.x</b><br /> | ||
+ | </font></td> | ||
+ | <td valign="bottom" align="right" width="144"> | ||
+ | <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> | ||
+ | <tr> | ||
+ | <td> </td> | ||
+ | <td align="center" valign="top"><a href="toc.htm"> | ||
+ | <font size="-2">Contents</font></a></td> | ||
+ | </tr> | ||
+ | </table> | ||
+ | </td> | ||
+ | </tr> | ||
+ | </table> | ||
+ | <hr /> | ||
+ | <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100"> | ||
+ | <tr> | ||
+ | <td align="center"><a href="a_additionalcriteria.htm">Previous</a></td> | ||
+ | <td align="center"><a href="a_array.htm">Next</a></td> | ||
+ | <td> </td> | ||
+ | </tr> | ||
+ | </table> | ||
+ | </div> | ||
+ | <!-- class="header" --> | ||
+ | <div class="ind"><!-- End Header --><a id="BABCJADG" name="BABCJADG"></a> | ||
+ | <hr /> | ||
+ | <div class="refsect1"> | ||
+ | <h1 class="refsect1"><font face="arial, helvetica, sans-serif" color="#330099">@AdditionalCriteria</font></h1> | ||
+ | <p>Use <code>@AdditionalCriteria</code> to define parameterized views on data. You can define additional criteria on entities or mapped superclasses. When specified at the mapped superclass level, the additional criteria definition applies to all inheriting entities, unless those entities define their own additional criteria, in which case those defined for the mapped superclass are ignored.</p> | ||
+ | <a id="sthref11" name="sthref11"></a> | ||
+ | <p><strong><font face="arial, helvetica, sans-serif" color="#330099">Annotation Elements</font></strong></p> | ||
+ | <p><a href="#CHDGJGDCxx">Table 2-1</a> describes this annotation's elements.</p> | ||
+ | <div class="tblhruleformalwide"><a id="sthref12" name="sthref12"></a><a id="CHDGJGDCxx" name="CHDGJGDCxx"></a> | ||
+ | <p><strong><em><font face="arial, helvetica, sans-serif">Table 2-1 @AdditionalCriteria Annotation Elements</font></em></strong></p> | ||
+ | <table class="HRuleFormalWide" title="@AdditionalCriteria Annotation Elements" summary="This table describes the list of attributes for this annotation." dir="ltr" border="1" width="100%" frame="hsides" rules="rows" cellpadding="3" cellspacing="0"> | ||
+ | <col width="27%" /> | ||
+ | <col width="*" /> | ||
+ | <col width="18%" /> | ||
+ | <thead> | ||
+ | <tr align="left" valign="top"> | ||
+ | <th align="left" valign="bottom" id="r1c1-t2"><font face="Arial, Helvetica, sans-serif"><strong>Attribute</strong></font></th> | ||
+ | <th align="left" valign="bottom" id="r1c2-t2"><font face="Arial, Helvetica, sans-serif"><strong>Description</strong></font></th> | ||
+ | <th align="left" valign="bottom" id="r1c3-t2"><font face="Arial, Helvetica, sans-serif"><strong>Default</strong></font></th> | ||
+ | </tr> | ||
+ | </thead> | ||
+ | <tbody> | ||
+ | <tr align="left" valign="top"> | ||
+ | <td align="left" id="r2c1-t2" headers="r1c1-t2"> | ||
+ | <p><code>value</code></p> | ||
+ | </td> | ||
+ | <td align="left" headers="r2c1-t2 r1c2-t2"> | ||
+ | <p>(Required) The JPQL fragment to use as the additional criteria.</p> | ||
+ | </td> | ||
+ | <td align="left" headers="r2c1-t2 r1c3-t2"><br /></td> | ||
+ | </tr> | ||
+ | </tbody> | ||
+ | </table> | ||
+ | <br /></div> | ||
+ | <!-- class="tblhruleformalwide" --> | ||
+ | <a id="sthref13" name="sthref13"></a> | ||
+ | <p><strong><font face="arial, helvetica, sans-serif" color="#330099">Usage</font></strong></p> | ||
+ | <p>Set additional criteria parameters through properties on the entity manager factory or on the entity manager. Properties set on the entity manager override identically named properties set on the entity manager factory. Properties must be set on an entity manager before executing a query. Do not change the properties for the lifespan of the entity manager.</p> | ||
+ | <p>Additional criteria are not supported with native queries.</p> | ||
+ | <p>Specify additional criteria using the <code>@AdditionalCriteria</code> annotation or the <code><additional-criteria></code> element. The additional criteria definition supports any valid JPQL string and must use <code>this</code> as an alias to form the additional criteria. For example:</p> | ||
+ | <pre xml:space="preserve" class="oac_no_warn"> | ||
+ | @AdditionalCriteria("this.address.city IS NOT NULL") | ||
+ | </pre> | ||
+ | <p>The following example shows additional criteria defined for the entity <code>Employee</code> and then shows the parameters for the additional criteria set on the entity manager.</p> | ||
+ | <p>Define additional criteria on <code>Employee</code>, as follows:</p> | ||
+ | <pre xml:space="preserve" class="oac_no_warn"> | ||
+ | package model; | ||
− | @AdditionalCriteria("this.tenant = :tenant") | + | @AdditionalCriteria("this.company=:COMPANY") |
− | + | public class Employee { | |
− | When the tenant acquires its <code>EntityManagerFactory</code> or <code>EntityManager</code>, the persistence/entity manager property tenant is set to the name of the tenant acquiring it. For example, | + | ... |
− | + | } | |
− | + | </pre> | |
− | + | <p>then set the property on the <code>EntityManager</code>. This example returns all employees of MyCompany.</p> | |
− | + | <pre xml:space="preserve" class="oac_no_warn"> | |
− | + | <additional-criteria> <criteria>this.address.city IS NOT NULL</criteria></additional-criteria> | |
− | + | </pre> | |
− | Or | + | <a id="sthref14" name="sthref14"></a> |
− | + | <p><b><font face="arial, helvetica, sans-serif" color="#330099">Uses for Additional Criteria</font></b></p> | |
− | + | <p>Uses for additional criteria include:</p> | |
− | + | <p>Multitenancy Example</p> | |
− | + | <p>In a multitenancy environment, tenants (users, clients, organizations, applications) can share database tables, but the views on the data are restricted so that tenants have access only to their own data. You can use additional criteria to configure such restrictions.</p> | |
− | + | <p>Multitenancy Example 1</p> | |
− | + | <p>The following example restricts the data for a “Billing” client, such as a billing application or billing organization:</p> | |
− | Soft Delete Example | + | <pre xml:space="preserve" class="oac_no_warn"> |
− | + | @AdditionalCriteria("this.tenant = 'Billing'") | |
− | The following example filters data that is marked as deleted (but which still exists in the table) from a query: | + | </pre> |
− | + | <p>Multitenancy Example 2</p> | |
− | + | <p>The following example could be used in an application used by multiple tenants at the same time. The additional criteria is defined as:</p> | |
− | + | <pre xml:space="preserve" class="oac_no_warn"> | |
− | + | @AdditionalCriteria("this.tenant = :tenant") | |
− | Data History Example | + | </pre> |
− | + | <p>When the tenant acquires its <code>EntityManagerFactory</code> or <code>EntityManager</code>, the persistence/entity manager property tenant is set to the name of the tenant acquiring it. For example,</p> | |
− | The following example returns the current data from a query, thus filtering out any out-of-date data, for example data stored in a history table. | + | <pre xml:space="preserve" class="oac_no_warn"> |
− | + | Map properties = new HashMap(); | |
− | + | properties.put("tenant", "ACME"); | |
− | + | EntityManagerFactory emf = Persistence.createEntityManagerFactory(properties); | |
− | + | </pre> | |
− | < | + | <p>Or</p> |
− | + | <pre xml:space="preserve" class="oac_no_warn"> | |
− | + | Map properties = new HashMap(); | |
− | + | properties.put("tenant", "ACME"); | |
− | <font face="arial, helvetica, sans-serif"> | + | EntityManager em = factory.createEntityManager(properties); |
− | + | </pre> | |
− | + | <p>Soft Delete Example</p> | |
− | + | <p>The following example filters data that is marked as deleted (but which still exists in the table) from a query:</p> | |
− | + | <pre xml:space="preserve" class="oac_no_warn"> | |
− | <br /></div>< | + | @AdditionalCriteria("this.isDeleted = false") |
− | + | </pre> | |
− | Temporal Filtering Example | + | <p>Data History Example</p> |
− | + | <p>The following example returns the current data from a query, thus filtering out any out-of-date data, for example data stored in a history table.</p> | |
− | The following example filters on a specific date:: | + | <pre xml:space="preserve" class="oac_no_warn"> |
− | + | @AdditionalCriteria("this.endDate is null") | |
− | + | </pre> | |
− | + | <div align="center"> | |
− | + | <div class="inftblnote"><br /> | |
− | Shared Table Example | + | <table class="Note oac_no_warn" summary="" border="1" width="80%" frame="hsides" rules="groups" cellpadding="3" cellspacing="0"> |
− | + | <tbody> | |
− | For a shared table, there may be inheritance in the table but not in the object model. For example, a SavingsAccount class may be mapped to an <code>ACCOUNT</code> table, but the <code>ACCOUNT</code> table contains both savings account data (<code>SAVINGS</code>) and checking account (<code>CHECKING</code>) data. You can use additional criteria to filter out the checking account data. | + | <tr> |
− | + | <td align="left"> | |
− | + | <p><font face="arial, helvetica, sans-serif"><strong>Note:</strong></font></p> | |
− | + | TopLink also provides specific history support, via <code>HistoryPolicy</code> . See Tracking Changes Using History Policy at <code><a href="http://wiki.eclipse.org/EclipseLink/Examples/JPA/History">http://wiki.eclipse.org/EclipseLink/Examples/JPA/History</a></code>.</td> | |
+ | </tr> | ||
+ | </tbody> | ||
+ | </table> | ||
+ | <br /></div> | ||
+ | <!-- class="inftblnote" --></div> | ||
+ | <p>Temporal Filtering Example</p> | ||
+ | <p>The following example filters on a specific date::</p> | ||
+ | <pre xml:space="preserve" class="oac_no_warn"> | ||
+ | @AdditionalCriteria("this.startDate <= :viewDate and this.endDate >= :viewDate") | ||
+ | </pre> | ||
+ | <p>Shared Table Example</p> | ||
+ | <p>For a shared table, there may be inheritance in the table but not in the object model. For example, a SavingsAccount class may be mapped to an <code>ACCOUNT</code> table, but the <code>ACCOUNT</code> table contains both savings account data (<code>SAVINGS</code>) and checking account (<code>CHECKING</code>) data. You can use additional criteria to filter out the checking account data.</p> | ||
+ | <a id="sthref15" name="sthref15"></a> | ||
+ | <p><strong><font face="arial, helvetica, sans-serif" color="#330099">Examples</font></strong></p> | ||
+ | <pre xml:space="preserve" class="oac_no_warn"> | ||
+ | Code | ||
+ | </pre> | ||
+ | <p>Alternatively, you can include a few examples using <code>SegmentedList</code> <code>Role = Example</code>.</p> | ||
+ | <a id="sthref16" name="sthref16"></a> | ||
+ | <p><strong><font face="arial, helvetica, sans-serif" color="#330099">See Also</font></strong></p> | ||
+ | </div> | ||
+ | <!-- class="refsect1" --></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="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100"> | ||
+ | <col width="*" /> | ||
+ | <col width="48%" /> | ||
+ | <col width="48%" /> | ||
+ | <tr> | ||
+ | <td> </td> | ||
+ | <td align="center"><a href="a_additionalcriteria.htm">Previous</a></td> | ||
+ | <td align="center"><a href="a_array.htm"><img src="../../dcommon/gifs/rarrow.gif" alt="Next" border="0" height="30" width="30" /></a></td> | ||
+ | </tr> | ||
+ | </table> | ||
+ | </td> | ||
+ | <td align="center" width="34%"><img src="http://wiki.eclipse.org/images/thumb/6/6b/Eclipselink-logo.gif/150px-Eclipselink-logo.gif" alt="EclispeLink" border="0" /> <br /> | ||
− | + | </td> | |
− | + | <td valign="bottom" align="right"> | |
− | + | <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="225"> | |
− | + | <tr> | |
− | + | <td> </td> | |
− | + | <td align="center" valign="top"><a href="toc.htm"> | |
− | < | + | <font size="-2">Contents</font></a></td> |
− | + | </tr> | |
− | + | </table> | |
− | + | </td> | |
− | + | </tr> | |
− | + | </table> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</div> | </div> | ||
+ | <!-- class="footer" --> | ||
+ | </body> | ||
+ | </html> |
Revision as of 09:18, 17 May 2012
<!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>@AdditionalCriteria</title>
<meta name="date" content="2012-05-17T9:17:7Z" /> <meta name="robots" content="noarchive" /> <meta name="doctitle" content="@AdditionalCriteria" />
<link rel="contents" href="toc.htm" title="Contents" type="text/html" />
<link rel="prev" href="a_additionalcriteria.htm" title="Previous" type="text/html" />
<link rel="next" href="a_array.htm" title="Next" type="text/html" />
</head>
<body bgcolor="#FFFFFF">
EclipseLink JPA Extensions Reference Guide Version 2.4.x |
|
<a href="a_additionalcriteria.htm">Previous</a> | <a href="a_array.htm">Next</a> |
@AdditionalCriteria
Use @AdditionalCriteria
to define parameterized views on data. You can define additional criteria on entities or mapped superclasses. When specified at the mapped superclass level, the additional criteria definition applies to all inheriting entities, unless those entities define their own additional criteria, in which case those defined for the mapped superclass are ignored.
<a id="sthref11" name="sthref11"></a>
Annotation Elements
<a href="#CHDGJGDCxx">Table 2-1</a> describes this annotation's elements.
Table 2-1 @AdditionalCriteria Annotation Elements
Attribute | Description | Default |
---|---|---|
|
(Required) The JPQL fragment to use as the additional criteria. |
<a id="sthref13" name="sthref13"></a>
Usage
Set additional criteria parameters through properties on the entity manager factory or on the entity manager. Properties set on the entity manager override identically named properties set on the entity manager factory. Properties must be set on an entity manager before executing a query. Do not change the properties for the lifespan of the entity manager.
Additional criteria are not supported with native queries.
Specify additional criteria using the @AdditionalCriteria
annotation or the <additional-criteria>
element. The additional criteria definition supports any valid JPQL string and must use this
as an alias to form the additional criteria. For example:
@AdditionalCriteria("this.address.city IS NOT NULL")
The following example shows additional criteria defined for the entity Employee
and then shows the parameters for the additional criteria set on the entity manager.
Define additional criteria on Employee
, as follows:
package model; @AdditionalCriteria("this.company=:COMPANY") public class Employee { ... }
then set the property on the EntityManager
. This example returns all employees of MyCompany.
<additional-criteria> <criteria>this.address.city IS NOT NULL</criteria></additional-criteria>
<a id="sthref14" name="sthref14"></a>
Uses for Additional Criteria
Uses for additional criteria include:
Multitenancy Example
In a multitenancy environment, tenants (users, clients, organizations, applications) can share database tables, but the views on the data are restricted so that tenants have access only to their own data. You can use additional criteria to configure such restrictions.
Multitenancy Example 1
The following example restricts the data for a “Billing” client, such as a billing application or billing organization:
@AdditionalCriteria("this.tenant = 'Billing'")
Multitenancy Example 2
The following example could be used in an application used by multiple tenants at the same time. The additional criteria is defined as:
@AdditionalCriteria("this.tenant = :tenant")
When the tenant acquires its EntityManagerFactory
or EntityManager
, the persistence/entity manager property tenant is set to the name of the tenant acquiring it. For example,
Map properties = new HashMap(); properties.put("tenant", "ACME"); EntityManagerFactory emf = Persistence.createEntityManagerFactory(properties);
Or
Map properties = new HashMap(); properties.put("tenant", "ACME"); EntityManager em = factory.createEntityManager(properties);
Soft Delete Example
The following example filters data that is marked as deleted (but which still exists in the table) from a query:
@AdditionalCriteria("this.isDeleted = false")
Data History Example
The following example returns the current data from a query, thus filtering out any out-of-date data, for example data stored in a history table.
@AdditionalCriteria("this.endDate is null")
Note: TopLink also provides specific history support, viaHistoryPolicy . See Tracking Changes Using History Policy at <a href="http://wiki.eclipse.org/EclipseLink/Examples/JPA/History">http://wiki.eclipse.org/EclipseLink/Examples/JPA/History</a> . |
Temporal Filtering Example
The following example filters on a specific date::
@AdditionalCriteria("this.startDate <= :viewDate and this.endDate >= :viewDate")
Shared Table Example
For a shared table, there may be inheritance in the table but not in the object model. For example, a SavingsAccount class may be mapped to an ACCOUNT
table, but the ACCOUNT
table contains both savings account data (SAVINGS
) and checking account (CHECKING
) data. You can use additional criteria to filter out the checking account data.
<a id="sthref15" name="sthref15"></a>
Examples
Code
Alternatively, you can include a few examples using SegmentedList
Role = Example
.
<a id="sthref16" name="sthref16"></a>
See Also
</body> </html>