Skip to main content
Jump to: navigation, search

EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Additional Criteria

EclipseLink JPA

Defining Additional Criteria for Queries

You can define additional criteria for queries defined for entities or mapped superclasses, using the @AdditionalCriteria annotation or the <additional-criteria> element. 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. This feature can be useful for multi-tenancy, soft deletes, history, shared tables, and temporal filtering.

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.

Set additional criteria parameters can through properties on the entity manager factory or on an entity manager. When set on the entity manager, the properties must be set before any query execution and should not be changed for the lifespan of that entity manager. Properties set on the entity manager override identically named properties set on the entity manager factory.

Additional criteria are not supported with any native queries.

The additional criteria definition supports any valid JPQL string and must use this as an alias to form the additional criteria. For example,

@AdditionalCriteria(" IS NOT NULL")

You can specify additional criteria using the @AdditionalCriteria annotation or the <additional-criteria> element, as shown in the following examples.

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:

package model;
public class Employee {

Then set the property on the EntityManager. This example returns all employees of "MyCompany."

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

The following is a simple example shows the <additional-criteria> element used in the eclipselink-orm.xml</tt> descriptor:

  <criteria> IS NOT NULL</criteria>

Version: 2.2.0 DRAFT
Other versions...

Back to the top