Skip to main content

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.

Jump to: navigation, search

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"
+
<div class="header">
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+
{| class="simple oac_no_warn" width="100%"
<head>
+
| align="left" valign="top" | <font face="helvetica, arial, sans-serif">'''EclipseLink JPA Extensions Reference Guide'''<br />'''Version 2.4.x'''<br /></font>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
+
| width="144" align="right" valign="bottom" |
<meta http-equiv="Content-Style-Type" content="text/css" />
+
{| class="simple oac_no_warn" width="100%"
<meta http-equiv="Content-Script-Type" content="text/javascript" />
+
|
<title>@AdditionalCriteria</title>
+
| align="center" valign="top" |
 +
[toc.htm  <font size="-2">Contents</font>]
 +
|}
 +
|}
 +
 
 +
----
 +
 
 +
{| class="simple oac_no_warn" width="100"
 +
| align="center" |
 +
[a_additionalcriteria.htm Previous]
 +
| align="center" |
 +
[a_array.htm Next]
 +
|
 +
|}
 +
 
 +
</div><div class="ind">
 +
----
 +
<div class="refsect1">
 +
 
 +
= <font color="#330099" face="arial, helvetica, sans-serif">@AdditionalCriteria</font> =
 +
 
 +
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.
 +
 
 +
'''<font color="#330099" face="arial, helvetica, sans-serif">Annotation Elements</font>'''
 +
 
 +
[#CHDGJGDCxx Table 2-1] describes this annotation's elements.
 +
 
 +
<div class="tblhruleformalwide">
 +
 
 +
'''''<font face="arial, helvetica, sans-serif">Table 2-1 @AdditionalCriteria Annotation Elements</font>'''''
 +
 
 +
{| class="HRuleFormalWide" dir="ltr" title="@AdditionalCriteria Annotation Elements" summary="This table describes the list of attributes for this annotation." width="100%" border="1" frame="hsides" rules="rows" cellpadding="3" frame="hsides" rules="rows"
 +
|- align="left" valign="top"
 +
! id="r1c1-t2" align="left" valign="bottom" | <font face="Arial, Helvetica, sans-serif">'''Attribute'''</font>
 +
! id="r1c2-t2" align="left" valign="bottom" | <font face="Arial, Helvetica, sans-serif">'''Description'''</font>
 +
! id="r1c3-t2" align="left" valign="bottom" | <font face="Arial, Helvetica, sans-serif">'''Default'''</font>
 +
|- align="left" valign="top"
 +
| id="r2c1-t2" headers="r1c1-t2" align="left" |
 +
<code>value</code>
 +
| headers="r2c1-t2 r1c2-t2" align="left" |
 +
(Required) The JPQL fragment to use as the additional criteria.
 +
| headers="r2c1-t2 r1c3-t2" align="left" | <br />
 +
|}
 +
 
 +
<br /></div>
 +
 
 +
'''<font color="#330099" face="arial, helvetica, sans-serif">Usage</font>'''
 +
 
 +
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 <code>@AdditionalCriteria</code> annotation or the <code>&lt;additional-criteria&gt;</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:
 +
 
 
   
 
   
<meta name="date" content="2012-05-17T9:17:7Z" />
+
@AdditionalCriteria("this.address.city IS NOT NULL")
<meta name="robots" content="noarchive" />
+
 
<meta name="doctitle" content="@AdditionalCriteria" />
+
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.
 +
 
 +
Define additional criteria on <code>Employee</code>, as follows:
 +
 
 
   
 
   
 +
package model;
 
   
 
   
 +
@AdditionalCriteria("this.company=:COMPANY")
 +
public class Employee {
 +
  ...
 +
}
 +
 +
then set the property on the <code>EntityManager</code>. This example returns all employees of MyCompany.
 +
 
   
 
   
 +
&lt;additional-criteria&gt;  &lt;criteria&gt;this.address.city IS NOT NULL&lt;/criteria&gt;&lt;/additional-criteria&gt;
 +
 +
'''<font color="#330099" face="arial, helvetica, sans-serif">Uses for Additional Criteria</font>'''
 +
 +
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:
 +
 
   
 
   
<link rel="contents" href="toc.htm" title="Contents" type="text/html" />
+
@AdditionalCriteria("this.tenant = 'Billing'")
<link rel="prev" href="a_additionalcriteria.htm" title="Previous" type="text/html" />
+
 
<link rel="next" href="a_array.htm" title="Next" type="text/html" />
+
Multitenancy Example 2
</head>
+
 
<body bgcolor="#FFFFFF">
+
The following example could be used in an application used by multiple tenants at the same time. The additional criteria is defined as:
<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>&nbsp;</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>&nbsp;</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>&lt;additional-criteria&gt;</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.company=:COMPANY")
+
  @AdditionalCriteria("this.tenant = :tenant")
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>
+
Map properties = new HashMap();
<pre xml:space="preserve" class="oac_no_warn">
+
properties.put("tenant", "ACME");
&lt;additional-criteria&gt; &lt;criteria&gt;this.address.city IS NOT NULL&lt;/criteria&gt;&lt;/additional-criteria&gt;
+
EntityManagerFactory emf = Persistence.createEntityManagerFactory(properties);
</pre>
+
 
<a id="sthref14" name="sthref14"></a>
+
Or
<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>
+
Map properties = new HashMap();
<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>
+
properties.put("tenant", "ACME");
<p>Multitenancy Example 1</p>
+
EntityManager em = factory.createEntityManager(properties);
<p>The following example restricts the data for a &ldquo;Billing&rdquo; client, such as a billing application or billing organization:</p>
+
 
<pre xml:space="preserve" class="oac_no_warn">
+
Soft Delete Example
@AdditionalCriteria("this.tenant = 'Billing'")
+
 
</pre>
+
The following example filters data that is marked as deleted (but which still exists in the table) from a query:
<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.isDeleted = false")
@AdditionalCriteria("this.tenant = :tenant")
+
 
</pre>
+
Data History Example
<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>
+
 
<pre xml:space="preserve" class="oac_no_warn">
+
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.
Map properties = new HashMap();
+
 
properties.put("tenant", "ACME");
+
EntityManagerFactory emf = Persistence.createEntityManagerFactory(properties);
+
@AdditionalCriteria("this.endDate is null")
</pre>
+
 
<p>Or</p>
+
<div><center><div class="inftblnote"><br />
<pre xml:space="preserve" class="oac_no_warn">
+
 
Map properties = new HashMap();
+
{| class="Note oac_no_warn" width="80%" border="1" frame="hsides" rules="groups" cellpadding="3" frame="hsides" rules="groups"
properties.put("tenant", "ACME");
+
| align="left" |
EntityManager em = factory.createEntityManager(properties);
+
<font face="arial, helvetica, sans-serif">'''Note:'''</font>
</pre>
+
 
<p>Soft Delete Example</p>
+
TopLink also provides specific history support, via <code>HistoryPolicy</code> . See Tracking Changes Using History Policy at <code>http://wiki.eclipse.org/EclipseLink/Examples/JPA/History</code>.
<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">
+
 
@AdditionalCriteria("this.isDeleted = false")
+
<br /></div></center></div>
</pre>
+
 
<p>Data History Example</p>
+
Temporal Filtering 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.</p>
+
 
<pre xml:space="preserve" class="oac_no_warn">
+
The following example filters on a specific date::
@AdditionalCriteria("this.endDate is null")
+
 
</pre>
+
<div align="center">
+
@AdditionalCriteria("this.startDate &lt;= :viewDate and this.endDate &gt;= :viewDate")
<div class="inftblnote"><br />
+
 
<table class="Note oac_no_warn" summary="" border="1" width="80%" frame="hsides" rules="groups" cellpadding="3" cellspacing="0">
+
Shared Table Example
<tbody>
+
 
<tr>
+
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.
<td align="left">
+
 
<p><font face="arial, helvetica, sans-serif"><strong>Note:</strong></font></p>
+
'''<font color="#330099" face="arial, helvetica, sans-serif">Examples</font>'''
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 &lt;= :viewDate and this.endDate &gt;= :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>&nbsp;</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>
+
  Code
<td valign="bottom" align="right">
+
 
<table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="225">
+
Alternatively, you can include a few examples using <code>SegmentedList</code> <code>Role = Example</code>.
<tr>
+
 
<td>&nbsp;</td>
+
'''<font color="#330099" face="arial, helvetica, sans-serif">See Also</font>'''
<td align="center" valign="top"><a href="toc.htm">
+
 
<font size="-2">Contents</font></a></td>
+
</div></div><div class="footer">
</tr>
+
----
</table>
+
 
</td>
+
{| class="simple oac_no_warn" width="100%"
</tr>
+
|-
</table>
+
| valign="bottom" |
 +
{| class="simple oac_no_warn" width="100"
 +
|-
 +
|
 +
| align="center" |
 +
[a_additionalcriteria.htm Previous]
 +
| align="center" |
 +
[a_array.htm [[Image:rarrow.gif|Next]]]
 +
|}
 +
| width="34%" align="center" |
 +
[[Image:150px-Eclipselink-logo.gif|EclispeLink]]<br />
 +
| align="right" valign="bottom" |
 +
{| class="simple oac_no_warn" width="225"
 +
|
 +
| align="center" valign="top" |
 +
[toc.htm <font size="-2">Contents</font>]
 +
|}
 +
|}
 +
 
 
</div>
 
</div>
<!-- class="footer" -->
 
</body>
 
</html>
 

Revision as of 09:19, 17 May 2012

EclipseLink JPA Extensions Reference Guide
Version 2.4.x

[toc.htm Contents]


[a_additionalcriteria.htm Previous]

[a_array.htm Next]


@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.

Annotation Elements

[#CHDGJGDCxx Table 2-1] describes this annotation's elements.

Table 2-1 @AdditionalCriteria Annotation Elements

Attribute Description Default

value

(Required) The JPQL fragment to use as the additional criteria.



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>

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, via HistoryPolicy . See Tracking Changes Using History Policy at http://wiki.eclipse.org/EclipseLink/Examples/JPA/History.

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.

Examples


Code

Alternatively, you can include a few examples using SegmentedList Role = Example.

See Also

Back to the top