SerializableEntity.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>Chapter 6. 
		Using Serializable Entities
	</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="index.html" title="Berkeley DB Collections Tutorial" />
    <link rel="previous" href="sortedcollections.html" title="&#10;&#9;&#9;Using Sorted Collections&#10;&#9;" />
    <link rel="next" href="transientfieldsinbinding.html" title="&#10;&#9;&#9;Using Transient Fields in an Entity Binding&#10;&#9;" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Chapter 6. 
		Using Serializable Entities
	</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="sortedcollections.html">Prev</a> </td>
          <th width="60%" align="center"> </th>
          <td width="20%" align="right"> <a accesskey="n" href="transientfieldsinbinding.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="chapter" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title"><a id="SerializableEntity"></a>Chapter 6. 
		Using Serializable Entities
	</h2>
          </div>
        </div>
        <div></div>
      </div>
      <div class="toc">
        <p>
          <b>Table of Contents</b>
        </p>
        <dl>
          <dt>
            <span class="sect1">
              <a href="SerializableEntity.html#transientfieldsinclass">
		Using Transient Fields in an Entity Class
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="transientfieldsinbinding.html">
		Using Transient Fields in an Entity Binding
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="removingredundantvalueclasses.html">
		Removing the Redundant Value Classes
	</a>
            </span>
          </dt>
        </dl>
      </div>
      <p>
    In the prior examples that used entities (the Entity and Tuple examples) you
	may have noticed the redundancy between the serializable value
	classes and the entity classes. An entity class by definition
	contains all properties of the value class as well as all
	properties of the key class.
</p>
      <p>
    When using serializable values it is possible to remove this
	redundancy by changing the entity class in two ways:
</p>
      <div class="itemizedlist">
        <ul type="disc">
          <li>
            <p>
            Make the entity class serializable, so it can be used in place
            of the value class.
        </p>
          </li>
          <li>
            <p>
            Make the key fields transient, so they are not redundantly
            stored in the record.
        </p>
          </li>
        </ul>
      </div>
      <p>
    The modified entity class can then serve double-duty: It can be
	serialized and stored as the record value, and it can be used as
	the entity class as usual along with the Java collections API. The
	<tt class="classname">PartData</tt>, <tt class="classname">SupplierData</tt> and <tt class="classname">ShipmentData</tt>
	classes can then be removed.
</p>
      <p>
    Transient fields are defined in Java as fields that are not
	stored in the serialized form of an object. Therefore, when an
	object is deserialized the transient fields must be explicitly
	initialized. Since the entity binding is responsible for creating
	entity objects, it is the natural place to initialize the transient
	key fields.
</p>
      <p>
    Note that it is not strictly necessary to make the key fields of
	a serializable entity class transient. If this is not done, the key
	will simply be stored redundantly in the record's value. This extra
	storage may or may not be acceptable to an application. But since
	we are using tuple keys and an entity binding class must be
	implemented anyway to extract the key from the entity, it is
	sensible to use transient key fields to reduce the record size. Of
	course there may be a reason that transient fields are not desired;
	for example, if an application wants to serialize the entity
	objects for other purposes, then using transient fields should be
	avoided.
</p>
      <p>
    The complete source of the final version of the example program
	is included in the Berkeley DB distribution.
</p>
      <div class="sect1" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h2 class="title" style="clear: both"><a id="transientfieldsinclass"></a>
		Using Transient Fields in an Entity Class
	</h2>
            </div>
          </div>
          <div></div>
        </div>
        <p>
        The entity classes in this example are redefined such that they
        can be used both as serializable value classes and as entity
	    classes. Compared to the prior example there are three changes to
	    the <tt class="classname">Part</tt>, <tt class="classname">Supplier</tt> and <tt class="classname">Shipment</tt> entity
	    classes:
    </p>
        <div class="itemizedlist">
          <ul type="disc">
            <li>
              <p>
            Each class now implements the <tt class="classname">Serializable</tt>
            interface.
        </p>
            </li>
            <li>
              <p>
            The key fields in each class are declared as <tt class="literal">transient</tt>.
        </p>
            </li>
            <li>
              <p>
            A package-private <tt class="methodname">setKey()</tt> method is added to each class
	        for initializing the transient key fields. This method will be
	        called from the entity bindings.
        </p>
            </li>
          </ul>
        </div>
        <a id="sentity_part"></a>
        <pre class="programlisting"><b class="userinput"><tt>import java.io.Serializable;</tt></b>
...
public class Part <b class="userinput"><tt>implements Serializable</tt></b>
{
    private <b class="userinput"><tt>transient</tt></b> String number;
    private String name;
    private String color;
    private Weight weight;
    private String city;

    public Part(String number, String name, String color, Weight weight,
                String city)
    {
        this.number = number;
        this.name = name;
        this.color = color;
        this.weight = weight;
        this.city = city;
    }

<b class="userinput"><tt>    final void setKey(String number)
    {
        this.number = number;
    }</tt></b>

    public final String getNumber()
    {
        return number;
    }

    public final String getName()
    {
        return name;
    }

    public final String getColor()
    {
        return color;
    }

    public final Weight getWeight()
    {
        return weight;
    }

    public final String getCity()
    {
        return city;
    }

    public String toString()
    {
        return "Part: number=" + number +
               " name=" + name +
               " color=" + color +
               " weight=" + weight +
               " city=" + city + '.';
    }
}
...
public class Supplier <b class="userinput"><tt>implements Serializable</tt></b>
{
    private <b class="userinput"><tt>transient</tt></b> String number;
    private String name;
    private int status;
    private String city;

    public Supplier(String number, String name, int status, String city)
    {
        this.number = number;
        this.name = name;
        this.status = status;
        this.city = city;
    }

<b class="userinput"><tt>    void setKey(String number)
    {
        this.number = number;
    }</tt></b>

    public final String getNumber()
    {
        return number;
    }

    public final String getName()
    {
        return name;
    }

    public final int getStatus()
    {
        return status;
    }

    public final String getCity()
    {
        return city;
    }

    public String toString()
    {
        return "Supplier: number=" + number +
               " name=" + name +
               " status=" + status +
               " city=" + city + '.';
    }
}
...
public class Shipment <b class="userinput"><tt>implements Serializable</tt></b>
{
    private <b class="userinput"><tt>transient</tt></b> String partNumber;
    private <b class="userinput"><tt>transient</tt></b> String supplierNumber;
    private int quantity;

    public Shipment(String partNumber, String supplierNumber, int quantity)
    {
        this.partNumber = partNumber;
        this.supplierNumber = supplierNumber;
        this.quantity = quantity;
    }

<b class="userinput"><tt>    void setKey(String partNumber, String supplierNumber)
    {
        this.partNumber = partNumber;
        this.supplierNumber = supplierNumber;
    } </tt></b>

    public final String getPartNumber()
    {
        return partNumber;
    }

    public final String getSupplierNumber()
    {
        return supplierNumber;
    }

    public final int getQuantity()
    {
        return quantity;
    }

    public String toString()
    {
        return "Shipment: part=" + partNumber +
                " supplier=" + supplierNumber +
                " quantity=" + quantity + '.';
    }
}
	</pre>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="sortedcollections.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="index.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="transientfieldsinbinding.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">
		Using Sorted Collections
	 </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> 
		Using Transient Fields in an Entity Binding
	</td>
        </tr>
      </table>
    </div>
  </body>
</html>