mirror of
https://github.com/berkeleydb/je.git
synced 2024-11-15 01:46:24 +00:00
275 lines
9.7 KiB
HTML
275 lines
9.7 KiB
HTML
<?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.73.2" />
|
||
<link rel="start" href="index.html" title="Getting Started with Berkeley DB Java Edition" />
|
||
<link rel="up" href="dpl.html" title="Part I. Programming with the Direct Persistence Layer" />
|
||
<link rel="prev" href="dpl_replace.html" title="Replacing Entity Objects" />
|
||
<link rel="next" href="inventoryclass.html" title="Inventory.java" />
|
||
</head>
|
||
<body>
|
||
<div xmlns="" class="navheader">
|
||
<div class="libver">
|
||
<p>Library Version 12.2.7.5</p>
|
||
</div>
|
||
<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 class="toc">
|
||
<p>
|
||
<b>Table of Contents</b>
|
||
</p>
|
||
<dl>
|
||
<dt>
|
||
<span class="sect1">
|
||
<a href="dpl_example.html#vendorclass">Vendor.java</a>
|
||
</span>
|
||
</dt>
|
||
<dt>
|
||
<span class="sect1">
|
||
<a href="inventoryclass.html">Inventory.java</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.java</a>
|
||
</span>
|
||
</dt>
|
||
<dt>
|
||
<span class="sect1">
|
||
<a href="dpl_exampledatabaseput.html">ExampleDatabasePut.java</a>
|
||
</span>
|
||
</dt>
|
||
<dt>
|
||
<span class="sect1">
|
||
<a href="dpl_exampleinventoryread.html">ExampleInventoryRead.java</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 class="xref" href="dpl_example.html#vendorclass" title="Vendor.java">Vendor.java</a>
|
||
and
|
||
<a class="xref" href="inventoryclass.html" title="Inventory.java">Inventory.java</a>.
|
||
</p>
|
||
</li>
|
||
<li>
|
||
<p>
|
||
A convenience class used to open and close
|
||
our environment and entity store. See
|
||
<a class="xref" href="mydbenv-persist.html" title="MyDbEnv">MyDbEnv</a>.
|
||
</p>
|
||
</li>
|
||
<li>
|
||
<p>
|
||
A class that loads data into the store. See
|
||
<a class="xref" href="dpl_exampledatabaseput.html" title="ExampleDatabasePut.java">ExampleDatabasePut.java</a>.
|
||
</p>
|
||
</li>
|
||
<li>
|
||
<p>
|
||
Finally, a class that reads data from the
|
||
store. See
|
||
<a class="xref" href="dpl_exampleinventoryread.html" title="ExampleInventoryRead.java">ExampleInventoryRead.java</a>.
|
||
</p>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<p>
|
||
Be aware that this example can be found in your JE distribution in
|
||
the following location:
|
||
</p>
|
||
<p>
|
||
<span class="emphasis"><em>JE_HOME</em></span>/examples/persist/gettingStarted
|
||
</p>
|
||
<p>
|
||
where <code class="literal"><span class="emphasis"><em>JE_HOME</em></span></code> is the location where you
|
||
placed your JE distribution.
|
||
</p>
|
||
<div class="sect1" lang="en" xml:lang="en">
|
||
<div class="titlepage">
|
||
<div>
|
||
<div>
|
||
<h2 class="title" style="clear: both"><a id="vendorclass"></a>Vendor.java</h2>
|
||
</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
|
||
<code class="literal">vendor</code> 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
|
||
<code class="classname">EntityStore</code>, 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
|
||
<code class="classname">Vendor</code> class appears as
|
||
follows. Notice that the <code class="literal">@Entity</code>
|
||
annotation appears immediately before the class
|
||
declaration, and the <code class="literal">@PrimaryKey</code>
|
||
annotation appears immediately before the
|
||
<code class="literal">vendor</code> 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 <code class="literal">vendor</code> value is set for an individual
|
||
<code class="classname">Vendor</code> class object by
|
||
the <code class="methodname">setVendorName()</code>
|
||
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.java</td>
|
||
</tr>
|
||
</table>
|
||
</div>
|
||
</body>
|
||
</html>
|