Skip to main content
Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Additional Criteria"

Line 60: Line 60:
The following is a simple example using the <tt.<additional-criteria></tt> element:
The following is a simple example using the <tt><additional-criteria></tt> element:
<source lang="xml">
<source lang="xml">

Revision as of 16:21, 28 March 2011

EclipseLink JPA

Additional Criteria

You can define additional criteria for queries executed on entities or mapped superclasses, using the @AdditionalCriteria annotation or the <additional-criteria> element. 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.

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.

Additional criteria parameters can also be set 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("this.employees.hiredate = :EMPLOYEES_HIREDATE and this.employees.status = :EMPLOYEES_STATUS")
public class Status {...}

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


The following is a simple example using the @AdditionalCriteria annotation:

@AdditionalCriteria(" IS NOT NULL")

The following shows a more complete example of how to use this feature.

With the additional criteria defined for the Employee entity...

package model;
public class Employee {

...setting the following property on the EntityManager will return all employees of "MyCompany."

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

The following is a simple example using the <additional-criteria> element:

  <criteria> IS NOT NULL</criteria>

Version: 2.2.0 DRAFT
Other versions...

Back to the top