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. 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 through properties on the entity manager factory or on the entity manager. When set on the entity manager, you must set the properties before executing the query. They 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("this.address.city 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 example shows the <additional-criteria> element used in the eclipselink-orm.xml descriptor:

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

Version: 2.2.0 DRAFT
Other versions...