1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Creating Collections with Entity Bindings</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Berkeley DB Collections Tutorial" />
<link rel="up" href="Entity.html" title="Chapter 4. Using Entity Classes" />
<link rel="prev" href="creatingentitybindings.html" title="Creating Entity Bindings" />
<link rel="next" href="entitieswithcollections.html" title="Using Entities with Collections" />
</head>
<body>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">
Creating Collections with Entity Bindings
</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="creatingentitybindings.html">Prev</a> </td>
<th width="60%" align="center">Chapter 4.
Using Entity Classes
</th>
<td width="20%" align="right"> <a accesskey="n" href="entitieswithcollections.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="collectionswithentities"></a>
Creating Collections with Entity Bindings
</h2>
</div>
</div>
</div>
<p>
Stored map objects are created in this example in the same way
as in prior examples, but using entity bindings in place of value
bindings. All value objects passed and returned to the Java
collections API are then actually entity objects (<code class="classname">Part</code>,
<code class="classname">Supplier</code> and <code class="classname">Shipment</code>). The application no longer
deals directly with plain value objects (<code class="classname">PartData</code>,
<code class="classname">SupplierData</code> and <code class="classname">ShipmentData</code>).
</p>
<p>
Since the <code class="literal">partValueBinding</code>, <code class="literal">supplierValueBinding</code>
and <code class="literal">shipmentValueBinding</code> were defined as entity bindings in
the prior section, there are no source code changes necessary for
creating the stored map objects.
</p>
<a id="entity_sampleviews2"></a>
<pre class="programlisting">public class SampleViews
{
...
public SampleViews(SampleDatabase db)
{
...
partMap =
new StoredMap(db.getPartDatabase(),
partKeyBinding, partValueBinding, true);
supplierMap =
new StoredMap(db.getSupplierDatabase(),
supplierKeyBinding, supplierValueBinding, true);
shipmentMap =
new StoredMap(db.getShipmentDatabase(),
shipmentKeyBinding, shipmentValueBinding, true);
...
} </pre>
<p>
Specifying an
<a class="ulink" href="../../java/com/sleepycat/bind/EntityBinding.html" target="_top">EntityBinding</a>
will select a different
<a class="ulink" href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a>
constructor, but the syntax is the same. In general, an entity
binding may be used anywhere that a value binding is used.
</p>
<p>
The following getter methods are defined for use by other
classes in the example program. Instead of returning the map's
entry set
(<a class="ulink" href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#entrySet" target="_top">Map.entrySet</a>),
the map's value set
(<a class="ulink" href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#values" target="_top">Map.values</a>)
is returned. The entry set was convenient in prior examples because
it allowed enumerating all key/value pairs in the collection. Since
an entity contains the key and the value, enumerating the value set
can now be used more conveniently for the same purpose.
</p>
<a id="entity_sampleviews3"></a>
<pre class="programlisting"><strong class="userinput"><code>import com.sleepycat.collections.StoredValueSet;</code></strong>
...
public class SampleViews
{
...
<strong class="userinput"><code> public StoredValueSet getPartSet()
{
return (StoredValueSet) partMap.values();
}
public StoredValueSet getSupplierSet()
{
return (StoredValueSet) supplierMap.values();
}
public StoredValueSet getShipmentSet()
{
return (StoredValueSet) shipmentMap.values();
}</code></strong>
...
} </pre>
<p>
Notice that the collection returned by the
<a class="ulink" href="../../java/com/sleepycat/collections/StoredMap.html#values()" target="_top">StoredMap.values</a>
method is actually a
<a class="ulink" href="../../java/com/sleepycat/collections/StoredValueSet.html" target="_top">StoredValueSet</a>
and not just a
<a class="ulink" href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a>
as defined by the
<a class="ulink" href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#values" target="_top">Map.values</a>
interface. As long as duplicate keys are not allowed, this
collection will behave as a true set and will disallow the addition
of duplicates, etc.
</p>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="creatingentitybindings.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="Entity.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="entitieswithcollections.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">
Creating Entity Bindings
</td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top">
Using Entities with Collections
</td>
</tr>
</table>
</div>
</body>
</html>
|