Skip to main content

Notice: This Wiki is now read only and edits are no longer 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/Development/AdditionalCriteria"

(Additional Criteria Requirements and Design)
(A more complex example)
 
(93 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
<div style="float:right;width:300px">
 +
__TOC__
 +
</div>
 +
 
== Additional Criteria Requirements and Design  ==
 
== Additional Criteria Requirements and Design  ==
  
Line 11: Line 15:
 
A point form list of requirements are then as follows:  
 
A point form list of requirements are then as follows:  
  
*Provide a mechanism for users to achieve the following operations
+
*Provide a mechanism for users to achieve:
** Multi-tenancy
+
**Multi-tenancy
** Soft deletes
+
**Soft deletes
 +
**Temporal filtering
 
*Simplify the definition and usage of EclipseLink's additional join expressions using Eclipselink metadata.  
 
*Simplify the definition and usage of EclipseLink's additional join expressions using Eclipselink metadata.  
 
*Allow additional criteria to be specified through the use of annotations and xml.  
 
*Allow additional criteria to be specified through the use of annotations and xml.  
Line 24: Line 29:
 
== Metadata  ==
 
== Metadata  ==
  
The following sections will detail the metadata changes and modifications that are required. The additional criteria will be available in the form of annotations and xml and will be used to form the descritor query manager's additionalJoinExpression that is applied to all queries.  
+
The following sections will detail the metadata changes and modifications that are required. The additional criteria will be available in the form of annotations and xml and will be used to form the descritor query manager's additionalJoinExpression that is applied to all queries for that descriptor.  
  
 
=== @AdditionalCriteria  ===
 
=== @AdditionalCriteria  ===
<pre>/**
+
<source lang="java">
  * Can be specified at the Entity or MappedSuperclass level. When specified at  
+
/**
* the mapped superclass level, it applies to all inheriting entities unless
+
  * An additional criteria can be specified at the Entity or MappedSuperclass  
  * those entities define their own additional criteria, at which point the  
+
* level. When specified at the mapped superclass level, it applies to all  
* additional criteria from the mapped superclass is ignored.  
+
  * inheriting entities unless those entities define their own additional  
 +
* criteria, at which point the additional criteria from the mapped superclass  
 +
* is ignored.
 +
*
 +
* The additional criteria supports any valid JPQL string and must use 'this'
 +
* as an alias to form your additional criteria. E.G.,
 +
*
 +
* @Entity
 +
* @AdditionalCriteria("this.nut.size = :NUT_SIZE and this.nut.color = :NUT_COLOR")
 +
* public class Bolt {...}
 +
 +
* Additional criteria parameters are also 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.
 +
*
 +
* Properties set on the entity manager will override those similarly named
 +
* properties set on the entity manager factory.
 +
*
 +
* Additional criteria is not supported with any native queries.
 
  *  
 
  *  
* Additional criteria can be specified as a single item or as multiples using
 
* the plural AdditionalCriterias annotation.
 
*
 
 
  * @author Guy Pelletier
 
  * @author Guy Pelletier
 
  * @since EclipseLink 2.2
 
  * @since EclipseLink 2.2
Line 42: Line 63:
 
@Retention(RUNTIME)
 
@Retention(RUNTIME)
 
public @interface AdditionalCriteria {
 
public @interface AdditionalCriteria {
    /**
 
    * (Optional) The name of the additional criteria. Naming allows the
 
    * capability of turning off individual criteria based on name.
 
    */
 
    String name() default "default";
 
   
 
    /**
 
    * (Optional) By default, the additional criteria is not enabled and applied.
 
    */
 
    boolean enabled() default false;
 
   
 
 
     /**
 
     /**
 
     * (Required) The JPQL fragment to use as the additional criteria.
 
     * (Required) The JPQL fragment to use as the additional criteria.
Line 58: Line 68:
 
     String value();
 
     String value();
 
}
 
}
</pre>  
+
</source>
=== @AdditionalCriterias  ===
+
 
<pre>/**
+
* An AdditionalCriterias annotation allows the definition of multiple
+
* AdditionalCriteria.
+
*
+
* @see org.eclipse.persistence.annotations.AdditionalCriteria
+
* @author Guy Pelletier
+
* @since EclipseLink 2.2
+
*/
+
@Target({TYPE})
+
@Retention(RUNTIME)
+
public @interface AdditionalCriterias {
+
    /**
+
    * (Required) An array of additional criteria.
+
    */
+
    AdditionalCriteria[] value();
+
}
+
</pre>
+
 
=== &lt;additional-criteria&gt;  ===
 
=== &lt;additional-criteria&gt;  ===
<pre>&lt;xsd:complexType name="additional-criteria"&gt;
+
 
   &lt;xsd:annotation&gt;
+
An additional criteria complex element will be created to allow future expansion.
     &lt;xsd:documentation&gt;
+
 
     ...
+
<source lang="xml">
    &lt;/xsd:documentation&gt;
+
<xsd:complexType name="additional-criteria">
   &lt;/xsd:annotation&gt;
+
   <xsd:annotation>
   &lt;xsd:sequence&gt;
+
     <xsd:documentation>
       &lt;xsd:element name="criteria" type="xsd:string"/&gt;
+
     /**
   &lt;/xsd:sequence&gt;
+
      * An additional criteria can be specified at the Entity or MappedSuperclass
  &lt;xsd:attribute name="name" type="xsd:string"/&gt;
+
      * level. When specified at the mapped superclass level, it applies to all
  &lt;xsd:attribute name="enabled" type="xsd:boolean"/&gt;
+
      * inheriting entities unless those entities define their own additional
&lt;/xsd:complexType&gt;
+
      * criteria, at which point the additional criteria from the mapped superclass
</pre>  
+
      * is ignored.
 +
      *
 +
      * The additional criteria supports any valid JPQL string and must use 'this'
 +
      * as an alias to form your additional criteria. E.G.,
 +
      *
 +
      * @Entity
 +
      * @AdditionalCriteria("this.nut.size = :NUT_SIZE and this.nut.color = :NUT_COLOR")
 +
      * public class Bolt {...}
 +
      * 
 +
      * Additional criteria parameters are also 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.
 +
      *
 +
      * Properties set on the entity manager will override those similarly named
 +
      * properties set on the entity manager factory.
 +
      *
 +
      * Additional criteria is not supported with any native queries.
 +
      *
 +
      * @author Guy Pelletier
 +
      * @since EclipseLink 2.2
 +
      */
 +
    @Target({TYPE})
 +
    @Retention(RUNTIME)
 +
    public @interface AdditionalCriteria {
 +
        /**
 +
          * (Required) The JPQL fragment to use as the additional criteria.
 +
          */
 +
        String value();
 +
      }
 +
 
 +
    </xsd:documentation>
 +
   </xsd:annotation>
 +
   <xsd:sequence>
 +
       <xsd:element name="criteria" type="xsd:string"/>
 +
   </xsd:sequence>
 +
</xsd:complexType>
 +
</source>  
 
The additional-criteria element will be added to entity and mapped-superclass complex elements as follows  
 
The additional-criteria element will be added to entity and mapped-superclass complex elements as follows  
<pre>&lt;xsd:complexType name="entity"&gt;
+
<source lang=xml>
     &lt;xsd:annotation&gt;
+
<xsd:complexType name="entity">
       &lt;xsd:documentation&gt;
+
     <xsd:annotation>
 +
       <xsd:documentation>
 
       ...
 
       ...
       &lt;/xsd:documentation&gt;
+
       </xsd:documentation>
     &lt;/xsd:annotation&gt;
+
     </xsd:annotation>
     &lt;xsd:sequence&gt;
+
     <xsd:sequence>
 
       ...
 
       ...
       &lt;xsd:element name="additional-criteria" type="orm:additional-criteria" maxOccurs="unbounded"/&gt;
+
       <xsd:element name="additional-criteria" type="orm:additional-criteria" minOccurs="0"/>
 
       ...
 
       ...
     &lt;/xsd:sequence&gt;
+
     </xsd:sequence>
 
     ...
 
     ...
&lt;/xsd:complexType&gt;
+
</xsd:complexType>
  
&lt;xsd:complexType name="mapped-superclass"&gt;
+
<xsd:complexType name="mapped-superclass">
     &lt;xsd:annotation&gt;
+
     <xsd:annotation>
       &lt;xsd:documentation&gt;
+
       <xsd:documentation>
 
       ...
 
       ...
       &lt;/xsd:documentation&gt;
+
       </xsd:documentation>
     &lt;/xsd:annotation&gt;
+
     </xsd:annotation>
     &lt;xsd:sequence&gt;
+
     <xsd:sequence>
 
       ...
 
       ...
       &lt;xsd:element name="additional-criteria" type="orm:additional-criteria" maxOccurs="unbounded"/&gt;
+
       <xsd:element name="additional-criteria" type="orm:additional-criteria" minOccurs="0"/>
 
       ...
 
       ...
     &lt;/xsd:sequence&gt;
+
     </xsd:sequence>
 
     ...
 
     ...
&lt;/xsd:complexType&gt;
+
</xsd:complexType>
</pre>  
+
</source>
 +
 
 
=== Example  ===
 
=== Example  ===
<pre> @AdditionalCriteria(
+
<pre>
    name="CitySpecified",
+
@AdditionalCriteria("this.address.city IS NOT NULL")
    enabled=true,
+
 
    value="address.city IS NOT NULL"
+
&lt;additional-criteria&gt;
  )
+
  &lt;criteria&gt;this.address.city IS NOT NULL&lt;/criteria&gt;
 +
&lt;/additional-criteria&gt;
 +
</pre>
  
  &lt;additional-criteria name="CitySpecified" enabled="true"&gt;
 
    &lt;criteria&gt;address.city IS NOT NULL&lt;/criteria&gt;
 
  &lt;/additional-criteria&gt;
 
</pre>
 
 
=== Metadata overriding and merging  ===
 
=== Metadata overriding and merging  ===
  
The additional criteria can be overridden from the eclipselink-orm.xml. XML file merging will be available as well in its current form, simply expanded to include the additional criteria.  
+
The additional criteria can be overridden from the eclipselink-orm.xml. XML file merging will be available as well in its current form, simply expanded to include the additional criteria. The following will be the order of precedence used to determine the which additional-criteria is applied to the descriptor.
 
+
An example of the XML override is as follows:
+
 
+
*mapping.xml defines:
+
**additional-criteria named - A
+
**additional-criteria named - B
+
*eclipselink.xml defines:
+
**additional-criteria named - A
+
**additional-criteria named - C
+
 
+
The outcome of additional criteria added to the descriptor's query event manager:
+
 
+
*A - from eclipselink-orm.xml
+
*B - from mapping.xml
+
*C - from eclipselink-orm.xml
+
 
+
The additional criteria that is applied then is as follows:
+
 
+
#From XML
+
#From the entity class
+
#From a mapped superclass (first one to define additional criteria)
+
  
Meaning, where ever the additional criteria is found, it must be the complete set of additional criteria that should be added to the descriptor's query event manager.
+
#eclipselink-orm.xml
 +
#mapping file
 +
#on the entity class
 +
#on a mapped superclass (first one to define one)
  
 
=== Internal mapping  ===
 
=== Internal mapping  ===
  
The additional criteria will be used to form the additionalJoinExpression from the descriptor's query manager. The core sections below will discuss the internal handling of this additional criteria metadata.  
+
The additional criteria will be used to form the additionalJoinExpression from the descriptor's query manager.
  
 
== Core  ==
 
== Core  ==
Line 168: Line 180:
 
=== DescriptorQueryManager  ===
 
=== DescriptorQueryManager  ===
  
The additional criterias will be stored on the query manager, keyed on name and can be enabled/disabled using properties set on the EntityManager (see IsolatedSession API below). Additional criteria should not be used in conjunction with setting the additional join expression directly. Setting additional criteria will set and control the additional join expression.  
+
The additional criteria will be used to form the existing additional join expression stored on the descriptor query manager. Care must be taken by those users who currently set the additional join expression directly through a customizer. Adding additional criteria will be AND'ed to this expression.
  
==== New API ====
+
The metadata will set the additional criteria through the following new API on the DescriptorQueryManager.
<pre>public void addAdditionalCriteria(String jpqlFragment, String name, boolean enabled)
+
</pre>
+
==== Exceptions  ====
+
  
#JPQL parse exception
+
<source lang="java">public void setAdditionalCriteria(String jpqlFragment)</source>
#Invalid additional criteria name (DescriptorException)
+
  
=== IsolatedSession API  ===
+
The jpql fragment will be parsed into the additional join expression at post-initialization time. Any parsing exceptions will be thrown at that time.
  
To leverage the enable/disable users will need to use an isolated session. This session will be responsible for controlling the additional criteria to be applied to queries.
+
Other new API added to DescriptorQueryManager:
<pre>public void disableAdditionalCriteria(String name)
+
public void enableAdditionalCriteria(String name)
+
public void setAdditionalCriteriaParameterValue(String name, Object value)
+
</pre>
+
=== JPQL fragment  ===
+
  
The current notion will be to tie into the existing JPQL parser to parse the additional criteria where the only alias allowed will be 'this'. We will prepend the select statement, e.g. "Select this from &lt;class&gt; this where" and harness the selection criteria from that resulting query and appended to the executing query.
+
<source lang="java">
 +
HashMap<String, Class> getAdditionalCriteriaArguments() // key = name and value = type
 +
boolean hasAdditionalCriteria()
 +
boolean hasAdditionalCriteriaArguments()
 +
</source>
  
For an additional criteria group, each selection criteria harnessed from each additional criteria within the group will be joined with an 'AND'
+
=== JPQL fragment parsing ===
  
Items that will not be supported in the JPQL fragment are:
+
We will tie into the existing JPQL parser to parse the additional criteria where the only alias allowed will be 'this'. We will prepend the following select statement with the additional criteria JPQL fragment and harness the selection criteria from that resulting query and set it to be the additional join expression.
  
*multiple selects
+
<pre>Select this from &lt;class&gt; this where</pre>
*order by
+
*group by
+
*... more to come ...
+
  
== Application  ==
+
The JPQL will be parsed at descriptor postInitialization time. Multiple selects will not be supported in the JPQL fragment.
  
New properties will be added to control the enabling/disabling of properties and to provide the parameter values. These properties can be set at both the EntityManagerFactory and EntityManager level. At the EntityManager level, however, the user must be using an isolated session.
+
=== Parameters ===
  
Modifying the additional criteria properties at the EntityManager level through the following API will not be allowed and an exception will be thrown. AdditionalCriteria properties should only be supported at create entity manager time.  
+
At runtime, users will provide parameters to their additional criteria during EntityManager create or through a set property call before any query execution. Changing the parameters during the lifespan of the EntityManager is not defined and could lead to a 'corrupted' cache and unpredictable results.  
  
#setProperties
+
Parameters are global in scope and are added to the active JPA context (server session) and will be applied all additional criteria that accept the same parameter name at query execution time.
#setProperty
+
#find
+
#refresh
+
#remove
+
  
=== Enabling/Disabling additional criteria  ===
+
'''NOTE:''' Users must be careful when selecting their parameter names so as to not conflict with existing JPA and Eclipselink properties.
  
The additional criteria will be enabled and disabled using the following EntityManagerFactory properties (added to org.eclipse.persistence.config.PersistenceUnitProperties):  
+
Query parameters and their types will be available from the DescriptorQueryManager after postInitialization through the following API:
  
*eclipselink.enable-additional-criteria
+
<source lang="java">
*eclipselink.disable-additional-criteria
+
HashMap<String, Class> getAdditionalCriteriaArguments()
 +
</source>
  
Both properties accept the entity name (or qualified name) appended to the property name to determine the additional criteria that is to be enabled/disabled.  
+
These parameters are added to each executing query during its prepare. More specifically in the call to buildArgumentFields(). Following that, the parameter values are added to each executing query before its execution in the call to rowFromArguments(List, AbstractSession). The properties are extracted from the session given. If an additional criteria parameter value is not found, a new exception QueryException.argumentFromAdditionalCriteriaMissing is thrown. To ease the checking if a parameter is an additional criteria parameter, the following API had been added to DatabaseQuery:
  
Example:
+
<source lang="java">
<pre>eclipselink.enable-additional-criteria.Employee = CompanyFilter</pre>  
+
boolean isAdditionalCriteriaArgument(String argument)
This would then enable the additional criteria named 'CompanyFilter' on the model.Employee's DescriptorQueryManager.
+
</source>
  
==== Bulk Enabling/Disabling additional criteria  ====
+
Beyond that, existing checking remains in place for an incorrect number of arguments vs. number of values provided, that is, QueryException.argumentSizeMismatchInQueryAndQueryDefinition
  
There is currently no bulk enabling/disable of additional criteria across all descriptors. They must all be enabled/disabled separately.
+
=== Example  ===
  
=== Passing parameters to the additional criteria ===
+
With the given additional additional criteria  
 +
<source lang="java">
 +
package model;
  
The additional criteria parameters will also be set using EntityManagerFactory properties:  
+
@AdditionalCriteria("this.company=:COMPANY")
 +
public class Employee {
 +
  ...
 +
}
 +
</source>
  
The following properties will be added to org.eclipse.persistence.config.PersistenceUnitProperties
+
Setting the following property on the EntityManager would return all employees of Oracle.  
  
*eclipselink-additional-criteria-parameter
+
<source lang="java">entityManager.setProperty("COMPANY", "Oracle");</source>
  
This property accept the entity name and parameter appended to the property name. The entity name and parameter should be separated by a semi-colon.
+
=== A more complex example ===
  
Note: Specifying a parameter to a disabled additional criteria will have no effect until the additional criteria containing the parameter is enabled.  
+
<source lang="java">
 +
@Entity
 +
@AdditionalCriteria("this.nut.size = :NUT_SIZE and this.nut.color = :NUT_COLOR order by this.id")
 +
public class Bolt {
 +
...
 +
}
 +
</source>
  
Example:
+
This additional criteria accepts two parameters across a 1-1 relation and is order by id.
<pre>eclipselink.additional-criteria-parameter.model.Employee:COMPANY = ORACLE</pre>
+
=== Example  ===
+
 
+
With the given additional additional criterias
+
<pre>
+
package model;
+
 
+
@AdditionalCriterias({
+
  @AdditionalCriteria(
+
    name="Company",
+
    enabled=true,
+
    value="company=:COMPANY"),
+
  @AdditionalCriteria(
+
    name="MaleEmps",
+
    enabled=true,
+
    value="gender = 'M'"),
+
  @AdditionalCriteria(
+
    name="FemaleEmps",
+
    enabled=false,
+
    value="gender = 'F'")
+
)
+
public class Employee {
+
  ...
+
}
+
  
</pre>
+
=== Exceptions/Open Issues/Future ===
Setting the following property on the EntityManager would return all male employees at Oracle.
+
<pre>eclipselink.additional-criteria-parameter.model.Employee:COMPANY = ORACLE
+
</pre>
+
Passing the following properties would return all female employees at Oracle.
+
<pre>eclipselink.enable-additional-criteria.Employee = FemaleEmps
+
eclipselink.disable-additional-criteria.Employee = MaleEmps
+
eclipselink.additional-criteria-parameter.Employee:COMPANY = ORACLE
+
</pre>
+
==== Bulk Parameter passing  ====
+
  
There is currently no bulk parameter passing to all additional criteria across all descriptors. They must all be set for each descriptor separately.
+
* Exception: Additional criteria specified on a descriptor within an inheritance hierarchy using views is not supported.
 +
* Future: Allow additional criteria to be specified at the mapping level.
 +
* Future: Allow multiple additional criteria with option to enable/disable individual additonal criteria

Latest revision as of 17:16, 9 March 2011

Additional Criteria Requirements and Design

Enhancement Request: bug 322008

This work will introduce an additional criteria EclipseLink users can apply to a descriptors default queries in turn providing them with a filtering option. This filtering option will allow users to leverage the existing additional join expression from a descriptor query manager and allow parameters to be passed to it.

See the following blog from Doug discussing filters and their current usage though a descriptor customizer.

http://java-persistence.blogspot.com/2010/08/eclipselink-filters-how-to.html

A point form list of requirements are then as follows:

  • Provide a mechanism for users to achieve:
    • Multi-tenancy
    • Soft deletes
    • Temporal filtering
  • Simplify the definition and usage of EclipseLink's additional join expressions using Eclipselink metadata.
  • Allow additional criteria to be specified through the use of annotations and xml.
  • Allow for xml mapping file merging and overridding using the eclipselink-orm.xml
  • Allow for JPQL fragments to specify the details of the additional criteria.
  • Allow for a mechanism to pass parameters to the additional criteria.
  • Easy to use and forward compatible.
  • Respect the general JPA look and feel.

Metadata

The following sections will detail the metadata changes and modifications that are required. The additional criteria will be available in the form of annotations and xml and will be used to form the descritor query manager's additionalJoinExpression that is applied to all queries for that descriptor.

@AdditionalCriteria

/**
 * An additional criteria can be specified 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, at which point the additional criteria from the mapped superclass 
 * is ignored.
 * 
 * The additional criteria supports any valid JPQL string and must use 'this' 
 * as an alias to form your additional criteria. E.G.,
 * 
 * @Entity
 * @AdditionalCriteria("this.nut.size = :NUT_SIZE and this.nut.color = :NUT_COLOR")
 * public class Bolt {...}
 *   
 * Additional criteria parameters are also 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.
 * 
 * Properties set on the entity manager will override those similarly named 
 * properties set on the entity manager factory.
 * 
 * Additional criteria is not supported with any native queries.
 * 
 * @author Guy Pelletier
 * @since EclipseLink 2.2
 */
@Target({TYPE})
@Retention(RUNTIME)
public @interface AdditionalCriteria {
    /**
     * (Required) The JPQL fragment to use as the additional criteria.
     */
    String value();
}

<additional-criteria>

An additional criteria complex element will be created to allow future expansion.

<xsd:complexType name="additional-criteria">
  <xsd:annotation>
    <xsd:documentation>
     /**
      * An additional criteria can be specified 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, at which point the additional criteria from the mapped superclass 
      * is ignored.
      * 
      * The additional criteria supports any valid JPQL string and must use 'this' 
      * as an alias to form your additional criteria. E.G.,
      * 
      * @Entity
      * @AdditionalCriteria("this.nut.size = :NUT_SIZE and this.nut.color = :NUT_COLOR")
      * public class Bolt {...}
      *   
      * Additional criteria parameters are also 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.
      * 
      * Properties set on the entity manager will override those similarly named 
      * properties set on the entity manager factory.
      * 
      * Additional criteria is not supported with any native queries.
      * 
      * @author Guy Pelletier
      * @since EclipseLink 2.2
      */
     @Target({TYPE})
     @Retention(RUNTIME)
     public @interface AdditionalCriteria {
         /**
          * (Required) The JPQL fragment to use as the additional criteria.
          */
         String value();
      }
 
    </xsd:documentation>
  </xsd:annotation>
  <xsd:sequence>
      <xsd:element name="criteria" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>

The additional-criteria element will be added to entity and mapped-superclass complex elements as follows

<xsd:complexType name="entity">
    <xsd:annotation>
      <xsd:documentation>
       ...
      </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      ...
      <xsd:element name="additional-criteria" type="orm:additional-criteria" minOccurs="0"/>
      ...
    </xsd:sequence>
    ...
</xsd:complexType>
 
<xsd:complexType name="mapped-superclass">
    <xsd:annotation>
      <xsd:documentation>
       ...
      </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      ...
      <xsd:element name="additional-criteria" type="orm:additional-criteria" minOccurs="0"/>
      ...
    </xsd:sequence>
    ...
</xsd:complexType>

Example

@AdditionalCriteria("this.address.city IS NOT NULL")

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

Metadata overriding and merging

The additional criteria can be overridden from the eclipselink-orm.xml. XML file merging will be available as well in its current form, simply expanded to include the additional criteria. The following will be the order of precedence used to determine the which additional-criteria is applied to the descriptor.

  1. eclipselink-orm.xml
  2. mapping file
  3. on the entity class
  4. on a mapped superclass (first one to define one)

Internal mapping

The additional criteria will be used to form the additionalJoinExpression from the descriptor's query manager.

Core

The following sections will detail the core changes and modifications that are required.

DescriptorQueryManager

The additional criteria will be used to form the existing additional join expression stored on the descriptor query manager. Care must be taken by those users who currently set the additional join expression directly through a customizer. Adding additional criteria will be AND'ed to this expression.

The metadata will set the additional criteria through the following new API on the DescriptorQueryManager.

public void setAdditionalCriteria(String jpqlFragment)

The jpql fragment will be parsed into the additional join expression at post-initialization time. Any parsing exceptions will be thrown at that time.

Other new API added to DescriptorQueryManager:

HashMap<String, Class> getAdditionalCriteriaArguments() // key = name and value = type
boolean hasAdditionalCriteria()
boolean hasAdditionalCriteriaArguments()

JPQL fragment parsing

We will tie into the existing JPQL parser to parse the additional criteria where the only alias allowed will be 'this'. We will prepend the following select statement with the additional criteria JPQL fragment and harness the selection criteria from that resulting query and set it to be the additional join expression.

Select this from <class> this where

The JPQL will be parsed at descriptor postInitialization time. Multiple selects will not be supported in the JPQL fragment.

Parameters

At runtime, users will provide parameters to their additional criteria during EntityManager create or through a set property call before any query execution. Changing the parameters during the lifespan of the EntityManager is not defined and could lead to a 'corrupted' cache and unpredictable results.

Parameters are global in scope and are added to the active JPA context (server session) and will be applied all additional criteria that accept the same parameter name at query execution time.

NOTE: Users must be careful when selecting their parameter names so as to not conflict with existing JPA and Eclipselink properties.

Query parameters and their types will be available from the DescriptorQueryManager after postInitialization through the following API:

HashMap<String, Class> getAdditionalCriteriaArguments()

These parameters are added to each executing query during its prepare. More specifically in the call to buildArgumentFields(). Following that, the parameter values are added to each executing query before its execution in the call to rowFromArguments(List, AbstractSession). The properties are extracted from the session given. If an additional criteria parameter value is not found, a new exception QueryException.argumentFromAdditionalCriteriaMissing is thrown. To ease the checking if a parameter is an additional criteria parameter, the following API had been added to DatabaseQuery:

boolean isAdditionalCriteriaArgument(String argument)

Beyond that, existing checking remains in place for an incorrect number of arguments vs. number of values provided, that is, QueryException.argumentSizeMismatchInQueryAndQueryDefinition

Example

With the given additional additional criteria

package model;
 
@AdditionalCriteria("this.company=:COMPANY")
public class Employee {
  ...
}

Setting the following property on the EntityManager would return all employees of Oracle.

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

A more complex example

@Entity
@AdditionalCriteria("this.nut.size = :NUT_SIZE and this.nut.color = :NUT_COLOR order by this.id")
public class Bolt {
...
}

This additional criteria accepts two parameters across a 1-1 relation and is order by id.

Exceptions/Open Issues/Future

  • Exception: Additional criteria specified on a descriptor within an inheritance hierarchy using views is not supported.
  • Future: Allow additional criteria to be specified at the mapping level.
  • Future: Allow multiple additional criteria with option to enable/disable individual additonal criteria

Back to the top