mirror of
https://github.com/berkeleydb/je.git
synced 2024-11-15 01:46:24 +00:00
345 lines
10 KiB
HTML
345 lines
10 KiB
HTML
<?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.73.2" />
|
||
<link rel="start" href="index.html" title="Berkeley DB Java Edition Collections Tutorial" />
|
||
<link rel="up" href="index.html" title="Berkeley DB Java Edition Collections Tutorial" />
|
||
<link rel="prev" href="sortedcollections.html" title="Using Sorted Collections" />
|
||
<link rel="next" href="transientfieldsinbinding.html" title="Using Transient Fields in an Entity Binding" />
|
||
</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">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 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
|
||
<code class="classname">PartData</code>, <code class="classname">SupplierData</code> and <code class="classname">ShipmentData</code>
|
||
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>
|
||
<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 <code class="classname">Part</code>, <code class="classname">Supplier</code> and <code class="classname">Shipment</code> entity
|
||
classes:
|
||
</p>
|
||
<div class="itemizedlist">
|
||
<ul type="disc">
|
||
<li>
|
||
<p>
|
||
Each class now implements the <code class="classname">Serializable</code>
|
||
interface.
|
||
</p>
|
||
</li>
|
||
<li>
|
||
<p>
|
||
The key fields in each class are declared as <code class="literal">transient</code>.
|
||
</p>
|
||
</li>
|
||
<li>
|
||
<p>
|
||
A package-private <code class="methodname">setKey()</code> 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"><strong class="userinput"><code>import java.io.Serializable;</code></strong>
|
||
...
|
||
public class Part <strong class="userinput"><code>implements Serializable</code></strong>
|
||
{
|
||
private <strong class="userinput"><code>transient</code></strong> 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;
|
||
}
|
||
|
||
<strong class="userinput"><code> final void setKey(String number)
|
||
{
|
||
this.number = number;
|
||
}</code></strong>
|
||
|
||
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 <strong class="userinput"><code>implements Serializable</code></strong>
|
||
{
|
||
private <strong class="userinput"><code>transient</code></strong> 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;
|
||
}
|
||
|
||
<strong class="userinput"><code> void setKey(String number)
|
||
{
|
||
this.number = number;
|
||
}</code></strong>
|
||
|
||
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 <strong class="userinput"><code>implements Serializable</code></strong>
|
||
{
|
||
private <strong class="userinput"><code>transient</code></strong> String partNumber;
|
||
private <strong class="userinput"><code>transient</code></strong> String supplierNumber;
|
||
private int quantity;
|
||
|
||
public Shipment(String partNumber, String supplierNumber, int quantity)
|
||
{
|
||
this.partNumber = partNumber;
|
||
this.supplierNumber = supplierNumber;
|
||
this.quantity = quantity;
|
||
}
|
||
|
||
<strong class="userinput"><code> void setKey(String partNumber, String supplierNumber)
|
||
{
|
||
this.partNumber = partNumber;
|
||
this.supplierNumber = supplierNumber;
|
||
} </code></strong>
|
||
|
||
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"> </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>
|