| 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>Creating Tuple Key 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="Tuple.html" title="Chapter 5. Using Tuples" /> |
| 11 |
<link rel="prev" href="tupleswithkeycreators.html" title="Using Tuples with Key Creators" /> |
| 12 |
<link rel="next" href="tuple-serialentitybindings.html" title="Creating Tuple-Serial Entity Bindings" /> |
| 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"> |
| 22 |
Creating Tuple Key Bindings |
| 23 |
</th> |
| 24 |
</tr> |
| 25 |
<tr> |
| 26 |
<td width="20%" align="left"><a accesskey="p" href="tupleswithkeycreators.html">Prev</a> </td> |
| 27 |
<th width="60%" align="center">Chapter 5. |
| 28 |
Using Tuples |
| 29 |
</th> |
| 30 |
<td width="20%" align="right"> <a accesskey="n" href="tuple-serialentitybindings.html">Next</a></td> |
| 31 |
</tr> |
| 32 |
</table> |
| 33 |
<hr /> |
| 34 |
</div> |
| 35 |
<div class="sect1" lang="en" xml:lang="en"> |
| 36 |
<div class="titlepage"> |
| 37 |
<div> |
| 38 |
<div> |
| 39 |
<h2 class="title" style="clear: both"><a id="tuplekeybindings"></a> |
| 40 |
Creating Tuple Key Bindings |
| 41 |
</h2> |
| 42 |
</div> |
| 43 |
</div> |
| 44 |
</div> |
| 45 |
<p> |
| 46 |
Serial bindings were used in prior examples as key bindings, and |
| 47 |
keys were stored as serialized objects. In this example, a tuple |
| 48 |
binding is used for each key since keys will be stored as tuples. |
| 49 |
Because keys are no longer stored as serialized objects, the |
| 50 |
<code class="classname">PartKey</code>, <code class="classname">SupplierKey</code> and <code class="classname">ShipmentKey</code> classes |
| 51 |
no longer implement the |
| 52 |
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html" target="_top">Serializable</a> |
| 53 |
|
| 54 |
interface (this is the only change to these classes and is not |
| 55 |
shown below). |
| 56 |
</p> |
| 57 |
<p> |
| 58 |
For the <code class="classname">Part</code> key, <code class="classname">Supplier</code> key, |
| 59 |
and <code class="classname">Shipment</code> key, the |
| 60 |
<code class="classname">SampleViews</code> class was changed in this example to create a |
| 61 |
custom |
| 62 |
<a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleBinding.html" target="_top">TupleBinding</a> |
| 63 |
|
| 64 |
instead of a |
| 65 |
<a class="ulink" href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>. |
| 66 |
The custom tuple key binding classes are defined further below. |
| 67 |
</p> |
| 68 |
<a id="tuple_sampleviews"></a> |
| 69 |
<pre class="programlisting"><strong class="userinput"><code>import com.sleepycat.bind.tuple.TupleBinding; |
| 70 |
...</code></strong> |
| 71 |
public class SampleViews |
| 72 |
{ |
| 73 |
... |
| 74 |
public SampleViews(SampleDatabase db) |
| 75 |
{ |
| 76 |
... |
| 77 |
ClassCatalog catalog = db.getClassCatalog(); |
| 78 |
<strong class="userinput"><code> EntryBinding partKeyBinding = |
| 79 |
new PartKeyBinding(); |
| 80 |
EntityBinding partDataBinding = |
| 81 |
new PartBinding(catalog, PartData.class); |
| 82 |
EntryBinding supplierKeyBinding = |
| 83 |
new SupplierKeyBinding(); |
| 84 |
EntityBinding supplierDataBinding = |
| 85 |
new SupplierBinding(catalog, SupplierData.class); |
| 86 |
EntryBinding shipmentKeyBinding = |
| 87 |
new ShipmentKeyBinding(); |
| 88 |
EntityBinding shipmentDataBinding = |
| 89 |
new ShipmentBinding(catalog, ShipmentData.class); |
| 90 |
EntryBinding cityKeyBinding = |
| 91 |
TupleBinding.getPrimitiveBinding(String.class);</code></strong> |
| 92 |
... |
| 93 |
} |
| 94 |
} </pre> |
| 95 |
<p> |
| 96 |
For the City key, however, a custom binding class is not needed |
| 97 |
because the key class is a primitive Java type, |
| 98 |
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html" target="_top">String</a>. |
| 99 |
For any primitive Java type, a tuple binding may be created using the |
| 100 |
<a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleBinding.html#getPrimitiveBinding(java.lang.Class)" target="_top">TupleBinding.getPrimitiveBinding</a> |
| 101 |
|
| 102 |
static method. |
| 103 |
</p> |
| 104 |
<p> |
| 105 |
The custom key binding classes, <code class="classname">PartKeyBinding</code>, |
| 106 |
<code class="classname">SupplierKeyBinding</code> and <code class="classname">ShipmentKeyBinding</code>, are |
| 107 |
defined by extending the |
| 108 |
<a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleBinding.html" target="_top">TupleBinding</a> |
| 109 |
|
| 110 |
class. The |
| 111 |
<a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleBinding.html" target="_top">TupleBinding</a> |
| 112 |
|
| 113 |
abstract class implements the |
| 114 |
<a class="ulink" href="../../java/com/sleepycat/bind/EntryBinding.html" target="_top">EntryBinding</a> |
| 115 |
|
| 116 |
interface, and is used for one-to-one bindings between tuples and |
| 117 |
objects. Each binding class implements two methods for converting |
| 118 |
between tuples and objects. Tuple fields are read using the |
| 119 |
<a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleInput.html" target="_top">TupleInput</a> |
| 120 |
|
| 121 |
parameter and written using the |
| 122 |
<a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleOutput.html" target="_top">TupleOutput</a> |
| 123 |
|
| 124 |
parameter. |
| 125 |
</p> |
| 126 |
<a id="tuple_partkeybinding"></a> |
| 127 |
<pre class="programlisting"><strong class="userinput"><code>import com.sleepycat.bind.tuple.TupleBinding; |
| 128 |
import com.sleepycat.bind.tuple.TupleInput; |
| 129 |
import com.sleepycat.bind.tuple.TupleOutput; |
| 130 |
...</code></strong> |
| 131 |
public class SampleViews |
| 132 |
{ |
| 133 |
... |
| 134 |
|
| 135 |
<strong class="userinput"><code>private static class PartKeyBinding extends TupleBinding |
| 136 |
{ |
| 137 |
private PartKeyBinding() |
| 138 |
{ |
| 139 |
} |
| 140 |
|
| 141 |
public Object entryToObject(TupleInput input) |
| 142 |
{ |
| 143 |
String number = input.readString(); |
| 144 |
return new PartKey(number); |
| 145 |
} |
| 146 |
|
| 147 |
public void objectToEntry(Object object, TupleOutput output) |
| 148 |
{ |
| 149 |
PartKey key = (PartKey) object; |
| 150 |
output.writeString(key.getNumber()); |
| 151 |
} |
| 152 |
} |
| 153 |
... |
| 154 |
private static class SupplierKeyBinding extends TupleBinding |
| 155 |
{ |
| 156 |
private SupplierKeyBinding() |
| 157 |
{ |
| 158 |
} |
| 159 |
|
| 160 |
public Object entryToObject(TupleInput input) |
| 161 |
{ |
| 162 |
String number = input.readString(); |
| 163 |
return new SupplierKey(number); |
| 164 |
} |
| 165 |
|
| 166 |
public void objectToEntry(Object object, TupleOutput output) |
| 167 |
{ |
| 168 |
SupplierKey key = (SupplierKey) object; |
| 169 |
output.writeString(key.getNumber()); |
| 170 |
} |
| 171 |
} |
| 172 |
... |
| 173 |
private static class ShipmentKeyBinding extends TupleBinding |
| 174 |
{ |
| 175 |
private ShipmentKeyBinding() |
| 176 |
{ |
| 177 |
} |
| 178 |
|
| 179 |
public Object entryToObject(TupleInput input) |
| 180 |
{ |
| 181 |
String partNumber = input.readString(); |
| 182 |
String supplierNumber = input.readString(); |
| 183 |
return new ShipmentKey(partNumber, supplierNumber); |
| 184 |
} |
| 185 |
|
| 186 |
public void objectToEntry(Object object, TupleOutput output) |
| 187 |
{ |
| 188 |
ShipmentKey key = (ShipmentKey) object; |
| 189 |
output.writeString(key.getPartNumber()); |
| 190 |
output.writeString(key.getSupplierNumber()); |
| 191 |
} |
| 192 |
}</code></strong> |
| 193 |
... |
| 194 |
} </pre> |
| 195 |
</div> |
| 196 |
<div class="navfooter"> |
| 197 |
<hr /> |
| 198 |
<table width="100%" summary="Navigation footer"> |
| 199 |
<tr> |
| 200 |
<td width="40%" align="left"><a accesskey="p" href="tupleswithkeycreators.html">Prev</a> </td> |
| 201 |
<td width="20%" align="center"> |
| 202 |
<a accesskey="u" href="Tuple.html">Up</a> |
| 203 |
</td> |
| 204 |
<td width="40%" align="right"> <a accesskey="n" href="tuple-serialentitybindings.html">Next</a></td> |
| 205 |
</tr> |
| 206 |
<tr> |
| 207 |
<td width="40%" align="left" valign="top"> |
| 208 |
Using Tuples with Key Creators |
| 209 |
</td> |
| 210 |
<td width="20%" align="center"> |
| 211 |
<a accesskey="h" href="index.html">Home</a> |
| 212 |
</td> |
| 213 |
<td width="40%" align="right" valign="top"> |
| 214 |
Creating Tuple-Serial Entity Bindings |
| 215 |
</td> |
| 216 |
</tr> |
| 217 |
</table> |
| 218 |
</div> |
| 219 |
</body> |
| 220 |
</html> |