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> 7 Creating Tuple Key Bindings 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="Tuple.html" title="Chapter��5.�� 		Using Tuples 	" /> 13 <link rel="previous" href="tupleswithkeycreators.html" title=" 		Using Tuples with Key Creators 	" /> 14 <link rel="next" href="tuple-serialentitybindings.html" title=" Creating Tuple-Serial Entity Bindings " /> 15 </head> 16 <body> 17 <div class="navheader"> 18 <table width="100%" summary="Navigation header"> 19 <tr> 20 <th colspan="3" align="center"> 21 Creating Tuple Key Bindings 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="tupleswithkeycreators.html">Prev</a>��</td> 26 <th width="60%" align="center">Chapter��5.�� 27 Using Tuples 28 </th> 29 <td width="20%" align="right">��<a accesskey="n" href="tuple-serialentitybindings.html">Next</a></td> 30 </tr> 31 </table> 32 <hr /> 33 </div> 34 <div class="sect1" lang="en" xml:lang="en"> 35 <div class="titlepage"> 36 <div> 37 <div> 38 <h2 class="title" style="clear: both"><a id="tuplekeybindings"></a> 39 Creating Tuple Key Bindings 40 </h2> 41 </div> 42 </div> 43 <div></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 <tt class="classname">PartKey</tt>, <tt class="classname">SupplierKey</tt> and <tt class="classname">ShipmentKey</tt> classes 51 no longer implement the 52 <a href="http://java.sun.com/j2se/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 <tt class="classname">Part</tt> key, <tt class="classname">Supplier</tt> key, 59 and <tt class="classname">Shipment</tt> key, the 60 <tt class="classname">SampleViews</tt> class was changed in this example to create a 61 custom 62 <a href="../../java/com/sleepycat/bind/tuple/TupleBinding.html" target="_top">TupleBinding</a> 63 64 instead of a 65 <a 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"><b class="userinput"><tt>import com.sleepycat.bind.tuple.TupleBinding; 70...</tt></b> 71public class SampleViews 72{ 73 ... 74 public SampleViews(SampleDatabase db) 75 { 76 ... 77 ClassCatalog catalog = db.getClassCatalog(); 78<b class="userinput"><tt> 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);</tt></b> 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 href="http://java.sun.com/j2se/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 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, <tt class="classname">PartKeyBinding</tt>, 106 <tt class="classname">SupplierKeyBinding</tt> and <tt class="classname">ShipmentKeyBinding</tt>, are 107 defined by extending the 108 <a href="../../java/com/sleepycat/bind/tuple/TupleBinding.html" target="_top">TupleBinding</a> 109 110 class. The 111 <a href="../../java/com/sleepycat/bind/tuple/TupleBinding.html" target="_top">TupleBinding</a> 112 113 abstract class implements the 114 <a 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 href="../../java/com/sleepycat/bind/tuple/TupleInput.html" target="_top">TupleInput</a> 120 121 parameter and written using the 122 <a 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"><b class="userinput"><tt>import com.sleepycat.bind.tuple.TupleBinding; 128import com.sleepycat.bind.tuple.TupleInput; 129import com.sleepycat.bind.tuple.TupleOutput; 130...</tt></b> 131public class SampleViews 132{ 133... 134 135 <b class="userinput"><tt>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 }</tt></b> 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">�� 214Creating Tuple-Serial Entity Bindings 215</td> 216 </tr> 217 </table> 218 </div> 219 </body> 220</html> 221