Debian Codegraph @

docs/collections/tutorial/SerializableEntity.html

raw
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.  Using Serializable Entities</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="Berkeley DB Collections Tutorial" />
10 <link rel="up" href="index.html" title="Berkeley DB Collections Tutorial" />
11 <link rel="prev" href="sortedcollections.html" title="Using Sorted Collections" />
12 <link rel="next" href="transientfieldsinbinding.html" title="Using Transient Fields in an Entity Binding" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">Chapter 6. 
22 Using Serializable Entities
23 </th>
24 </tr>
25 <tr>
26 <td width="20%" align="left"><a accesskey="p" href="sortedcollections.html">Prev</a> </td>
27 <th width="60%" align="center"> </th>
28 <td width="20%" align="right"> <a accesskey="n" href="transientfieldsinbinding.html">Next</a></td>
29 </tr>
30 </table>
31 <hr />
32 </div>
33 <div class="chapter" lang="en" xml:lang="en">
34 <div class="titlepage">
35 <div>
36 <div>
37 <h2 class="title"><a id="SerializableEntity"></a>Chapter 6. 
38 Using Serializable Entities
39 </h2>
40 </div>
41 </div>
42 </div>
43 <div class="toc">
44 <p>
45 <b>Table of Contents</b>
46 </p>
47 <dl>
48 <dt>
49 <span class="sect1">
50 <a href="SerializableEntity.html#transientfieldsinclass">
51 Using Transient Fields in an Entity Class
52 </a>
53 </span>
54 </dt>
55 <dt>
56 <span class="sect1">
57 <a href="transientfieldsinbinding.html">
58 Using Transient Fields in an Entity Binding
59 </a>
60 </span>
61 </dt>
62 <dt>
63 <span class="sect1">
64 <a href="removingredundantvalueclasses.html">
65 Removing the Redundant Value Classes
66 </a>
67 </span>
68 </dt>
69 </dl>
70 </div>
71 <p>
72 In the prior examples that used entities (the Entity and Tuple examples) you
73 may have noticed the redundancy between the serializable value
74 classes and the entity classes. An entity class by definition
75 contains all properties of the value class as well as all
76 properties of the key class.
77 </p>
78 <p>
79 When using serializable values it is possible to remove this
80 redundancy by changing the entity class in two ways:
81 </p>
82 <div class="itemizedlist">
83 <ul type="disc">
84 <li>
85 <p>
86 Make the entity class serializable, so it can be used in place
87 of the value class.
88 </p>
89 </li>
90 <li>
91 <p>
92 Make the key fields transient, so they are not redundantly
93 stored in the record.
94 </p>
95 </li>
96 </ul>
97 </div>
98 <p>
99 The modified entity class can then serve double-duty: It can be
100 serialized and stored as the record value, and it can be used as
101 the entity class as usual along with the Java collections API. The
102 <code class="classname">PartData</code>, <code class="classname">SupplierData</code> and <code class="classname">ShipmentData</code>
103 classes can then be removed.
104 </p>
105 <p>
106 Transient fields are defined in Java as fields that are not
107 stored in the serialized form of an object. Therefore, when an
108 object is deserialized the transient fields must be explicitly
109 initialized. Since the entity binding is responsible for creating
110 entity objects, it is the natural place to initialize the transient
111 key fields.
112 </p>
113 <p>
114 Note that it is not strictly necessary to make the key fields of
115 a serializable entity class transient. If this is not done, the key
116 will simply be stored redundantly in the record's value. This extra
117 storage may or may not be acceptable to an application. But since
118 we are using tuple keys and an entity binding class must be
119 implemented anyway to extract the key from the entity, it is
120 sensible to use transient key fields to reduce the record size. Of
121 course there may be a reason that transient fields are not desired;
122 for example, if an application wants to serialize the entity
123 objects for other purposes, then using transient fields should be
124 avoided.
125 </p>
126 <p>
127 The complete source of the final version of the example program
128 is included in the Berkeley DB distribution.
129 </p>
130 <div class="sect1" lang="en" xml:lang="en">
131 <div class="titlepage">
132 <div>
133 <div>
134 <h2 class="title" style="clear: both"><a id="transientfieldsinclass"></a>
135 Using Transient Fields in an Entity Class
136 </h2>
137 </div>
138 </div>
139 </div>
140 <p>
141 The entity classes in this example are redefined such that they
142 can be used both as serializable value classes and as entity
143 classes. Compared to the prior example there are three changes to
144 the <code class="classname">Part</code>, <code class="classname">Supplier</code> and <code class="classname">Shipment</code> entity
145 classes:
146 </p>
147 <div class="itemizedlist">
148 <ul type="disc">
149 <li>
150 <p>
151 Each class now implements the <code class="classname">Serializable</code>
152 interface.
153 </p>
154 </li>
155 <li>
156 <p>
157 The key fields in each class are declared as <code class="literal">transient</code>.
158 </p>
159 </li>
160 <li>
161 <p>
162 A package-private <code class="methodname">setKey()</code> method is added to each class
163 for initializing the transient key fields. This method will be
164 called from the entity bindings.
165 </p>
166 </li>
167 </ul>
168 </div>
169 <a id="sentity_part"></a>
170 <pre class="programlisting"><strong class="userinput"><code>import java.io.Serializable;</code></strong>
171 ...
172 public class Part <strong class="userinput"><code>implements Serializable</code></strong>
173 {
174 private <strong class="userinput"><code>transient</code></strong> String number;
175 private String name;
176 private String color;
177 private Weight weight;
178 private String city;
179
180 public Part(String number, String name, String color, Weight weight,
181 String city)
182 {
183 this.number = number;
184 this.name = name;
185 this.color = color;
186 this.weight = weight;
187 this.city = city;
188 }
189
190 <strong class="userinput"><code> final void setKey(String number)
191 {
192 this.number = number;
193 }</code></strong>
194
195 public final String getNumber()
196 {
197 return number;
198 }
199
200 public final String getName()
201 {
202 return name;
203 }
204
205 public final String getColor()
206 {
207 return color;
208 }
209
210 public final Weight getWeight()
211 {
212 return weight;
213 }
214
215 public final String getCity()
216 {
217 return city;
218 }
219
220 public String toString()
221 {
222 return "Part: number=" + number +
223 " name=" + name +
224 " color=" + color +
225 " weight=" + weight +
226 " city=" + city + '.';
227 }
228 }
229 ...
230 public class Supplier <strong class="userinput"><code>implements Serializable</code></strong>
231 {
232 private <strong class="userinput"><code>transient</code></strong> String number;
233 private String name;
234 private int status;
235 private String city;
236
237 public Supplier(String number, String name, int status, String city)
238 {
239 this.number = number;
240 this.name = name;
241 this.status = status;
242 this.city = city;
243 }
244
245 <strong class="userinput"><code> void setKey(String number)
246 {
247 this.number = number;
248 }</code></strong>
249
250 public final String getNumber()
251 {
252 return number;
253 }
254
255 public final String getName()
256 {
257 return name;
258 }
259
260 public final int getStatus()
261 {
262 return status;
263 }
264
265 public final String getCity()
266 {
267 return city;
268 }
269
270 public String toString()
271 {
272 return "Supplier: number=" + number +
273 " name=" + name +
274 " status=" + status +
275 " city=" + city + '.';
276 }
277 }
278 ...
279 public class Shipment <strong class="userinput"><code>implements Serializable</code></strong>
280 {
281 private <strong class="userinput"><code>transient</code></strong> String partNumber;
282 private <strong class="userinput"><code>transient</code></strong> String supplierNumber;
283 private int quantity;
284
285 public Shipment(String partNumber, String supplierNumber, int quantity)
286 {
287 this.partNumber = partNumber;
288 this.supplierNumber = supplierNumber;
289 this.quantity = quantity;
290 }
291
292 <strong class="userinput"><code> void setKey(String partNumber, String supplierNumber)
293 {
294 this.partNumber = partNumber;
295 this.supplierNumber = supplierNumber;
296 } </code></strong>
297
298 public final String getPartNumber()
299 {
300 return partNumber;
301 }
302
303 public final String getSupplierNumber()
304 {
305 return supplierNumber;
306 }
307
308 public final int getQuantity()
309 {
310 return quantity;
311 }
312
313 public String toString()
314 {
315 return "Shipment: part=" + partNumber +
316 " supplier=" + supplierNumber +
317 " quantity=" + quantity + '.';
318 }
319 }
320 </pre>
321 </div>
322 </div>
323 <div class="navfooter">
324 <hr />
325 <table width="100%" summary="Navigation footer">
326 <tr>
327 <td width="40%" align="left"><a accesskey="p" href="sortedcollections.html">Prev</a> </td>
328 <td width="20%" align="center"> </td>
329 <td width="40%" align="right"> <a accesskey="n" href="transientfieldsinbinding.html">Next</a></td>
330 </tr>
331 <tr>
332 <td width="40%" align="left" valign="top">
333 Using Sorted Collections
334  </td>
335 <td width="20%" align="center">
336 <a accesskey="h" href="index.html">Home</a>
337 </td>
338 <td width="40%" align="right" valign="top"> 
339 Using Transient Fields in an Entity Binding
340 </td>
341 </tr>
342 </table>
343 </div>
344 </body>
345 </html>