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