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 Collections with 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="Entity.html" title="Chapter��4.�� Using Entity Classes	 	" /> 13 <link rel="previous" href="creatingentitybindings.html" title=" 		Creating Entity Bindings 	" /> 14 <link rel="next" href="entitieswithcollections.html" title=" 		Using Entities with Collections 	" /> 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 Collections with Entity Bindings 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="creatingentitybindings.html">Prev</a>��</td> 26 <th width="60%" align="center">Chapter��4.�� 27 Using Entity Classes 28 </th> 29 <td width="20%" align="right">��<a accesskey="n" href="entitieswithcollections.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="collectionswithentities"></a> 39 Creating Collections with Entity Bindings 40 </h2> 41 </div> 42 </div> 43 <div></div> 44 </div> 45 <p> 46 Stored map objects are created in this example in the same way 47 as in prior examples, but using entity bindings in place of value 48 bindings. All value objects passed and returned to the Java 49 collections API are then actually entity objects (<tt class="classname">Part</tt>, 50 <tt class="classname">Supplier</tt> and <tt class="classname">Shipment</tt>). The application no longer 51 deals directly with plain value objects (<tt class="classname">PartData</tt>, 52 <tt class="classname">SupplierData</tt> and <tt class="classname">ShipmentData</tt>). 53</p> 54 <p> 55 Since the <tt class="literal">partValueBinding</tt>, <tt class="literal">supplierValueBinding</tt> 56 and <tt class="literal">shipmentValueBinding</tt> were defined as entity bindings in 57 the prior section, there are no source code changes necessary for 58 creating the stored map objects. 59</p> 60 <a id="entity_sampleviews2"></a> 61 <pre class="programlisting">public class SampleViews 62{ 63 ... 64 public SampleViews(SampleDatabase db) 65 { 66 ... 67 partMap = 68 new StoredMap(db.getPartDatabase(), 69 partKeyBinding, partValueBinding, true); 70 supplierMap = 71 new StoredMap(db.getSupplierDatabase(), 72 supplierKeyBinding, supplierValueBinding, true); 73 shipmentMap = 74 new StoredMap(db.getShipmentDatabase(), 75 shipmentKeyBinding, shipmentValueBinding, true); 76 ... 77 } </pre> 78 <p> 79 Specifying an 80 <a href="../../java/com/sleepycat/bind/EntityBinding.html" target="_top">EntityBinding</a> 81 82 will select a different 83 <a href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a> 84 85 constructor, but the syntax is the same. In general, an entity 86 binding may be used anywhere that a value binding is used. 87</p> 88 <p> 89 The following getter methods are defined for use by other 90 classes in the example program. Instead of returning the map's 91 entry set 92 (<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#entrySet" target="_top">Map.entrySet</a>), 93 the map's value set 94 (<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#values" target="_top">Map.values</a>) 95 is returned. The entry set was convenient in prior examples because 96 it allowed enumerating all key/value pairs in the collection. Since 97 an entity contains the key and the value, enumerating the value set 98 can now be used more conveniently for the same purpose. 99</p> 100 <a id="entity_sampleviews3"></a> 101 <pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.collections.StoredValueSet;</tt></b> 102... 103public class SampleViews 104{ 105 ... 106<b class="userinput"><tt> public StoredValueSet getPartSet() 107 { 108 return (StoredValueSet) partMap.values(); 109 } 110 111 public StoredValueSet getSupplierSet() 112 { 113 return (StoredValueSet) supplierMap.values(); 114 } 115 116 public StoredValueSet getShipmentSet() 117 { 118 return (StoredValueSet) shipmentMap.values(); 119 }</tt></b> 120 ... 121} </pre> 122 <p> 123 Notice that the collection returned by the 124 <a href="../../java/com/sleepycat/collections/StoredMap.html#values()" target="_top">StoredMap.values</a> 125 126 method is actually a 127 <a href="../../java/com/sleepycat/collections/StoredValueSet.html" target="_top">StoredValueSet</a> 128 129 and not just a 130 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a> 131 132 as defined by the 133 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#values" target="_top">Map.values</a> 134 135 interface. As long as duplicate keys are not allowed, this 136 collection will behave as a true set and will disallow the addition 137 of duplicates, etc. 138</p> 139 </div> 140 <div class="navfooter"> 141 <hr /> 142 <table width="100%" summary="Navigation footer"> 143 <tr> 144 <td width="40%" align="left"><a accesskey="p" href="creatingentitybindings.html">Prev</a>��</td> 145 <td width="20%" align="center"> 146 <a accesskey="u" href="Entity.html">Up</a> 147 </td> 148 <td width="40%" align="right">��<a accesskey="n" href="entitieswithcollections.html">Next</a></td> 149 </tr> 150 <tr> 151 <td width="40%" align="left" valign="top"> 152 Creating Entity Bindings 153 ��</td> 154 <td width="20%" align="center"> 155 <a accesskey="h" href="index.html">Home</a> 156 </td> 157 <td width="40%" align="right" valign="top">�� 158 Using Entities with Collections 159 </td> 160 </tr> 161 </table> 162 </div> 163 </body> 164</html> 165