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
m
Line 11: Line 11:
 
}}
 
}}
  
=Defining Additional Criteria for Queries=
+
=@AdditionalCriteria=
  
 +
Use <tt>@AdditionalCriteria</tt> to define parameterized views on data. You can define additional criteria on entities or mapped superclasses.
  
You can define additional criteria for queries defined for entities or mapped superclasses. 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.
+
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.  
  
 
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.
  
Set additional criteria parameters through properties on the entity manager factory or on the entity manager. When set on the entity manager, you must set the properties before executing the query. They 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 through properties on the entity manager factory or on the entity manager. When set on the entity manager, you must set the properties before executing a query. Do not change the properties 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 ative queries.
  
 
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,
 
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,
Line 57: Line 58:
 
</additional-criteria&>
 
</additional-criteria&>
 
</source>
 
</source>
 +
 +
==Uses of AdditionalCriteria==
 +
 +
Use AdditionalCriteria is to define parameterized views on data.
 +
 +
Uses include the following:
 +
*[[#Multitenancy|Multitenancy]]
 +
*[[#Soft Deletes|Soft Deletes]]
 +
*[[#Multitenancy|Multitenancy]]
 +
 +
===Multitenancy===
 +
 +
In a multitenancy environment, tenants (users, clients, organizations, applications) can share database tables, but the views on the data must be restricted so that tenants have access only to their own data. You can use <tt>AdditionalCriteria</tt> to thus restrict views on the data.  For example,
 +
 +
* Use <tt>@AdditionalCriteria("TENANT = 'Billing'")</tt> to restrict the data for a “Billing” client, such as a billing application or billing organization.
 +
 +
* You could use <tt>@AdditionalCriteria("this.tenant = :tenant")</tt> for an application that can be used by multiple different tenants at the same time. When the tenant acquires its <tt>EntityManagerFactory</tt> or <tt>EntityManager</tt> the persistence/entity manager property <tt>tenant</tt> is set to the name of the tenant acquring it.
 +
 +
          Map properties = new HashMap();
 +
          properties.put("tenant", "ACME");
 +
          EntityManagerFactory emf = Persistence.createEntityManagerFactory(properties);
 +
 +
or,
 +
          Map properties = new HashMap();
 +
          properties.put("tenant", "ACME");
 +
          EntityManager em = factory.createEntityManager(properties);
 +
 +
 +
===Soft Deletes===
 +
 +
A soft delete is when a user marks data as deleted but does not actually delete it from the database, for example to keep a record.
 +
 +
For example,
 +
 +
<tt>@AdditionalCriteria("this.isDeleted = false")</tt> filters any delete data from any query.
 +
 +
===History===
 +
History is when data is never removed or updated. On an update, a new row is inserted with a new start/end time instead.
 +
 +
<!--We have specific history support, but if the user just wants something simpler or more generic they can use criteria. -->
 +
 +
<tt>@AdditionalCriteria("this.endDate is null")</tt> returns the current data from any query.
 +
 +
Or, you could create an <tt>EntityManager</tt> on a specific date, for example:
 +
 +
<tt>@AdditionalCriteria("this.startDate <= :viewDate and this.endDate >= :viewDate")</tt>
 +
 +
===Shared Tables===
 +
For a shared table, you could have inheritance in the table, but not in the object model.
 +
i.e. you have an SavingsAccount class and an ACCOUNT table but the ACCOUNT table also has checking account data that you want filtered.
  
  

Revision as of 12:50, 18 May 2011

EclipseLink JPA


@AdditionalCriteria

Use @AdditionalCriteria to define parameterized views on data. You can define additional criteria on entities or mapped superclasses.

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.

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 through properties on the entity manager factory or on the entity manager. When set on the entity manager, you must set the properties before executing a query. Do not change the properties 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 ative 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 example shows the <additional-criteria> element used in the eclipselink-orm.xml descriptor:

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

Uses of AdditionalCriteria

Use AdditionalCriteria is to define parameterized views on data.

Uses include the following:

Multitenancy

In a multitenancy environment, tenants (users, clients, organizations, applications) can share database tables, but the views on the data must be restricted so that tenants have access only to their own data. You can use AdditionalCriteria to thus restrict views on the data. For example,

  • Use @AdditionalCriteria("TENANT = 'Billing'") to restrict the data for a “Billing” client, such as a billing application or billing organization.
  • You could use @AdditionalCriteria("this.tenant = :tenant") for an application that can be used by multiple different tenants at the same time. When the tenant acquires its EntityManagerFactory or EntityManager the persistence/entity manager property tenant is set to the name of the tenant acquring it.
         Map properties = new HashMap();
         properties.put("tenant", "ACME");
         EntityManagerFactory emf = Persistence.createEntityManagerFactory(properties);

or,

         Map properties = new HashMap();
         properties.put("tenant", "ACME");
         EntityManager em = factory.createEntityManager(properties);


Soft Deletes

A soft delete is when a user marks data as deleted but does not actually delete it from the database, for example to keep a record.

For example,

@AdditionalCriteria("this.isDeleted = false") filters any delete data from any query.

History

History is when data is never removed or updated. On an update, a new row is inserted with a new start/end time instead.


@AdditionalCriteria("this.endDate is null") returns the current data from any query.

Or, you could create an EntityManager on a specific date, for example:

@AdditionalCriteria("this.startDate <= :viewDate and this.endDate >= :viewDate")

Shared Tables

For a shared table, you could have inheritance in the table, but not in the object model. i.e. you have an SavingsAccount class and an ACCOUNT table but the ACCOUNT table also has checking account data that you want filtered.


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

Back to the top