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">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Creating Entity Bindings</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="Entity.html" title="Chapter 4. Using Entity Classes" />
11 <link rel="prev" href="Entity.html" title="Chapter 4. Using Entity Classes" />
12 <link rel="next" href="collectionswithentities.html" title="Creating Collections with Entity Bindings" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">
22 Creating Entity Bindings
26 <td width="20%" align="left"><a accesskey="p" href="Entity.html">Prev</a> </td>
27 <th width="60%" align="center">Chapter 4.
30 <td width="20%" align="right"> <a accesskey="n" href="collectionswithentities.html">Next</a></td>
35 <div class="sect1" lang="en" xml:lang="en">
36 <div class="titlepage">
39 <h2 class="title" style="clear: both"><a id="creatingentitybindings"></a>
40 Creating Entity Bindings
46 <span class="emphasis"><em>Entity bindings</em></span> are similar to ordinary bindings in that
47 they convert between Java objects and the stored data format of
48 keys and values. In addition, entity bindings map between key/value
49 pairs and entity objects. An ordinary binding is a one-to-one
50 mapping, while an entity binding is a two-to-one mapping.
53 The <code class="literal">partValueBinding</code>, <code class="literal">supplierValueBinding</code> and
54 <code class="literal">shipmentValueBinding</code> bindings are created below as entity
55 bindings rather than (in the prior examples) serial bindings.
57 <a id="entity_sampleviews"></a>
58 <pre class="programlisting">import com.sleepycat.bind.EntryBinding;
59 <strong class="userinput"><code>import com.sleepycat.bind.EntityBinding;</code></strong>
60 import com.sleepycat.bind.serial.SerialBinding;
61 <strong class="userinput"><code>import com.sleepycat.bind.serial.SerialSerialBinding;</code></strong>
64 public class SampleViews
67 public SampleViews(SampleDatabase db)
69 ClassCatalog catalog = db.getClassCatalog();
70 SerialBinding partKeyBinding =
71 new SerialBinding(catalog, PartKey.class);
72 <strong class="userinput"><code> EntityBinding partValueBinding =
73 new PartBinding(catalog, PartKey.class, PartData.class);</code></strong>
74 SerialBinding supplierKeyBinding =
75 new SerialBinding(catalog, SupplierKey.class);
76 <strong class="userinput"><code> EntityBinding supplierValueBinding =
77 new SupplierBinding(catalog, SupplierKey.class,
78 SupplierData.class);</code></strong>
79 SerialBinding shipmentKeyBinding =
80 new SerialBinding(catalog, ShipmentKey.class);
81 <strong class="userinput"><code> EntityBinding shipmentValueBinding =
82 new ShipmentBinding(catalog, ShipmentKey.class,
83 ShipmentData.class);</code></strong>
84 SerialBinding cityKeyBinding =
85 new SerialBinding(catalog, String.class);
90 The entity bindings will be used in the next section to
91 construct stored map objects.
94 The <code class="classname">PartBinding</code> class is defined below.
96 <a id="entity_partbinding"></a>
97 <pre class="programlisting">public class SampleViews
100 <strong class="userinput"><code> private static class PartBinding extends SerialSerialBinding {
101 private PartBinding(ClassCatalog classCatalog,
105 super(classCatalog, keyClass, dataClass);
108 public Object entryToObject(Object keyInput, Object dataInput)
110 PartKey key = (PartKey) keyInput;
111 PartData data = (PartData) dataInput;
112 return new Part(key.getNumber(), data.getName(),
113 data.getColor(), data.getWeight(),
117 public Object objectToKey(Object object)
119 Part part = (Part) object;
120 return new PartKey(part.getNumber());
123 public Object objectToData(Object object)
125 Part part = (Part) object;
126 return new PartData(part.getName(), part.getColor(),
127 part.getWeight(), part.getCity());
133 In general, an entity binding is any class that implements the
134 <a class="ulink" href="../../java/com/sleepycat/bind/EntityBinding.html" target="_top">EntityBinding</a>
136 interface, just as an ordinary binding is any class that implements
138 <a class="ulink" href="../../java/com/sleepycat/bind/EntryBinding.html" target="_top">EntryBinding</a>
140 interface. In the prior examples the built-in
141 <a class="ulink" href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
143 class (which implements
144 <a class="ulink" href="../../java/com/sleepycat/bind/EntryBinding.html" target="_top">EntryBinding</a>)
145 was used and no application-defined binding classes were needed.
148 In this example, application-defined binding classes are used
150 <a class="ulink" href="../../java/com/sleepycat/bind/serial/SerialSerialBinding.html" target="_top">SerialSerialBinding</a>
152 abstract base class. This base class implements
153 <a class="ulink" href="../../java/com/sleepycat/bind/EntityBinding.html" target="_top">EntityBinding</a>
155 and provides the conversions between key/value bytes and key/value
157 <a class="ulink" href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
159 class does. The application-defined entity class implements the
160 abstract methods defined in the base class that map between
161 key/value objects and entity objects.
164 Three abstract methods are implemented for each entity binding.
165 The <code class="methodname">entryToObject()</code> method takes as input the key and data
166 objects, which have been deserialized automatically by the base
167 class. As output, it returns the combined <code class="classname">Part</code> entity.
170 The <code class="methodname">objectToKey()</code> and <code class="methodname">objectToData()</code> methods take an
171 entity object as input. As output they return the part key or data
172 object that is extracted from the entity object. The key or data
173 will then be serialized automatically by the base class.
176 The <code class="classname">SupplierBinding</code> and <code class="classname">ShipmentBinding</code> classes
177 are very similar to the <code class="classname">PartBinding</code> class.
179 <a id="entity_supplierbinding"></a>
180 <pre class="programlisting">public class SampleViews
183 <strong class="userinput"><code> private static class SupplierBinding extends SerialSerialBinding {
184 private SupplierBinding(ClassCatalog classCatalog,
188 super(classCatalog, keyClass, dataClass);
191 public Object entryToObject(Object keyInput, Object dataInput)
193 SupplierKey key = (SupplierKey) keyInput;
194 SupplierData data = (SupplierData) dataInput;
195 return new Supplier(key.getNumber(), data.getName(),
196 data.getStatus(), data.getCity());
199 public Object objectToKey(Object object)
201 Supplier supplier = (Supplier) object;
202 return new SupplierKey(supplier.getNumber());
205 public Object objectToData(Object object)
207 Supplier supplier = (Supplier) object;
208 return new SupplierData(supplier.getName(),
209 supplier.getStatus(),
214 private static class ShipmentBinding extends SerialSerialBinding {
215 private ShipmentBinding(ClassCatalog classCatalog,
219 super(classCatalog, keyClass, dataClass);
222 public Object entryToObject(Object keyInput, Object dataInput)
224 ShipmentKey key = (ShipmentKey) keyInput;
225 ShipmentData data = (ShipmentData) dataInput;
226 return new Shipment(key.getPartNumber(),
227 key.getSupplierNumber(),
231 public Object objectToKey(Object object)
233 Shipment shipment = (Shipment) object;
234 return new ShipmentKey(shipment.getPartNumber(),
235 shipment.getSupplierNumber());
238 public Object objectToData(Object object)
240 Shipment shipment = (Shipment) object;
241 return new ShipmentData(shipment.getQuantity());
247 <div class="navfooter">
249 <table width="100%" summary="Navigation footer">
251 <td width="40%" align="left"><a accesskey="p" href="Entity.html">Prev</a> </td>
252 <td width="20%" align="center">
253 <a accesskey="u" href="Entity.html">Up</a>
255 <td width="40%" align="right"> <a accesskey="n" href="collectionswithentities.html">Next</a></td>
258 <td width="40%" align="left" valign="top">Chapter 4.
261 <td width="20%" align="center">
262 <a accesskey="h" href="index.html">Home</a>
264 <td width="40%" align="right" valign="top">
265 Creating Collections with Entity Bindings