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>Chapter 5. Saving and Retrieving Objects</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9    <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
10    <link rel="up" href="dpl.html" title="Part I. Programming with the Direct Persistence Layer" />
11    <link rel="prev" href="dplindexcreate.html" title="Creating Indexes" />
12    <link rel="next" href="simpleda.html" title="SimpleDA.class" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Chapter 5. Saving and Retrieving Objects</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="dplindexcreate.html">Prev</a> </td>
22          <th width="60%" align="center">Part I. Programming with the Direct Persistence Layer</th>
23          <td width="20%" align="right"> <a accesskey="n" href="simpleda.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="chapter" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title"><a id="persist_access"></a>Chapter 5. Saving and Retrieving Objects</h2>
33          </div>
34        </div>
35      </div>
36      <div class="toc">
37        <p>
38          <b>Table of Contents</b>
39        </p>
40        <dl>
41          <dt>
42            <span class="sect1">
43              <a href="persist_access.html#simpleentity">A Simple Entity Class</a>
44            </span>
45          </dt>
46          <dt>
47            <span class="sect1">
48              <a href="simpleda.html">SimpleDA.class</a>
49            </span>
50          </dt>
51          <dt>
52            <span class="sect1">
53              <a href="simpleput.html">Placing Objects in an Entity Store</a>
54            </span>
55          </dt>
56          <dt>
57            <span class="sect1">
58              <a href="simpleget.html">Retrieving Objects from an Entity Store</a>
59            </span>
60          </dt>
61          <dt>
62            <span class="sect1">
63              <a href="getmultiple.html">Retrieving Multiple Objects</a>
64            </span>
65          </dt>
66          <dd>
67            <dl>
68              <dt>
69                <span class="sect2">
70                  <a href="getmultiple.html#dpl_cursor_initialize">Cursor Initialization</a>
71                </span>
72              </dt>
73              <dt>
74                <span class="sect2">
75                  <a href="getmultiple.html#dpl_dups">Working with Duplicate Keys</a>
76                </span>
77              </dt>
78              <dt>
79                <span class="sect2">
80                  <a href="getmultiple.html#dpl_cursor_range">Key Ranges</a>
81                </span>
82              </dt>
83            </dl>
84          </dd>
85          <dt>
86            <span class="sect1">
87              <a href="dpl_entityjoin.html">Join Cursors</a>
88            </span>
89          </dt>
90          <dt>
91            <span class="sect1">
92              <a href="dpl_delete.html">Deleting Entity Objects</a>
93            </span>
94          </dt>
95          <dt>
96            <span class="sect1">
97              <a href="dpl_replace.html">Replacing Entity Objects</a>
98            </span>
99          </dt>
100        </dl>
101      </div>
102      <p>
103          To store an object in an <code class="classname">EntityStore</code> you
104          must annotate the class appropriately and then store it using
105          <code class="methodname">PrimaryIndex.put()</code>.
106  </p>
107      <p>
108          To retrieve and object from an <code class="classname">EntityStore</code>
109          you use the <code class="methodname">get()</code> method from either the
110          <code class="classname">PrimaryIndex</code> or
111          <code class="classname">SecondaryIndex</code>, whichever is most
112          appropriate for your application.
113  </p>
114      <p>
115          In both cases, it simplifies things greatly if you create a data
116          accessor class to organize your indexes.
117  </p>
118      <p>
119          In the next few sections we:
120  </p>
121      <div class="orderedlist">
122        <ol type="1">
123          <li>
124            <p>
125                          Create an entity class that is ready to be stored
126                          in an entity store. This class will have both a
127                          primary index (required) declared for it, as well
128                          as a secondary index (which is optional).
129                  </p>
130            <p>
131                          See the next section for this implementation.
132                  </p>
133          </li>
134          <li>
135            <p>
136                          Create a data accessor class which is used to
137                          organize our data.
138                  </p>
139            <p>
140                          See <a class="xref" href="simpleda.html" title="SimpleDA.class">SimpleDA.class</a>
141                          for this implementation.
142                  </p>
143          </li>
144          <li>
145            <p>
146                          Create a simple class that is used to put objects
147                          to our entity store.
148                  </p>
149            <p>
150                          See <a class="xref" href="simpleput.html" title="Placing Objects in an Entity Store">Placing Objects in an Entity Store</a>
151                          for this implementation.
152                  </p>
153          </li>
154          <li>
155            <p>
156                          Create another class that retrieves objects from
157                          our entity store.
158                  </p>
159            <p>
160                          See <a class="xref" href="simpleget.html" title="Retrieving Objects from an Entity Store">Retrieving Objects from an Entity Store</a>
161                          for this implementation.
162                  </p>
163          </li>
164        </ol>
165      </div>
166      <div class="sect1" lang="en" xml:lang="en">
167        <div class="titlepage">
168          <div>
169            <div>
170              <h2 class="title" style="clear: both"><a id="simpleentity"></a>A Simple Entity Class</h2>
171            </div>
172          </div>
173        </div>
174        <p>
175                  For clarity's sake, this entity class is a simple a class as we can write.
176                  It contains only two data members, both of which are set
177                  and retrieved by simple setter and getter methods. Beyond
178                  that, by design this class does not do anything or particular
179                  interest.
180          </p>
181        <p>
182                  Its implementation is as follows:
183          </p>
184        <pre class="programlisting">package persist.gettingStarted;
185
186import com.sleepycat.persist.model.Entity;
187import com.sleepycat.persist.model.PrimaryKey;
188import static com.sleepycat.persist.model.Relationship.*;
189import com.sleepycat.persist.model.SecondaryKey;
190
191@Entity
192public class SimpleEntityClass {
193
194    // Primary key is pKey
195    @PrimaryKey
196    private String pKey;
197
198    // Secondary key is the sKey
199    @SecondaryKey(relate=MANY_TO_ONE)
200    private String sKey;
201
202    public void setPKey(String data) {
203        pKey = data;
204    }
205
206    public void setSKey(String data) {
207        sKey = data;
208    }
209
210    public String getPKey() {
211        return pKey;
212    }
213
214    public String getSKey() {
215        return sKey;
216    }
217} </pre>
218      </div>
219    </div>
220    <div class="navfooter">
221      <hr />
222      <table width="100%" summary="Navigation footer">
223        <tr>
224          <td width="40%" align="left"><a accesskey="p" href="dplindexcreate.html">Prev</a> </td>
225          <td width="20%" align="center">
226            <a accesskey="u" href="dpl.html">Up</a>
227          </td>
228          <td width="40%" align="right"> <a accesskey="n" href="simpleda.html">Next</a></td>
229        </tr>
230        <tr>
231          <td width="40%" align="left" valign="top">Creating Indexes </td>
232          <td width="20%" align="center">
233            <a accesskey="h" href="index.html">Home</a>
234          </td>
235          <td width="40%" align="right" valign="top"SimpleDA.class</td>
236        </tr>
237      </table>
238    </div>
239  </body>
240</html>
241