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 7: Line 7:
 
=Additional Criteria=
 
=Additional Criteria=
  
You can add additional criteria to a descriptor's default queries, thereby providing a filtering option. This filtering option, for example, allows you to use an existing additional JOIN expression from a descriptor query manager and allows you to pass parameters to it.  
+
You can define additional criteria for queries executed on entities or mapped superclasses, using the <tt>@AdditionalCriteria</tt> annotation or the <tt><additional-criteria></tt> 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.
  
You can specify additional criteria at the Entity or MappedSuperclass level. When specified at the mapped superclass level, it applies to all inheriting entities, unless those entities define their own additional criteria, in which case the additional criteria from the mapped superclass is 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.  
  
The additional criteria supports any valid JPQL string and must use <tt>this</tt> as an alias to form the additional criteria, for example,
+
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.
  
<pre>
+
Additional criteria are not supported with any native queries.
@Entity
+
@AdditionalCriteria("this.nut.size = :NUT_SIZE and this.nut.color = :NUT_COLOR")public class Bolt {...}
+
</pre>
+
  
Additional criteria parameters are accepted and are 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 life span of that entity manager.
+
The additional criteria definition supports any valid JPQL string and must use <tt>this</tt> as an alias to form the additional criteria. For example,
+
Properties set on the entity manager override those similarly named properties set on the entity manager factory.
+
  
Additional criteria is not supported with any native queries.
+
<source lang="java">
 +
@Entity
 +
@AdditionalCriteria("this.employees.hiredate = :EMPLOYEES_HIREDATE and this.employees.status = :EMPLOYEES_STATUS")
 +
public class Status {...}
 +
</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.
Line 28: Line 27:
 
==Examples==
 
==Examples==
  
Example using the annotation:
+
The following is a simple example using the <tt>@AdditionalCriteria</tt> annotation:
  
 
<source lang="java">
 
<source lang="java">
Line 34: Line 33:
 
</source>
 
</source>
  
Example using XML:
+
 
 +
The following shows a more complete example of how to use this feature.
 +
 
 +
With the additional criteria defined for the <tt>Employee</tt> entity...
 +
 
 +
<source lang="java">
 +
package model;
 +
 +
@AdditionalCriteria("this.company=:COMPANY")
 +
public class Employee {
 +
  ...
 +
}
 +
</source>
 +
 
 +
...setting the following property on the <tt>EntityManager</tt> will return all employees of "MyCompany."
 +
 
 +
<source lang="java">
 +
entityManager.setProperty("COMPANY", "MyCompany");
 +
</source>
 +
 
 +
 
 +
The following is a simple example using the <tt.<additional-criteria></tt> element:
  
 
<source lang="xml">
 
<source lang="xml">

Revision as of 16:02, 28 March 2011

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source


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,

@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 <tt.<additional-criteria></tt> 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