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/Caching/Indexes"

(New page: {{EclipseLink_UserGuide |info=y |toc=n |eclipselink=y |eclipselinktype=JPA |api=y |apis= *[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/CacheIndex.html...)
 
Line 7: Line 7:
 
|apis=
 
|apis=
 
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/CacheIndex.html @CacheIndex]
 
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/CacheIndex.html @CacheIndex]
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/CacheIndexs.html @CacheIndexs]
+
*[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/CacheIndexs.html @CacheIndexes]
 
|nativeapi=y
 
|nativeapi=y
 
|nativeapis=
 
|nativeapis=
Line 15: Line 15:
 
}}
 
}}
 
=Cache Indexes=
 
=Cache Indexes=
 +
The EclipseLink cache is indexed by the entities Id.  This allows the <tt>find()</tt> 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.
  
 +
Application tend to have other unique keys in there model in addition to there 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.
  
Expiry can also be used in the query results cache (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Caching/Query Results Cache|Query Results Cache]]).
+
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 <tt>@CacheIndex</tt> and <tt>@CacheIndexes</tt> annotations and <tt><cache-index></tt> XML element.  A <tt>@CacheIndex</tt> can be defined on the entity, or on an attribute to index the attribute.  Indexes defined on the entity must define the <tt>columnNames</tt> used for the index.  An index can be configured to be re-indexed when the object is updated using the <tt>updateable</tt> attribute.
 +
 
 +
It is still possible to cache query results for non-indexed queries using the query result cache, (see [[EclipseLink/UserGuide/JPA/Basic JPA Development/Caching/Query Results Cache|Query Results Cache]]).
  
 
======''Cache index annotation example''======
 
======''Cache index annotation example''======
Line 23: Line 29:
 
...
 
...
 
@Entity
 
@Entity
@Cache(
+
@CacheIndex(columnNames={"F_NAME", "L_NAME", updateable=true)
  expiry=600000 // 10 minutes
+
)
+
 
public class Employee {
 
public class Employee {
   ...
+
   @Id
}
+
  private long id;
</source>
+
  @CacheIndex
 
+
  private String ssn;
<source lang="java">
+
  @Column(name="F_NAME")
...
+
  private String firstName;
@Entity
+
   @Column(name="L_NAME")
@Cache(
+
   private String lastName;
   expiryTimeOfDay=@TimeOfDay(hour=3) // 3:00 AM
+
)
+
public class Project {
+
   ...
+
 
}
 
}
 
</source>
 
</source>
Line 51: Line 51:
 
version="2.4">
 
version="2.4">
 
     <entity name="Employee" class="org.acme.Employee" access="FIELD">
 
     <entity name="Employee" class="org.acme.Employee" access="FIELD">
         <cache>
+
         <cache-index updateable="true">
             <expiry>600000</expiry>
+
             <column-name>F_NAME</column-name>
         </cache>
+
            <column-name>L_NAME</column-name>
    </entity>
+
         </cache-index>
    <entity name="Project" class="org.acme.Project" access="FIELD">
+
        <attributes>
        <cache>
+
            <id name="id"/>
             <expiry-time-of-day hour="3"/>
+
            <basic name="ssn">
         </cache>
+
                <cache-index/>
 +
            </basic>
 +
            <basic name="firstName">
 +
                <column name="F_NAME"/>
 +
            </basic>
 +
             <basic name="lastName">
 +
                <column name="L_NAME"/>
 +
            </basic>
 +
         </attributes>
 
     </entity>
 
     </entity>
 
</entity-mappings>
 
</entity-mappings>

Revision as of 10:37, 24 May 2012

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
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.

Application tend to have other unique keys in there model in addition to there 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
...
@Entity
@CacheIndex(columnNames={"F_NAME", "L_NAME", updateable=true)
public class Employee {
  @Id
  private long id;
  @CacheIndex
  private String ssn;
  @Column(name="F_NAME")
  private String firstName;
  @Column(name="L_NAME")
  private String lastName;
}
Cache index XML example
<?xml version="1.0"?>
<entity-mappings
	xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_4.xsd"
	version="2.4">
    <entity name="Employee" class="org.acme.Employee" access="FIELD">
        <cache-index updateable="true">
            <column-name>F_NAME</column-name>
            <column-name>L_NAME</column-name>
        </cache-index>
        <attributes>
            <id name="id"/>
            <basic name="ssn">
                <cache-index/>
            </basic>
            <basic name="firstName">
                <column name="F_NAME"/>
            </basic>
            <basic name="lastName">
                <column name="L_NAME"/>
            </basic>
        </attributes>
    </entity>
</entity-mappings>

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

Back to the top