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 4. 
7        Using Entity Classes	
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="retrievingbyindexkey.html" title="&#10;&#9;&#9;Retrieving Items by Index Key&#10;&#9;" />
14    <link rel="next" href="creatingentitybindings.html" title="&#10;&#9;&#9;Creating Entity Bindings&#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 4. 
21        Using Entity Classes	
22	</th>
23        </tr>
24        <tr>
25          <td width="20%" align="left"><a accesskey="p" href="retrievingbyindexkey.html">Prev</a> </td>
26          <th width="60%" align="center"> </th>
27          <td width="20%" align="right"> <a accesskey="n" href="creatingentitybindings.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="Entity"></a>Chapter 4. 
37        Using Entity Classes	
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="Entity.html#definingentityclasses">
51		Defining Entity Classes
52	</a>
53            </span>
54          </dt>
55          <dt>
56            <span class="sect1">
57              <a href="creatingentitybindings.html">
58		Creating Entity Bindings
59	</a>
60            </span>
61          </dt>
62          <dt>
63            <span class="sect1">
64              <a href="collectionswithentities.html">
65		Creating Collections with Entity Bindings
66	</a>
67            </span>
68          </dt>
69          <dt>
70            <span class="sect1">
71              <a href="entitieswithcollections.html">
72		Using Entities with Collections
73	</a>
74            </span>
75          </dt>
76        </dl>
77      </div>
78      <p>
79    In the prior examples, the keys and values of each store were
80	represented using separate classes. For example, a <tt class="classname">PartKey</tt>
81	and a <tt class="classname">PartData</tt> class were used. Many times it is desirable
82	to have a single class representing both the key and the value, for
83	example, a <tt class="classname">Part</tt> class.
84</p>
85      <p>
86    Such a combined key and value class is called an <span class="emphasis"><em>entity
87	class</em></span> and is used along with an <span class="emphasis"><em>entity binding</em></span>. Entity
88	bindings combine a key and a value into an entity when reading a
89	record from a collection, and split an entity into a key and a
90	value when writing a record to a collection. Entity bindings are
91	used in place of value bindings, and entity objects are used with
92	collections in place of value objects.
93</p>
94      <p>
95    Some reasons for using entities are:
96</p>
97      <div class="itemizedlist">
98        <ul type="disc">
99          <li>
100            <p>
101            When the key is a property of an entity object representing the
102            record as a whole, the object's identity and concept are often
103            clearer than with key and value objects that are disjoint.
104        </p>
105          </li>
106          <li>
107            <p>
108            A single entity object per record is often more convenient to
109            use than two objects.
110        </p>
111          </li>
112        </ul>
113      </div>
114      <p>
115    Of course, instead of using an entity binding, you could simply
116	create the entity yourself after reading the key and value from a
117	collection, and split the entity into a key and value yourself
118	before writing it to a collection. But this would detract from the
119	convenience of the using the Java collections API. It is convenient
120	to obtain a <tt class="classname">Part</tt> object directly from 
121	<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#get" target="_top">Map.get</a>
122	
123	and to add a <tt class="classname">Part</tt> object using 
124	<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html#add" target="_top">Set.add</a>.
125	Collections having entity bindings can be used naturally without
126	combining and splitting objects each time a collection method is
127	called; however, an entity binding class must be defined by the
128	application.
129</p>
130      <p>
131    In addition to showing how to use entity bindings, this example
132	illustrates a key feature of all bindings: Bindings are independent
133	of database storage parameters and formats. Compare this example to
134	the prior Index example and you'll see that the <tt class="classname">Sample</tt> and
135	<tt class="classname">SampleViews</tt> classes have been changed to use entity
136	bindings, but the <tt class="classname">SampleDatabase</tt> class was not changed at
137	all. In fact, the Entity program and the Index program can be used
138	interchangeably to access the same physical database files. This
139	demonstrates that bindings are only a "view" onto the physical
140	stored data.
141</p>
142      <p>
143    <tt class="classname">Warning:</tt> When using multiple bindings for the same
144	database, it is the application's responsibility to ensure that the
145	same format is used for all bindings. For example, a serial binding
146	and a tuple binding cannot be used to access the same records.
147</p>
148      <p>
149    The complete source of the final version of the example program
150	is included in the Berkeley DB distribution.
151</p>
152      <div class="sect1" lang="en" xml:lang="en">
153        <div class="titlepage">
154          <div>
155            <div>
156              <h2 class="title" style="clear: both"><a id="definingentityclasses"></a>
157		Defining Entity Classes
158	</h2>
159            </div>
160          </div>
161          <div></div>
162        </div>
163        <p>
164    As described in the prior section, <span class="emphasis"><em>entity classes</em></span> are
165	combined key/value classes that are managed by entity bindings. In
166	this example the <tt class="classname">Part</tt>, <tt class="classname">Supplier</tt> and <tt class="classname">Shipment</tt>
167	classes are entity classes. These classes contain fields that are a
168	union of the fields of the key and value classes that were defined
169	earlier for each store.
170</p>
171        <p>
172    In general, entity classes may be defined in any way desired by
173	the application. The entity binding, which is also defined by the
174	application, is responsible for mapping between key/value objects
175	and entity objects.
176</p>
177        <p>
178    The <tt class="classname">Part</tt>, <tt class="classname">Supplier</tt> and <tt class="classname">Shipment</tt> 
179    entity classes are
180	defined below.
181</p>
182        <p>
183    An important difference between the entity classes defined here
184	and the key and value classes defined earlier is that the entity
185	classes are not serializable (do not implement the 
186    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html" target="_top">Serializable</a>
187    
188	interface). This is because the entity classes are not directly
189	stored. The entity binding decomposes an entity object into key and
190	value objects, and only the key and value objects are serialized
191	for storage.
192</p>
193        <p>
194    One advantage of using entities can already be seen in the
195	<tt class="methodname">toString()</tt> method of the classes below. These return debugging
196	output for the combined key and value, and will be used later to
197	create a listing of the database that is more readable than in the
198	prior examples.
199</p>
200        <a id="entity_part"></a>
201        <pre class="programlisting"><b class="userinput"><tt>public class Part
202{
203    private String number;
204    private String name;
205    private String color;
206    private Weight weight;
207    private String city;
208
209    public Part(String number, String name, String color, Weight weight,
210                String city)
211    {
212        this.number = number;
213        this.name = name;
214        this.color = color;
215        this.weight = weight;
216        this.city = city;
217    }
218
219    public final String getNumber()
220    {
221        return number;
222    }
223
224    public final String getName()
225    {
226        return name;
227    }
228
229    public final String getColor()
230    {
231        return color;
232    }
233
234    public final Weight getWeight()
235    {
236        return weight;
237    }
238
239    public final String getCity()
240    {
241        return city;
242    }
243
244    public String toString()
245    {
246        return "Part: number=" + number +
247               " name=" + name +
248               " color=" + color +
249               " weight=" + weight +
250               " city=" + city + '.';
251    }
252}</tt></b> </pre>
253        <a id="entity_supplier"></a>
254        <pre class="programlisting"><b class="userinput"><tt>public class Supplier
255{
256    private String number;
257    private String name;
258    private int status;
259    private String city;
260
261    public Supplier(String number, String name, int status, String city)
262    {
263        this.number = number;
264        this.name = name;
265        this.status = status;
266        this.city = city;
267    }
268
269    public final String getNumber()
270    {
271        return number;
272    }
273
274    public final String getName()
275    {
276        return name;
277    }
278
279    public final int getStatus()
280    {
281        return status;
282    }
283
284    public final String getCity()
285    {
286        return city;
287    }
288
289    public String toString()
290    {
291        return "Supplier: number=" + number +
292               " name=" + name +
293               " status=" + status +
294               " city=" + city + '.';
295    }
296} </tt></b> </pre>
297        <a id="entity_shipment"></a>
298        <pre class="programlisting"><b class="userinput"><tt>public class Shipment
299{
300    private String partNumber;
301    private String supplierNumber;
302    private int quantity;
303
304    public Shipment(String partNumber, String supplierNumber, int quantity)
305    {
306        this.partNumber = partNumber;
307        this.supplierNumber = supplierNumber;
308        this.quantity = quantity;
309    }
310
311    public final String getPartNumber()
312    {
313        return partNumber;
314    }
315
316    public final String getSupplierNumber()
317    {
318        return supplierNumber;
319    }
320
321    public final int getQuantity()
322    {
323        return quantity;
324    }
325
326    public String toString()
327    {
328        return "Shipment: part=" + partNumber +
329                " supplier=" + supplierNumber +
330                " quantity=" + quantity + '.';
331    }
332} </tt></b> </pre>
333      </div>
334    </div>
335    <div class="navfooter">
336      <hr />
337      <table width="100%" summary="Navigation footer">
338        <tr>
339          <td width="40%" align="left"><a accesskey="p" href="retrievingbyindexkey.html">Prev</a> </td>
340          <td width="20%" align="center">
341            <a accesskey="u" href="index.html">Up</a>
342          </td>
343          <td width="40%" align="right"> <a accesskey="n" href="creatingentitybindings.html">Next</a></td>
344        </tr>
345        <tr>
346          <td width="40%" align="left" valign="top">
347		Retrieving Items by Index Key
348	 </td>
349          <td width="20%" align="center">
350            <a accesskey="h" href="index.html">Home</a>
351          </td>
352          <td width="40%" align="right" valign="top"353		Creating Entity Bindings
354	</td>
355        </tr>
356      </table>
357    </div>
358  </body>
359</html>
360