Jump to: navigation, search

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

m
Line 17: Line 17:
 
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.
  
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.
+
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.
 
Additional criteria are not supported with any native queries.
Line 24: Line 24:
  
 
<source lang="java">
 
<source lang="java">
@Entity
+
@AdditionalCriteria("this.address.city IS NOT NULL")
@AdditionalCriteria("this.employees.hiredate = :EMPLOYEES_HIREDATE and this.employees.status = :EMPLOYEES_STATUS")
+
public class Status {...}
+
 
</source>
 
</source>
  
 
You can specify additional criteria using the <tt>@AdditionalCriteria</tt> annotation or the <tt>&lt;additional-criteria&gt;</tt> element, as shown in the following examples.
 
You can specify additional criteria using the <tt>@AdditionalCriteria</tt> annotation or the <tt>&lt;additional-criteria&gt;</tt> element, as shown in the following examples.
  
The following is a simple example using the <tt>@AdditionalCriteria</tt> annotation:
+
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.
 
+
<source lang="java">
+
@AdditionalCriteria("this.address.city IS NOT NULL")
+
</source>
+
 
+
 
+
The following shows a more complete example of how to use this feature.
+
  
With the additional criteria defined for the <tt>Employee</tt> entity...
+
Define additional criteria on <tt>Employee</tt>:
  
 
<source lang="java">
 
<source lang="java">
Line 51: Line 42:
 
</source>
 
</source>
  
...setting the following property on the <tt>EntityManager</tt> will return all employees of "MyCompany."
+
Then set the property on the <tt>EntityManager</tt>. This example returns all employees of "MyCompany."
  
 
<source lang="java">
 
<source lang="java">
Line 58: Line 49:
  
  
The following is a simple example using the <tt><additional-criteria></tt> element:
+
The following is a simple example shows the <tt><additional-criteria></tt> element used in the eclipselink-orm.xml</tt> descriptor:
  
 
<source lang="xml">
 
<source lang="xml">

Revision as of 13:22, 4 April 2011

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

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


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