collectionswithentities.html   [plain text]


<?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.62.4" />
    <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" />
    <link rel="up" href="Entity.html" title="Chapter 4. &#10;        Using Entity Classes&#9;&#10;&#9;" />
    <link rel="previous" href="creatingentitybindings.html" title="&#10;&#9;&#9;Creating Entity Bindings&#10;&#9;" />
    <link rel="next" href="entitieswithcollections.html" title="&#10;&#9;&#9;Using Entities with Collections&#10;&#9;" />
  </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></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 (<tt class="classname">Part</tt>,
	<tt class="classname">Supplier</tt> and <tt class="classname">Shipment</tt>). The application no longer
	deals directly with plain value objects (<tt class="classname">PartData</tt>,
	<tt class="classname">SupplierData</tt> and <tt class="classname">ShipmentData</tt>).
</p>
      <p>
    Since the <tt class="literal">partValueBinding</tt>, <tt class="literal">supplierValueBinding</tt>
	and <tt class="literal">shipmentValueBinding</tt> 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 href="../../java/com/sleepycat/bind/EntityBinding.html" target="_top">EntityBinding</a>
    
	will select a different 
    <a 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 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 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"><b class="userinput"><tt>import com.sleepycat.collections.StoredValueSet;</tt></b>
...
public class SampleViews
{
    ...
<b class="userinput"><tt>    public StoredValueSet getPartSet()
    {
        return (StoredValueSet) partMap.values();
    }

    public StoredValueSet getSupplierSet()
    {
        return (StoredValueSet) supplierMap.values();
    }

    public StoredValueSet getShipmentSet()
    {
        return (StoredValueSet) shipmentMap.values();
    }</tt></b>
    ...
} </pre>
      <p>
    Notice that the collection returned by the 
    <a href="../../java/com/sleepycat/collections/StoredMap.html#values()" target="_top">StoredMap.values</a>
    
	method is actually a 
    <a href="../../java/com/sleepycat/collections/StoredValueSet.html" target="_top">StoredValueSet</a>
    
	and not just a 
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a>
    
	as defined by the 
    <a 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>