Jump to: navigation, search

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

Line 73: Line 73:
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
|previous= [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Criteria|Criteria]]
+
|previous= [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/ManyToMany|ManyToMany]]
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Native|Native]]
+
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Locking|Locking]]
|up=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying|Querying]]
+
|up=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping|Mapping]]
 
|version=2.2.0 DRAFT}}
 
|version=2.2.0 DRAFT}}

Revision as of 15:36, 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...