je/docs/GettingStartedGuide/persist_index.html

246 lines
10 KiB
HTML
Raw Permalink Normal View History

2021-06-06 17:46:45 +00:00
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Chapter 4. Working with Indices</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Getting Started with Berkeley DB Java Edition" />
<link rel="up" href="dpl.html" title="Part I. Programming with the Direct Persistence Layer" />
<link rel="prev" href="saveret.html" title="Saving and Retrieving Data" />
<link rel="next" href="dplindexcreate.html" title="Creating Indexes" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 12.2.7.5</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Chapter 4. Working with Indices</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="saveret.html">Prev</a> </td>
<th width="60%" align="center">Part I. Programming with the Direct Persistence Layer</th>
<td width="20%" align="right"> <a accesskey="n" href="dplindexcreate.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="chapter" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a id="persist_index"></a>Chapter 4. Working with Indices</h2>
</div>
</div>
</div>
<div class="toc">
<p>
<b>Table of Contents</b>
</p>
<dl>
<dt>
<span class="sect1">
<a href="persist_index.html#dplindexaccess">Accessing Indexes</a>
</span>
</dt>
<dd>
<dl>
<dt>
<span class="sect2">
<a href="persist_index.html#primaryindexaccess">Accessing Primary Indices</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="persist_index.html#secondaryindexaccess">Accessing Secondary Indices</a>
</span>
</dt>
</dl>
</dd>
<dt>
<span class="sect1">
<a href="dplindexcreate.html">Creating Indexes</a>
</span>
</dt>
<dd>
<dl>
<dt>
<span class="sect2">
<a href="dplindexcreate.html#dplprimaryidxdecl">Declaring Primary Indexes</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="dplindexcreate.html#dplsecondaryidxdecl">Declaring Secondary Indexes</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="dplindexcreate.html#foreignkey">Foreign Key Constraints</a>
</span>
</dt>
</dl>
</dd>
</dl>
</div>
<p>
All entity classes stored in JE using the DPL must have a
primary index, or key, identified for them. All such classes may
also have one or more secondary keys declared for them. This
chapter describes primary and secondary indexes in detail, and
shows how to access the indexes created for a given entity class.
</p>
<p>
One way to organize access to your primary and secondary
indexes is to create a <span class="emphasis"><em>data accessor</em></span>
class. We show an implementation of a data accessor class in
<a class="xref" href="simpleda.html" title="SimpleDA.class">SimpleDA.class</a>.
</p>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="dplindexaccess"></a>Accessing Indexes</h2>
</div>
</div>
</div>
<div class="toc">
<dl>
<dt>
<span class="sect2">
<a href="persist_index.html#primaryindexaccess">Accessing Primary Indices</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="persist_index.html#secondaryindexaccess">Accessing Secondary Indices</a>
</span>
</dt>
</dl>
</div>
<p>
In order to retrieve any object from an entity store, you
must access at least the primary index for that object.
Different entity classes stored in an entity store can have
different primary indexes, but all entity classes must have a
primary index declared for it. The primary index is just
the default index used for the class. (That is, it is the
data's primary <span class="emphasis"><em>key</em></span> for the underlying database.)
</p>
<p>
Entity classes can optionally have secondary indexes
declared for them. In order to access these secondary
indexes, you must first access the primary index.
</p>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="primaryindexaccess"></a>Accessing Primary Indices</h3>
</div>
</div>
</div>
<p>
You retrieve a primary index using the
<code class="methodname">EntityStore.getPrimaryIndex()</code>
method. To do this, you indicate the index key type
(that is, whether it is a String, Integer, and
so forth) and the class of the entities stored
in the index.
</p>
<p>
For example, the following retrieves the
primary index for an <code class="classname">Inventory</code>
class (we provide an implementation of this class in
<a class="xref" href="inventoryclass.html" title="Inventory.java">Inventory.java</a>).
These index keys are of type <code class="classname">String</code>.
</p>
<pre class="programlisting">PrimaryIndex&lt;String,Inventory&gt; inventoryBySku =
store.getPrimaryIndex(String.class, Inventory.class); </pre>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="secondaryindexaccess"></a>Accessing Secondary Indices</h3>
</div>
</div>
</div>
<p>
You retrieve a secondary index using the
<code class="methodname">EntityStore.getSecondaryIndex()</code>
method. Because secondary indices actually
refer to a primary index somewhere in your data
store, to access a secondary index you:
</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>
Provide the primary index as
returned by
<code class="methodname">EntityStore.getPrimaryIndex()</code>.
</p>
</li>
<li>
<p>
Identify the key data type used by
the secondary index
(<code class="classname">String</code>,
<code class="classname">Long</code>,
and so forth).
</p>
</li>
<li>
<p>
Identify the name of the
secondary key field.
When you declare the
<code class="classname">SecondaryIndex</code>
object, you identify the entity class
to which the secondary index
must refer.
</p>
</li>
</ol>
</div>
<p>
For example, the following first retrieves the
primary index, and then uses that to retrieve a secondary
index. The secondary key is held by the
<code class="literal">itemName</code> field of the
<code class="classname">Inventory</code> class.
</p>
<pre class="programlisting">PrimaryIndex&lt;String,Inventory&gt; inventoryBySku =
store.getPrimaryIndex(String.class, Inventory.class);
SecondaryIndex&lt;String,String,Inventory&gt; inventoryByName =
store.getSecondaryIndex(inventoryBySku, String.class, "itemName"); </pre>
</div>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="saveret.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="dpl.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="dplindexcreate.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Saving and Retrieving Data </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Creating Indexes</td>
</tr>
</table>
</div>
</body>
</html>