Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Caching/Indexes"

(Cache index query example)
(Cache index query example)
Line 81: Line 81:
|previous= [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/DatabaseEvents|Database Event Notification and Oracle DCN/QCN]]
|previous= [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/DatabaseEvents|Database Event Notification and Oracle DCN/QCN]]
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Query Results Cache|Query Results Cache]]
|next=[[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Query Cache|Query Results Cache]]
|version=2.4 DRAFT}}
|version=2.4 DRAFT}}

Revision as of 15:26, 28 May 2012

EclipseLink JPA

Mailing ListForumsIRC
Help Wanted
Bug Day
Browse Source

Elug api package icon.png Native API

Cache Indexes

The EclipseLink cache is indexed by the entities Id. This allows the find() operation, relationships, and queries by Id to obtain cache hits and avoid database access. The cache is not used by default for any non-Id query. All non-Id queries will access the database then resolve with the cache for each row returned in the result-set.

Applications tend to have other unique keys in their model in addition to their Id. This is quite common when a generated Id is used. The application frequently queries on these unique keys, and it is desirable to be able to obtain cache hits to avoid database access on these queries.

Cache indexes allow an in-memory index to be created in the EclipseLink cache to allow cache hits on non-Id fields. The cache index can be on a single field, or on a set of fields. The indexed fields can be updateable, and although they should be unique, this is not a requirement. Queries that contain the indexed fields will be able to obtain cache hits. Only single results can be obtained from indexed queries.

Cache indexes can be configured using the @CacheIndex and @CacheIndexes annotations and <cache-index> XML element. A @CacheIndex can be defined on the entity, or on an attribute to index the attribute. Indexes defined on the entity must define the columnNames used for the index. An index can be configured to be re-indexed when the object is updated using the updateable attribute.

It is still possible to cache query results for non-indexed queries using the query result cache, (see Query Results Cache).

Cache index annotation example
@CacheIndex(columnNames={"F_NAME", "L_NAME"}, updateable=true)
public class Employee {
  private long id;
  private String ssn;
  private String firstName;
  private String lastName;
Cache index XML example
<?xml version="1.0"?>
	xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_4.xsd"
    <entity name="Employee" class="org.acme.Employee" access="FIELD">
        <cache-index updateable="true">
            <id name="id"/>
            <basic name="ssn">
            <basic name="firstName">
                <column name="F_NAME"/>
            <basic name="lastName">
                <column name="L_NAME"/>
Cache index query example
Query query = em.createQuery("Select e from Employee e where e.firstName = :firstName and e.lastName = :lastName");
query.setParameter("firstName", "Bob");
query.setParameter("lastName", "Smith");
Employee employee = (Employee)query.getSingleResult();

Version: 2.4 DRAFT
Other versions...