• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500-V1.0.1.40_1.0.68/ap/gpl/timemachine/db-4.7.25.NC/docs/collections/tutorial/
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.��
7		Using Serializable Entities
8	</title>
9    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
10    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
11    <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" />
12    <link rel="up" href="index.html" title="Berkeley DB Collections Tutorial" />
13    <link rel="previous" href="sortedcollections.html" title="&#10;&#9;&#9;Using Sorted Collections&#10;&#9;" />
14    <link rel="next" href="transientfieldsinbinding.html" title="&#10;&#9;&#9;Using Transient Fields in an Entity Binding&#10;&#9;" />
15  </head>
16  <body>
17    <div class="navheader">
18      <table width="100%" summary="Navigation header">
19        <tr>
20          <th colspan="3" align="center">Chapter��6.��
21		Using Serializable Entities
22	</th>
23        </tr>
24        <tr>
25          <td width="20%" align="left"><a accesskey="p" href="sortedcollections.html">Prev</a>��</td>
26          <th width="60%" align="center">��</th>
27          <td width="20%" align="right">��<a accesskey="n" href="transientfieldsinbinding.html">Next</a></td>
28        </tr>
29      </table>
30      <hr />
31    </div>
32    <div class="chapter" lang="en" xml:lang="en">
33      <div class="titlepage">
34        <div>
35          <div>
36            <h2 class="title"><a id="SerializableEntity"></a>Chapter��6.��
37		Using Serializable Entities
38	</h2>
39          </div>
40        </div>
41        <div></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	<tt class="classname">PartData</tt>, <tt class="classname">SupplierData</tt> and <tt class="classname">ShipmentData</tt>
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></div>
140        </div>
141        <p>
142        The entity classes in this example are redefined such that they
143        can be used both as serializable value classes and as entity
144	    classes. Compared to the prior example there are three changes to
145	    the <tt class="classname">Part</tt>, <tt class="classname">Supplier</tt> and <tt class="classname">Shipment</tt> entity
146	    classes:
147    </p>
148        <div class="itemizedlist">
149          <ul type="disc">
150            <li>
151              <p>
152            Each class now implements the <tt class="classname">Serializable</tt>
153            interface.
154        </p>
155            </li>
156            <li>
157              <p>
158            The key fields in each class are declared as <tt class="literal">transient</tt>.
159        </p>
160            </li>
161            <li>
162              <p>
163            A package-private <tt class="methodname">setKey()</tt> method is added to each class
164	        for initializing the transient key fields. This method will be
165	        called from the entity bindings.
166        </p>
167            </li>
168          </ul>
169        </div>
170        <a id="sentity_part"></a>
171        <pre class="programlisting"><b class="userinput"><tt>import java.io.Serializable;</tt></b>
172...
173public class Part <b class="userinput"><tt>implements Serializable</tt></b>
174{
175    private <b class="userinput"><tt>transient</tt></b> String number;
176    private String name;
177    private String color;
178    private Weight weight;
179    private String city;
180
181    public Part(String number, String name, String color, Weight weight,
182                String city)
183    {
184        this.number = number;
185        this.name = name;
186        this.color = color;
187        this.weight = weight;
188        this.city = city;
189    }
190
191<b class="userinput"><tt>    final void setKey(String number)
192    {
193        this.number = number;
194    }</tt></b>
195
196    public final String getNumber()
197    {
198        return number;
199    }
200
201    public final String getName()
202    {
203        return name;
204    }
205
206    public final String getColor()
207    {
208        return color;
209    }
210
211    public final Weight getWeight()
212    {
213        return weight;
214    }
215
216    public final String getCity()
217    {
218        return city;
219    }
220
221    public String toString()
222    {
223        return "Part: number=" + number +
224               " name=" + name +
225               " color=" + color +
226               " weight=" + weight +
227               " city=" + city + '.';
228    }
229}
230...
231public class Supplier <b class="userinput"><tt>implements Serializable</tt></b>
232{
233    private <b class="userinput"><tt>transient</tt></b> String number;
234    private String name;
235    private int status;
236    private String city;
237
238    public Supplier(String number, String name, int status, String city)
239    {
240        this.number = number;
241        this.name = name;
242        this.status = status;
243        this.city = city;
244    }
245
246<b class="userinput"><tt>    void setKey(String number)
247    {
248        this.number = number;
249    }</tt></b>
250
251    public final String getNumber()
252    {
253        return number;
254    }
255
256    public final String getName()
257    {
258        return name;
259    }
260
261    public final int getStatus()
262    {
263        return status;
264    }
265
266    public final String getCity()
267    {
268        return city;
269    }
270
271    public String toString()
272    {
273        return "Supplier: number=" + number +
274               " name=" + name +
275               " status=" + status +
276               " city=" + city + '.';
277    }
278}
279...
280public class Shipment <b class="userinput"><tt>implements Serializable</tt></b>
281{
282    private <b class="userinput"><tt>transient</tt></b> String partNumber;
283    private <b class="userinput"><tt>transient</tt></b> String supplierNumber;
284    private int quantity;
285
286    public Shipment(String partNumber, String supplierNumber, int quantity)
287    {
288        this.partNumber = partNumber;
289        this.supplierNumber = supplierNumber;
290        this.quantity = quantity;
291    }
292
293<b class="userinput"><tt>    void setKey(String partNumber, String supplierNumber)
294    {
295        this.partNumber = partNumber;
296        this.supplierNumber = supplierNumber;
297    } </tt></b>
298
299    public final String getPartNumber()
300    {
301        return partNumber;
302    }
303
304    public final String getSupplierNumber()
305    {
306        return supplierNumber;
307    }
308
309    public final int getQuantity()
310    {
311        return quantity;
312    }
313
314    public String toString()
315    {
316        return "Shipment: part=" + partNumber +
317                " supplier=" + supplierNumber +
318                " quantity=" + quantity + '.';
319    }
320}
321	</pre>
322      </div>
323    </div>
324    <div class="navfooter">
325      <hr />
326      <table width="100%" summary="Navigation footer">
327        <tr>
328          <td width="40%" align="left"><a accesskey="p" href="sortedcollections.html">Prev</a>��</td>
329          <td width="20%" align="center">
330            <a accesskey="u" href="index.html">Up</a>
331          </td>
332          <td width="40%" align="right">��<a accesskey="n" href="transientfieldsinbinding.html">Next</a></td>
333        </tr>
334        <tr>
335          <td width="40%" align="left" valign="top">
336		Using Sorted Collections
337	��</td>
338          <td width="20%" align="center">
339            <a accesskey="h" href="index.html">Home</a>
340          </td>
341          <td width="40%" align="right" valign="top">��
342		Using Transient Fields in an Entity Binding
343	</td>
344        </tr>
345      </table>
346    </div>
347  </body>
348</html>
349