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