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> 7Creating Tuple-Serial Entity 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="tuplekeybindings.html" title=" 		Creating Tuple Key Bindings 	" /> 14 <link rel="next" href="sortedcollections.html" title=" 		Using Sorted Collections 	" /> 15 </head> 16 <body> 17 <div class="navheader"> 18 <table width="100%" summary="Navigation header"> 19 <tr> 20 <th colspan="3" align="center"> 21Creating Tuple-Serial Entity Bindings 22</th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="tuplekeybindings.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="sortedcollections.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="tuple-serialentitybindings"></a> 39Creating Tuple-Serial Entity Bindings 40</h2> 41 </div> 42 </div> 43 <div></div> 44 </div> 45 <p> 46In the prior example serial keys and serial values were used, 47and the 48<a href="/java/com/sleepycat/bind/serial/SerialSerialBinding.html" target="_top">SerialSerialBinding</a> 49 50base class was used for entity bindings. In this example, tuple 51keys and serial values are used and therefore the 52<a href="/java/com/sleepycat/bind/serial/TupleSerialBinding.html" target="_top">TupleSerialBinding</a> 53 54base class is used for entity bindings. 55</p> 56 <p> 57As with any entity binding, a key and value is converted to an 58entity in the 59<a href="/java/com/sleepycat/bind/serial/TupleSerialBinding.html#entryToObject(com.sleepycat.bind.tuple.TupleInput,%20java.lang.Object)" target="_top">TupleSerialBinding.entryToObject</a> 60 61method, and from an entity to 62a key and value in the 63<a href="/java/com/sleepycat/bind/serial/TupleSerialBinding.html#objectToKey(java.lang.Object,%20com.sleepycat.db.DatabaseEntry)" target="_top">TupleSerialBinding.objectToKey</a> 64 65and 66<a href="/java/com/sleepycat/bind/serial/TupleSerialBinding.html#objectToData(java.lang.Object)" target="_top">TupleSerialBinding.objectToData</a> 67 68methods. But since keys are 69stored as tuples, not as serialized objects, key fields are read 70and written using the 71<a href="/java/com/sleepycat/bind/tuple/TupleInput.html" target="_top">TupleInput</a> 72 73and 74<a href="/java/com/sleepycat/bind/tuple/TupleOutput.html" target="_top">TupleOutput</a> 75 76parameters. 77</p> 78 <p> 79The <tt class="classname">SampleViews</tt> class contains the modified entity 80binding classes that were defined in the prior example: 81<tt class="classname">PartBinding</tt>, <tt class="classname">SupplierBinding</tt> and 82<tt class="classname">ShipmentBinding</tt>. 83</p> 84 <a id="tuple_partbinding"></a> 85 <pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.bind.serial.TupleSerialBinding; 86import com.sleepycat.bind.tuple.TupleInput; 87import com.sleepycat.bind.tuple.TupleOutput; 88...</tt></b> 89public class SampleViews 90{ 91 ... 92 private static class PartBinding extends <b class="userinput"><tt>TupleSerialBinding</tt></b> 93 { 94 private PartBinding(ClassCatalog classCatalog, <b class="userinput"><tt>Class dataClass</tt></b>) 95 { 96 super(classCatalog, dataClass); 97 } 98 public Object entryToObject(<b class="userinput"><tt>TupleInput</tt></b> keyInput, Object dataInput) 99 { 100<b class="userinput"><tt> String number = keyInput.readString();</tt></b> 101 PartData data = (PartData) dataInput; 102 return new Part(<b class="userinput"><tt>number</tt></b>, data.getName(), data.getColor(), 103 data.getWeight(), data.getCity()); 104 } 105 public void objectToKey(Object object, TupleOutput output) 106 { 107 Part part = (Part) object; 108<b class="userinput"><tt> output.writeString(part.getNumber());</tt></b> 109 } 110 public Object objectToData(Object object) 111 { 112 Part part = (Part) object; 113 return new PartData(part.getName(), part.getColor(), 114 part.getWeight(), part.getCity()); 115 } 116 } 117 ... 118 private static class SupplierBinding extends <b class="userinput"><tt>TupleSerialBinding</tt></b> 119 { 120 private SupplierBinding(ClassCatalog classCatalog, <b class="userinput"><tt>Class dataClass</tt></b>) 121 { 122 super(classCatalog, dataClass); 123 } 124 public Object entryToObject(<b class="userinput"><tt>TupleInput</tt></b> keyInput, Object dataInput) 125 { 126<b class="userinput"><tt> String number = keyInput.readString();</tt></b> 127 SupplierData data = (SupplierData) dataInput; 128 return new Supplier(<b class="userinput"><tt>number</tt></b>, data.getName(), 129 data.getStatus(), data.getCity()); 130 } 131 public void objectToKey(Object object, TupleOutput output) 132 { 133 Supplier supplier = (Supplier) object; 134<b class="userinput"><tt> output.writeString(supplier.getNumber());</tt></b> 135 } 136 public Object objectToData(Object object) 137 { 138 Supplier supplier = (Supplier) object; 139 return new SupplierData(supplier.getName(), supplier.getStatus(), 140 supplier.getCity()); 141 } 142 } 143 ... 144 private static class ShipmentBinding extends <b class="userinput"><tt>TupleSerialBinding</tt></b> 145 { 146 private ShipmentBinding(ClassCatalog classCatalog, <b class="userinput"><tt>Class dataClass</tt></b>) 147 { 148 super(classCatalog, dataClass); 149 } 150 public Object entryToObject(<b class="userinput"><tt>TupleInput</tt></b> keyInput, Object dataInput) 151 { 152<b class="userinput"><tt> String partNumber = keyInput.readString(); 153 String supplierNumber = keyInput.readString();</tt></b> 154 ShipmentData data = (ShipmentData) dataInput; 155 return new Shipment(<b class="userinput"><tt>partNumber, supplierNumber</tt></b>, 156 data.getQuantity()); 157 } 158 public void objectToKey(Object object, TupleOutput output) 159 { 160 Shipment shipment = (Shipment) object; 161<b class="userinput"><tt> output.writeString(shipment.getPartNumber()); 162 output.writeString(shipment.getSupplierNumber());</tt></b> 163 } 164 public Object objectToData(Object object) 165 { 166 Shipment shipment = (Shipment) object; 167 return new ShipmentData(shipment.getQuantity()); 168 } 169 } 170 ... 171} </pre> 172 </div> 173 <div class="navfooter"> 174 <hr /> 175 <table width="100%" summary="Navigation footer"> 176 <tr> 177 <td width="40%" align="left"><a accesskey="p" href="tuplekeybindings.html">Prev</a> </td> 178 <td width="20%" align="center"> 179 <a accesskey="u" href="Tuple.html">Up</a> 180 </td> 181 <td width="40%" align="right"> <a accesskey="n" href="sortedcollections.html">Next</a></td> 182 </tr> 183 <tr> 184 <td width="40%" align="left" valign="top"> 185 Creating Tuple Key Bindings 186 </td> 187 <td width="20%" align="center"> 188 <a accesskey="h" href="index.html">Home</a> 189 </td> 190 <td width="40%" align="right" valign="top"> 191 Using Sorted Collections 192 </td> 193 </tr> 194 </table> 195 </div> 196 </body> 197</html> 198