blob: ad066d74f17c4595d99791337a1a95ad2f5ae29d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
<?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" />
<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.1.6.1</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>
<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>
|