1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml">
4  <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6    <title>DataAccessor.class</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
9    <link rel="home" href="index.html" title="Getting Started with Berkeley DB" />
10    <link rel="up" href="dpl_example.html" title="Chapter 6. A DPL Example" />
11    <link rel="previous" href="mydbenv-persist.html" title="MyDbEnv" />
12    <link rel="next" href="dpl_exampledatabaseput.html" title="ExampleDatabasePut.class" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">DataAccessor.class</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="mydbenv-persist.html">Prev</a> </td>
22          <th width="60%" align="center">Chapter 6. A DPL Example</th>
23          <td width="20%" align="right"> <a accesskey="n" href="dpl_exampledatabaseput.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="dataaccessorclass"></a>DataAccessor.class</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <p>
38                            Now that we have implemented our data classes,
39                            we can write a class that will provide
40                            convenient access to our primary and
41                            secondary indexes.
42                            Note that like our data classes, this class is shared by both our
43                            example programs.
44                    </p>
45      <p>
46                        If you compare this class against our
47                        <tt class="classname">Vendor</tt> and 
48                        <tt class="classname">Inventory</tt>
49                        class implementations, you will see that the
50                        primary and secondary indices declared there are
51                        referenced by this class.
52                    </p>
53      <p>
54                            See <a href="dpl_example.html#vendorclass">Vendor.class</a>
55                        and 
56                        <a href="inventoryclass.html">Inventory.class</a>
57                        for those implementations.
58                    </p>
59      <pre class="programlisting">package persist.gettingStarted;
60
61import java.io.File;
62
63import com.sleepycat.db.DatabaseException;
64import com.sleepycat.persist.EntityStore;
65import com.sleepycat.persist.PrimaryIndex; 
66import com.sleepycat.persist.SecondaryIndex;
67                            
68public class DataAccessor {
69    // Open the indices
70    public DataAccessor(EntityStore store)
71        throws DatabaseException {
72
73        // Primary key for Inventory classes
74        inventoryBySku = store.getPrimaryIndex(
75            String.class, Inventory.class);
76
77        // Secondary key for Inventory classes
78        // Last field in the getSecondaryIndex() method must be
79        // the name of a class member; in this case, an Inventory.class
80        // data member.
81        inventoryByName = store.getSecondaryIndex(
82            inventoryBySku, String.class, "itemName");
83
84        // Primary key for Vendor class
85        vendorByName = store.getPrimaryIndex(
86            String.class, Vendor.class);
87    }
88
89    // Inventory Accessors
90    PrimaryIndex&lt;String,Inventory&gt; inventoryBySku;
91    SecondaryIndex&lt;String,String,Inventory&gt; inventoryByName;
92
93    // Vendor Accessors
94    PrimaryIndex&lt;String,Vendor&gt; vendorByName;
95} </pre>
96    </div>
97    <div class="navfooter">
98      <hr />
99      <table width="100%" summary="Navigation footer">
100        <tr>
101          <td width="40%" align="left"><a accesskey="p" href="mydbenv-persist.html">Prev</a> </td>
102          <td width="20%" align="center">
103            <a accesskey="u" href="dpl_example.html">Up</a>
104          </td>
105          <td width="40%" align="right"> <a accesskey="n" href="dpl_exampledatabaseput.html">Next</a></td>
106        </tr>
107        <tr>
108          <td width="40%" align="left" valign="top">MyDbEnv </td>
109          <td width="20%" align="center">
110            <a accesskey="h" href="index.html">Home</a>
111          </td>
112          <td width="40%" align="right" valign="top"ExampleDatabasePut.class</td>
113        </tr>
114      </table>
115    </div>
116  </body>
117</html>
118