je/docs/collections/tutorial/collectionswithentities.html
2021-06-06 13:46:45 -04:00

165 lines
6.6 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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 Java Edition 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 xmlns="" class="navheader">
<div class="libver">
<p>Library Version 12.2.7.5</p>
</div>
<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">partDataBinding</code>, <code class="literal">supplierDataBinding</code>
and <code class="literal">shipmentDataBinding</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 StoredSortedMap(db.getPartDatabase(),
partKeyBinding, partDataBinding, true);
supplierMap =
new StoredSortedMap(db.getSupplierDatabase(),
supplierKeyBinding, supplierDataBinding, true);
shipmentMap =
new StoredSortedMap(db.getShipmentDatabase(),
shipmentKeyBinding, shipmentDataBinding, 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">StoredSortedMap</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://download.oracle.com/javase/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://download.oracle.com/javase/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">StoredSortedMap.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://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a>
as defined by the
<a class="ulink" href="http://download.oracle.com/javase/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>