je/docs/GettingStartedGuide/dataaccessorclass.html
2021-06-06 13:46:45 -04:00

119 lines
4.8 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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>DataAccessor.java</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_example.html" title="Chapter 6. A DPL Example" />
<link rel="prev" href="mydbenv-persist.html" title="MyDbEnv" />
<link rel="next" href="dpl_exampledatabaseput.html" title="ExampleDatabasePut.java" />
</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">DataAccessor.java</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="mydbenv-persist.html">Prev</a> </td>
<th width="60%" align="center">Chapter 6. A DPL Example</th>
<td width="20%" align="right"> <a accesskey="n" href="dpl_exampledatabaseput.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="dataaccessorclass"></a>DataAccessor.java</h2>
</div>
</div>
</div>
<p>
Now that we have implemented our data classes,
we can write a class that will provide
convenient access to our primary and
secondary indexes.
Note that like our data classes, this class is shared by both our
example programs.
</p>
<p>
If you compare this class against our
<code class="classname">Vendor</code> and
<code class="classname">Inventory</code>
class implementations, you will see that the
primary and secondary indices declared there are
referenced by this class.
</p>
<p>
See <a class="xref" href="dpl_example.html#vendorclass" title="Vendor.java">Vendor.java</a>
and
<a class="xref" href="inventoryclass.html" title="Inventory.java">Inventory.java</a>
for those implementations.
</p>
<pre class="programlisting">package persist.gettingStarted;
import java.io.File;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
public class DataAccessor {
// Open the indices
public DataAccessor(EntityStore store)
throws DatabaseException {
// Primary key for Inventory classes
inventoryBySku = store.getPrimaryIndex(
String.class, Inventory.class);
// Secondary key for Inventory classes
// Last field in the getSecondaryIndex() method must be
// the name of a class member; in this case, an Inventory.class
// data member.
inventoryByName = store.getSecondaryIndex(
inventoryBySku, String.class, "itemName");
// Primary key for Vendor class
vendorByName = store.getPrimaryIndex(
String.class, Vendor.class);
}
// Inventory Accessors
PrimaryIndex&lt;String,Inventory&gt; inventoryBySku;
SecondaryIndex&lt;String,String,Inventory&gt; inventoryByName;
// Vendor Accessors
PrimaryIndex&lt;String,Vendor&gt; vendorByName;
} </pre>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="mydbenv-persist.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="dpl_example.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="dpl_exampledatabaseput.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">MyDbEnv </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> ExampleDatabasePut.java</td>
</tr>
</table>
</div>
</body>
</html>