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: | ||
− | < | + | <div class="header"> |
− | + | ||
− | < | + | {| class="simple oac_no_warn" width="100%" |
− | < | + | | align="left" valign="top" | <font face="helvetica, arial, sans-serif">'''EclipseLink JPA Extensions Reference Guide'''<br />'''Version 2.4.x'''<br /></font> |
− | < | + | | width="144" align="right" valign="bottom" | |
− | < | + | {| class="simple oac_no_warn" width="100%" |
− | + | | | |
− | < | + | | 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><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: | ||
+ | |||
− | + | @AdditionalCriteria("this.address.city IS NOT NULL") | |
− | < | + | |
− | < | + | 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. | ||
+ | |||
+ | <additional-criteria> <criteria>this.address.city IS NOT NULL</criteria></additional-criteria> | ||
+ | |||
+ | '''<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: | ||
+ | |||
− | + | @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 <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, | |
− | + | ||
− | + | ||
− | + | 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") | |
− | @AdditionalCriteria("this.tenant = :tenant") | + | |
− | + | 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. | |
− | Map properties = new HashMap(); | + | |
− | properties.put("tenant", "ACME"); | + | |
− | EntityManagerFactory emf = Persistence.createEntityManagerFactory(properties); | + | @AdditionalCriteria("this.endDate is null") |
− | + | ||
− | + | <div><center><div class="inftblnote"><br /> | |
− | + | ||
− | 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> |
− | + | ||
− | + | 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>. | |
− | + | |} | |
− | + | ||
− | @AdditionalCriteria("this.isDeleted = false") | + | <br /></div></center></div> |
− | + | ||
− | + | Temporal Filtering Example | |
− | + | ||
− | + | The following example filters on a specific date:: | |
− | @AdditionalCriteria("this.endDate is null") | + | |
− | < | + | |
− | < | + | @AdditionalCriteria("this.startDate <= :viewDate and this.endDate >= :viewDate") |
− | <div class="inftblnote"><br /> | + | |
− | + | 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 <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. | |
− | + | ||
− | + | '''<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 | + | |
− | + | ||
− | + | ||
− | + | ||
− | <br /></div> | + | |
− | < | + | |
− | + | ||
− | + | ||
− | + | ||
− | @AdditionalCriteria("this.startDate <= :viewDate and this.endDate >= :viewDate") | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | Code |
− | < | + | |
− | < | + | Alternatively, you can include a few examples using <code>SegmentedList</code> <code>Role = Example</code>. |
− | + | ||
− | < | + | '''<font color="#330099" face="arial, helvetica, sans-serif">See Also</font>''' |
− | + | ||
− | <font size="-2">Contents</font> | + | </div></div><div class="footer"> |
− | + | ---- | |
− | + | ||
− | + | {| class="simple oac_no_warn" width="100%" | |
− | + | |- | |
− | + | | 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> | ||
− | |||
− | |||
− |
Revision as of 09:19, 17 May 2012
EclipseLink JPA Extensions Reference Guide Version 2.4.x |
|
[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 |
---|---|---|
|
(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 |
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