<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Chapter 6. A DPL Example</title> <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> <link rel="home" href="index.html" title="Getting Started with Berkeley DB" /> <link rel="up" href="dpl.html" title="Part I. Programming with the Direct Persistence Layer" /> <link rel="previous" href="dpl_replace.html" title="Replacing Entity Objects" /> <link rel="next" href="inventoryclass.html" title="Inventory.class" /> </head> <body> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr> <th colspan="3" align="center">Chapter 6. A DPL Example</th> </tr> <tr> <td width="20%" align="left"><a accesskey="p" href="dpl_replace.html">Prev</a> </td> <th width="60%" align="center">Part I. Programming with the Direct Persistence Layer</th> <td width="20%" align="right"> <a accesskey="n" href="inventoryclass.html">Next</a></td> </tr> </table> <hr /> </div> <div class="chapter" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h2 class="title"><a id="dpl_example"></a>Chapter 6. A DPL Example</h2> </div> </div> <div></div> </div> <div class="toc"> <p> <b>Table of Contents</b> </p> <dl> <dt> <span class="sect1"> <a href="dpl_example.html#vendorclass">Vendor.class</a> </span> </dt> <dt> <span class="sect1"> <a href="inventoryclass.html">Inventory.class</a> </span> </dt> <dt> <span class="sect1"> <a href="mydbenv-persist.html">MyDbEnv</a> </span> </dt> <dt> <span class="sect1"> <a href="dataaccessorclass.html">DataAccessor.class</a> </span> </dt> <dt> <span class="sect1"> <a href="dpl_exampledatabaseput.html">ExampleDatabasePut.class</a> </span> </dt> <dt> <span class="sect1"> <a href="dpl_exampleinventoryread.html">ExampleInventoryRead.class</a> </span> </dt> </dl> </div> <p> In order to illustrate DPL usage, we provide a complete working example in this chapter. This example reads and writes inventory and vendor information for a mythical business. The application consists of the following classes: </p> <div class="itemizedlist"> <ul type="disc"> <li> <p> Several classes used to encapsulate our application's data. See <a href="dpl_example.html#vendorclass">Vendor.class</a> and <a href="inventoryclass.html">Inventory.class</a>. </p> </li> <li> <p> A convenience class used to open and close our environment and entity store. See <a href="mydbenv-persist.html">MyDbEnv</a>. </p> </li> <li> <p> A class that loads data into the store. See <a href="dpl_exampledatabaseput.html">ExampleDatabasePut.class</a>. </p> </li> <li> <p> Finally, a class that reads data from the store. See <a href="dpl_exampleinventoryread.html">ExampleInventoryRead.class</a>. </p> </li> </ul> </div> <div class="sect1" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h2 class="title" style="clear: both"><a id="vendorclass"></a>Vendor.class</h2> </div> </div> <div></div> </div> <p> The simplest class that our example wants to store contains vendor contact information. This class contains no secondary indices so all we have to do is identify it as an entity class and identify the field in the class used for the primary key. </p> <p> In the following example, we identify the <tt class="literal">vendor</tt> data member as containing the primary key. This data member is meant to contain a vendor's name. Because of the way we will use our <tt class="classname">EntityStore</tt>, the value provided for this data member must be unique within the store or runtime errors will result. </p> <p> When used with the DPL, our <tt class="classname">Vendor</tt> class appears as follows. Notice that the <tt class="literal">@Entity</tt> annotation appears immediately before the class declaration, and the <tt class="literal">@PrimaryKey</tt> annotation appears immediately before the <tt class="literal">vendor</tt> data member declaration. </p> <pre class="programlisting">package persist.gettingStarted; import com.sleepycat.persist.model.Entity; import com.sleepycat.persist.model.PrimaryKey; @Entity public class Vendor { private String address; private String bizPhoneNumber; private String city; private String repName; private String repPhoneNumber; private String state; // Primary key is the vendor's name // This assumes that the vendor's name is // unique in the database. @PrimaryKey private String vendor; private String zipcode; public void setRepName(String data) { repName = data; } public void setAddress(String data) { address = data; } public void setCity(String data) { city = data; } public void setState(String data) { state = data; } public void setZipcode(String data) { zipcode = data; } public void setBusinessPhoneNumber(String data) { bizPhoneNumber = data; } public void setRepPhoneNumber(String data) { repPhoneNumber = data; } public void setVendorName(String data) { vendor = data; } public String getRepName() { return repName; } public String getAddress() { return address; } public String getCity() { return city; } public String getState() { return state; } public String getZipcode() { return zipcode; } public String getBusinessPhoneNumber() { return bizPhoneNumber; } public String getRepPhoneNumber() { return repPhoneNumber; } } </pre> <p> For this class, the <tt class="literal">vendor</tt> value is set for an individual <tt class="classname">Vendor</tt> class object by the <tt class="methodname">setVendorName()</tt> method. If our example code fails to set this value before storing the object, the data member used to store the primary key is set to a null value. This would result in a runtime error. </p> </div> </div> <div class="navfooter"> <hr /> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"><a accesskey="p" href="dpl_replace.html">Prev</a> </td> <td width="20%" align="center"> <a accesskey="u" href="dpl.html">Up</a> </td> <td width="40%" align="right"> <a accesskey="n" href="inventoryclass.html">Next</a></td> </tr> <tr> <td width="40%" align="left" valign="top">Replacing Entity Objects </td> <td width="20%" align="center"> <a accesskey="h" href="index.html">Home</a> </td> <td width="40%" align="right" valign="top"> Inventory.class</td> </tr> </table> </div> </body> </html>