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 4: Line 4:
 
|category=JPA
 
|category=JPA
 
|release=2.4.x}}
 
|release=2.4.x}}
 +
  
 
{| class="simple oac_no_warn" width="100%"
 
{| class="simple oac_no_warn" width="100%"
Line 35: Line 36:
 
Use <code>@AdditionalCriteria</code> to define parameterized views on data. You can define additional criteria on entities or mapped superclass. 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.
 
Use <code>@AdditionalCriteria</code> to define parameterized views on data. You can define additional criteria on entities or mapped superclass. 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.
  
<span id="sthref11"></span>
+
<span id="sthref8"></span>
  
 
'''Annotation Elements'''
 
'''Annotation Elements'''
Line 41: Line 42:
 
[[#CHDGJGDCxx|Table 2-1]] describes this annotation's elements.
 
[[#CHDGJGDCxx|Table 2-1]] describes this annotation's elements.
  
<span id="sthref12"></span><span id="CHDGJGDCxx"></span>
+
<span id="sthref9"></span><span id="CHDGJGDCxx"></span>
  
 
'''''Table 2-1 @AdditionalCriteria Annotation Elements'''''
 
'''''Table 2-1 @AdditionalCriteria Annotation Elements'''''
Line 60: Line 61:
 
<br />
 
<br />
  
<span id="sthref13"></span>
+
<span id="sthref10"></span>
  
 
'''Usage'''
 
'''Usage'''
Line 79: Line 80:
 
<br />
 
<br />
  
<span id="sthref14"></span>
+
<span id="sthref11"></span>
  
 
'''Examples'''
 
'''Examples'''
Line 121: Line 122:
 
  &lt;/additional-criteria&gt;
 
  &lt;/additional-criteria&gt;
  
<span id="sthref15"></span>
+
<span id="sthref12"></span>
  
 
'''Uses for Additional Criteria'''
 
'''Uses for Additional Criteria'''
Line 127: Line 128:
 
Uses for additional criteria include:
 
Uses for additional criteria include:
  
<span id="sthref16"></span>
+
<span id="sthref13"></span>
  
 
'''Multitenancy'''
 
'''Multitenancy'''
Line 133: Line 134:
 
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.
 
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.
  
<span id="sthref17"></span>
+
<span id="sthref14"></span>
  
 
'''''Example 2-3 Multitenancy Example 1'''''
 
'''''Example 2-3 Multitenancy Example 1'''''
Line 142: Line 143:
 
  @AdditionalCriteria("this.tenant = 'Billing'")
 
  @AdditionalCriteria("this.tenant = 'Billing'")
  
<span id="sthref18"></span>
+
<span id="sthref15"></span>
  
 
'''''Example 2-4 Multitenancy Example 2'''''
 
'''''Example 2-4 Multitenancy Example 2'''''
Line 166: Line 167:
 
  EntityManager em = factory.createEntityManager(properties);
 
  EntityManager em = factory.createEntityManager(properties);
  
<span id="sthref19"></span>
+
<span id="sthref16"></span>
  
 
'''Soft Delete'''
 
'''Soft Delete'''
Line 175: Line 176:
 
  @AdditionalCriteria("this.isDeleted = false")
 
  @AdditionalCriteria("this.isDeleted = false")
  
<span id="sthref20"></span>
+
<span id="sthref17"></span>
  
 
'''Data History'''
 
'''Data History'''
Line 195: Line 196:
 
<br />
 
<br />
  
<span id="sthref21"></span>
+
<span id="sthref18"></span>
  
 
'''Temporal Filtering'''
 
'''Temporal Filtering'''
Line 204: Line 205:
 
  @AdditionalCriteria("this.startDate &lt;= :viewDate and this.endDate &gt;= :viewDate")
 
  @AdditionalCriteria("this.startDate &lt;= :viewDate and this.endDate &gt;= :viewDate")
  
<span id="sthref22"></span>
+
<span id="sthref19"></span>
  
 
'''Shared Table'''
 
'''Shared Table'''
Line 210: Line 211:
 
For a shared table, there may be inheritance in the table but not in the object model. For example, a <code>SavingsAccount</code> 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.
 
For a shared table, there may be inheritance in the table but not in the object model. For example, a <code>SavingsAccount</code> 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.
  
<span id="sthref23"></span>
+
<span id="sthref20"></span>
  
 
'''See Also'''
 
'''See Also'''
 +
 +
For more information, see:
  
 
----
 
----

Revision as of 11:11, 5 June 2012




EclipseLink JPA Extensions Reference Guide
Version 2.4.x

Search

Contents


<- Previous

Next ->


@AdditionalCriteria

Use @AdditionalCriteria to define parameterized views on data. You can define additional criteria on entities or mapped superclass. 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

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

Additional criteria can provide an additional filtering mechanism for queries. This filtering option, for example, allows you to use an existing additional JOIN expression defined for the entity or mapped superclass and allows you to pass parameters to it.

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.


Note Note:

Additional criteria are not supported with native queries.


Examples

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")

Example 2-1 shows additional criteria defined for the entity Employee and then shows the parameters for the additional criteria set on the entity manager.

Example 2-1 Using @AdditionalCriteria

Define additional criteria on Employee, as follows:


package model;

@AdditionalCriteria("this.company=:COMPANY")
public class Employee {

  ...
}

Set the property on the EntityManager. This example returns all employees of MyCompany.


entityManager.setProperty("COMPANY", "MyCompany");

Example 2-2 illustrates the same example as before, but uses the <additional-criteria> element in the eclipselink-orm.xml mapping file.

Example 2-2 Using <additional-criteria>


<additional-criteria>
  <criteria>this.address.city IS NOT NULL</criteria>
</additional-criteria>

Uses for Additional Criteria

Uses for additional criteria include:

Multitenancy

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.

Example 2-3 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'")

Example 2-4 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

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

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 Note:

EclipseLink also provides specific history support, via HistoryPolicy . See Tracking Changes Using History Policy at http://wiki.eclipse.org/EclipseLink/Examples/JPA/History.


Temporal Filtering

The following example filters on a specific date:


@AdditionalCriteria("this.startDate <= :viewDate and this.endDate >= :viewDate")

Shared Table

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.

See Also

For more information, see:


<- Previous

Next ->

EclispeLink
EclipseLink Home

Search

Contents

Back to the top