Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be 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/Basic JPA Development/Mapping/Additional Criteria"

m
Line 13: Line 13:
 
=Additional Criteria=
 
=Additional Criteria=
  
You can define additional criteria for queries defined for entities or mapped superclasses, using the <tt>@AdditionalCriteria</tt> annotation or the <tt><additional-criteria></tt> element. Additional criteria can provide an additional filtering mechanism for queries. This feature can be useful, for example, for multi-tenancy, soft deletes, history, shared tables, and temporal filtering.
+
You can define additional criteria for queries defined for entities or mapped superclasses, using the <tt>@AdditionalCriteria</tt> annotation or the <tt><additional-criteria></tt> element. Additional criteria can provide an additional filtering mechanism for queries. 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.
 
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.

Revision as of 16:29, 28 March 2011

EclipseLink JPA


Additional Criteria

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 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.

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,

@Entity
@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.

Examples

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

@AdditionalCriteria("this.address.city 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;
 
@AdditionalCriteria("this.company=:COMPANY")
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:

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


Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...

Back to the top