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��6.��A DPL Example</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="dpl_replace.html" title="Replacing Entity Objects" /> 12 <link rel="next" href="inventoryclass.html" title="Inventory.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��6.��A DPL Example</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dpl_replace.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="inventoryclass.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="dpl_example"></a>Chapter��6.��A DPL Example</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="dpl_example.html#vendorclass">Vendor.class</a> 45 </span> 46 </dt> 47 <dt> 48 <span class="sect1"> 49 <a href="inventoryclass.html">Inventory.class</a> 50 </span> 51 </dt> 52 <dt> 53 <span class="sect1"> 54 <a href="mydbenv-persist.html">MyDbEnv</a> 55 </span> 56 </dt> 57 <dt> 58 <span class="sect1"> 59 <a href="dataaccessorclass.html">DataAccessor.class</a> 60 </span> 61 </dt> 62 <dt> 63 <span class="sect1"> 64 <a href="dpl_exampledatabaseput.html">ExampleDatabasePut.class</a> 65 </span> 66 </dt> 67 <dt> 68 <span class="sect1"> 69 <a href="dpl_exampleinventoryread.html">ExampleInventoryRead.class</a> 70 </span> 71 </dt> 72 </dl> 73 </div> 74 <p> 75 In order to illustrate DPL usage, we provide a 76 complete working example in this chapter. This example 77 reads and writes inventory and vendor information for a 78 mythical business. The application consists of the 79 following classes: 80 </p> 81 <div class="itemizedlist"> 82 <ul type="disc"> 83 <li> 84 <p> 85 Several classes used to encapsulate our 86 application's data. See 87 <a href="dpl_example.html#vendorclass">Vendor.class</a> 88 and 89 <a href="inventoryclass.html">Inventory.class</a>. 90 </p> 91 </li> 92 <li> 93 <p> 94 A convenience class used to open and close 95 our environment and entity store. See 96 <a href="mydbenv-persist.html">MyDbEnv</a>. 97 </p> 98 </li> 99 <li> 100 <p> 101 A class that loads data into the store. See 102 <a href="dpl_exampledatabaseput.html">ExampleDatabasePut.class</a>. 103 </p> 104 </li> 105 <li> 106 <p> 107 Finally, a class that reads data from the 108 store. See 109 <a href="dpl_exampleinventoryread.html">ExampleInventoryRead.class</a>. 110 </p> 111 </li> 112 </ul> 113 </div> 114 <div class="sect1" lang="en" xml:lang="en"> 115 <div class="titlepage"> 116 <div> 117 <div> 118 <h2 class="title" style="clear: both"><a id="vendorclass"></a>Vendor.class</h2> 119 </div> 120 </div> 121 <div></div> 122 </div> 123 <p> 124 The simplest class that our example wants to store contains 125 vendor contact information. This class contains no 126 secondary indices so all we have to do is identify it 127 as an entity class and identify the field in the 128 class used for the primary key. 129 </p> 130 <p> 131 In the following example, we identify the 132 <tt class="literal">vendor</tt> data member as containing the 133 primary key. This data member is meant to contain a 134 vendor's name. Because of the way we will use our 135 <tt class="classname">EntityStore</tt>, the value 136 provided for this data member must be unique within 137 the store or runtime errors will result. 138 </p> 139 <p> 140 When used with the DPL, our 141 <tt class="classname">Vendor</tt> class appears as 142 follows. Notice that the <tt class="literal">@Entity</tt> 143 annotation appears immediately before the class 144 declaration, and the <tt class="literal">@PrimaryKey</tt> 145 annotation appears immediately before the 146 <tt class="literal">vendor</tt> data member declaration. 147 </p> 148 <pre class="programlisting">package persist.gettingStarted; 149 150import com.sleepycat.persist.model.Entity; 151import com.sleepycat.persist.model.PrimaryKey; 152 153@Entity 154public class Vendor { 155 156 private String address; 157 private String bizPhoneNumber; 158 private String city; 159 private String repName; 160 private String repPhoneNumber; 161 private String state; 162 163 // Primary key is the vendor's name 164 // This assumes that the vendor's name is 165 // unique in the database. 166 @PrimaryKey 167 private String vendor; 168 169 private String zipcode; 170 171 public void setRepName(String data) { 172 repName = data; 173 } 174 175 public void setAddress(String data) { 176 address = data; 177 } 178 179 public void setCity(String data) { 180 city = data; 181 } 182 183 public void setState(String data) { 184 state = data; 185 } 186 187 public void setZipcode(String data) { 188 zipcode = data; 189 } 190 191 public void setBusinessPhoneNumber(String data) { 192 bizPhoneNumber = data; 193 } 194 195 public void setRepPhoneNumber(String data) { 196 repPhoneNumber = data; 197 } 198 199 public void setVendorName(String data) { 200 vendor = data; 201 } 202 203 public String getRepName() { 204 return repName; 205 } 206 207 public String getAddress() { 208 return address; 209 } 210 211 public String getCity() { 212 return city; 213 } 214 215 public String getState() { 216 return state; 217 } 218 219 public String getZipcode() { 220 return zipcode; 221 } 222 223 public String getBusinessPhoneNumber() { 224 return bizPhoneNumber; 225 } 226 227 public String getRepPhoneNumber() { 228 return repPhoneNumber; 229 } 230} </pre> 231 <p> 232 For this class, the <tt class="literal">vendor</tt> value is set for an individual 233 <tt class="classname">Vendor</tt> class object by 234 the <tt class="methodname">setVendorName()</tt> 235 method. If our example code fails to set this 236 value before storing the object, the data 237 member used to store the primary key is set to a 238 null value. This would result in a runtime 239 error. 240 </p> 241 </div> 242 </div> 243 <div class="navfooter"> 244 <hr /> 245 <table width="100%" summary="Navigation footer"> 246 <tr> 247 <td width="40%" align="left"><a accesskey="p" href="dpl_replace.html">Prev</a>��</td> 248 <td width="20%" align="center"> 249 <a accesskey="u" href="dpl.html">Up</a> 250 </td> 251 <td width="40%" align="right">��<a accesskey="n" href="inventoryclass.html">Next</a></td> 252 </tr> 253 <tr> 254 <td width="40%" align="left" valign="top">Replacing Entity Objects��</td> 255 <td width="20%" align="center"> 256 <a accesskey="h" href="index.html">Home</a> 257 </td> 258 <td width="40%" align="right" valign="top">��Inventory.class</td> 259 </tr> 260 </table> 261 </div> 262 </body> 263</html> 264