summaryrefslogtreecommitdiff
path: root/db/docs/collections/tutorial/SerializableEntity.html
blob: 9f63bab4e9c254df066cbfda3f9381013293fd6f (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?xml version="1.0" encoding="ISO-8859-1" 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=ISO-8859-1" />
    <title>Chapter 6. 
		Using Serializable Entities
	</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="Berkeley DB Collections Tutorial" />
    <link rel="up" href="index.html" title="Berkeley DB Collections Tutorial" />
    <link rel="previous" href="sortedcollections.html" title="&#10;&#9;&#9;Using Sorted Collections&#10;&#9;" />
    <link rel="next" href="transientfieldsinbinding.html" title="&#10;&#9;&#9;Using Transient Fields in an Entity Binding&#10;&#9;" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Chapter 6. 
		Using Serializable Entities
	</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="sortedcollections.html">Prev</a> </td>
          <th width="60%" align="center"> </th>
          <td width="20%" align="right"> <a accesskey="n" href="transientfieldsinbinding.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="SerializableEntity"></a>Chapter 6. 
		Using Serializable Entities
	</h2>
          </div>
        </div>
        <div></div>
      </div>
      <div class="toc">
        <p>
          <b>Table of Contents</b>
        </p>
        <dl>
          <dt>
            <span class="sect1">
              <a href="SerializableEntity.html#transientfieldsinclass">
		Using Transient Fields in an Entity Class
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="transientfieldsinbinding.html">
		Using Transient Fields in an Entity Binding
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="removingredundantvalueclasses.html">
		Removing the Redundant Value Classes
	</a>
            </span>
          </dt>
        </dl>
      </div>
      <p>
    In the prior examples that used entities (the Entity and Tuple examples) you
	may have noticed the redundancy between the serializable value
	classes and the entity classes. An entity class by definition
	contains all properties of the value class as well as all
	properties of the key class.
</p>
      <p>
    When using serializable values it is possible to remove this
	redundancy by changing the entity class in two ways:
</p>
      <div class="itemizedlist">
        <ul type="disc">
          <li>
            <p>
            Make the entity class serializable, so it can be used in place
            of the value class.
        </p>
          </li>
          <li>
            <p>
            Make the key fields transient, so they are not redundantly
            stored in the record.
        </p>
          </li>
        </ul>
      </div>
      <p>
    The modified entity class can then serve double-duty: It can be
	serialized and stored as the record value, and it can be used as
	the entity class as usual along with the Java collections API. The
	<tt class="classname">PartData</tt>, <tt class="classname">SupplierData</tt> and <tt class="classname">ShipmentData</tt>
	classes can then be removed.
</p>
      <p>
    Transient fields are defined in Java as fields that are not
	stored in the serialized form of an object. Therefore, when an
	object is deserialized the transient fields must be explicitly
	initialized. Since the entity binding is responsible for creating
	entity objects, it is the natural place to initialize the transient
	key fields.
</p>
      <p>
    Note that it is not strictly necessary to make the key fields of
	a serializable entity class transient. If this is not done, the key
	will simply be stored redundantly in the record's value. This extra
	storage may or may not be acceptable to an application. But since
	we are using tuple keys and an entity binding class must be
	implemented anyway to extract the key from the entity, it is
	sensible to use transient key fields to reduce the record size. Of
	course there may be a reason that transient fields are not desired;
	for example, if an application wants to serialize the entity
	objects for other purposes, then using transient fields should be
	avoided.
</p>
      <p>
    The complete source of the final version of the example program
	is included in the Berkeley DB distribution.
</p>
      <div class="sect1" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h2 class="title" style="clear: both"><a id="transientfieldsinclass"></a>
		Using Transient Fields in an Entity Class
	</h2>
            </div>
          </div>
          <div></div>
        </div>
        <p>
        The entity classes in this example are redefined such that they
        can be used both as serializable value classes and as entity
	    classes. Compared to the prior example there are three changes to
	    the <tt class="classname">Part</tt>, <tt class="classname">Supplier</tt> and <tt class="classname">Shipment</tt> entity
	    classes:
    </p>
        <div class="itemizedlist">
          <ul type="disc">
            <li>
              <p>
            Each class now implements the <tt class="classname">Serializable</tt>
            interface.
        </p>
            </li>
            <li>
              <p>
            The key fields in each class are declared as <tt class="literal">transient</tt>.
        </p>
            </li>
            <li>
              <p>
            A package-private <tt class="methodname">setKey()</tt> method is added to each class
	        for initializing the transient key fields. This method will be
	        called from the entity bindings.
        </p>
            </li>
          </ul>
        </div>
        <a id="sentity_part"></a>
        <pre class="programlisting"><b class="userinput"><tt>import java.io.Serializable;</tt></b>
...
public class Part <b class="userinput"><tt>implements Serializable</tt></b>
{
    private <b class="userinput"><tt>transient</tt></b> String number;
    private String name;
    private String color;
    private Weight weight;
    private String city;

    public Part(String number, String name, String color, Weight weight,
                String city)
    {
        this.number = number;
        this.name = name;
        this.color = color;
        this.weight = weight;
        this.city = city;
    }

<b class="userinput"><tt>    final void setKey(String number)
    {
        this.number = number;
    }</tt></b>

    public final String getNumber()
    {
        return number;
    }

    public final String getName()
    {
        return name;
    }

    public final String getColor()
    {
        return color;
    }

    public final Weight getWeight()
    {
        return weight;
    }

    public final String getCity()
    {
        return city;
    }

    public String toString()
    {
        return "Part: number=" + number +
               " name=" + name +
               " color=" + color +
               " weight=" + weight +
               " city=" + city + '.';
    }
}
...
public class Supplier <b class="userinput"><tt>implements Serializable</tt></b>
{
    private <b class="userinput"><tt>transient</tt></b> String number;
    private String name;
    private int status;
    private String city;

    public Supplier(String number, String name, int status, String city)
    {
        this.number = number;
        this.name = name;
        this.status = status;
        this.city = city;
    }

<b class="userinput"><tt>    void setKey(String number)
    {
        this.number = number;
    }</tt></b>

    public final String getNumber()
    {
        return number;
    }

    public final String getName()
    {
        return name;
    }

    public final int getStatus()
    {
        return status;
    }

    public final String getCity()
    {
        return city;
    }

    public String toString()
    {
        return "Supplier: number=" + number +
               " name=" + name +
               " status=" + status +
               " city=" + city + '.';
    }
}
...
public class Shipment <b class="userinput"><tt>implements Serializable</tt></b>
{
    private <b class="userinput"><tt>transient</tt></b> String partNumber;
    private <b class="userinput"><tt>transient</tt></b> String supplierNumber;
    private int quantity;

    public Shipment(String partNumber, String supplierNumber, int quantity)
    {
        this.partNumber = partNumber;
        this.supplierNumber = supplierNumber;
        this.quantity = quantity;
    }

<b class="userinput"><tt>    void setKey(String partNumber, String supplierNumber)
    {
        this.partNumber = partNumber;
        this.supplierNumber = supplierNumber;
    } </tt></b>

    public final String getPartNumber()
    {
        return partNumber;
    }

    public final String getSupplierNumber()
    {
        return supplierNumber;
    }

    public final int getQuantity()
    {
        return quantity;
    }

    public String toString()
    {
        return "Shipment: part=" + partNumber +
                " supplier=" + supplierNumber +
                " quantity=" + quantity + '.';
    }
}
	</pre>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="sortedcollections.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="index.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="transientfieldsinbinding.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">
		Using Sorted Collections
	 </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> 
		Using Transient Fields in an Entity Binding
	</td>
        </tr>
      </table>
    </div>
  </body>
</html>