Import BSDDB 4.7.25 (as of svn r89086)
This commit is contained in:
268
docs/collections/tutorial/creatingentitybindings.html
Normal file
268
docs/collections/tutorial/creatingentitybindings.html
Normal file
@@ -0,0 +1,268 @@
|
||||
<?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 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. Using Entity Classes	 	" />
|
||||
<link rel="previous" href="Entity.html" title="Chapter 4. Using Entity Classes	 	" />
|
||||
<link rel="next" href="collectionswithentities.html" title=" 		Creating Collections with Entity Bindings 	" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="navheader">
|
||||
<table width="100%" summary="Navigation header">
|
||||
<tr>
|
||||
<th colspan="3" align="center">
|
||||
Creating Entity Bindings
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="20%" align="left"><a accesskey="p" href="Entity.html">Prev</a> </td>
|
||||
<th width="60%" align="center">Chapter 4.
|
||||
Using Entity Classes
|
||||
</th>
|
||||
<td width="20%" align="right"> <a accesskey="n" href="collectionswithentities.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="creatingentitybindings"></a>
|
||||
Creating Entity Bindings
|
||||
</h2>
|
||||
</div>
|
||||
</div>
|
||||
<div></div>
|
||||
</div>
|
||||
<p>
|
||||
<span class="emphasis"><em>Entity bindings</em></span> are similar to ordinary bindings in that
|
||||
they convert between Java objects and the stored data format of
|
||||
keys and values. In addition, entity bindings map between key/value
|
||||
pairs and entity objects. An ordinary binding is a one-to-one
|
||||
mapping, while an entity binding is a two-to-one mapping.
|
||||
</p>
|
||||
<p>
|
||||
The <tt class="literal">partValueBinding</tt>, <tt class="literal">supplierValueBinding</tt> and
|
||||
<tt class="literal">shipmentValueBinding</tt> bindings are created below as entity
|
||||
bindings rather than (in the prior examples) serial bindings.
|
||||
</p>
|
||||
<a id="entity_sampleviews"></a>
|
||||
<pre class="programlisting">import com.sleepycat.bind.EntryBinding;
|
||||
<b class="userinput"><tt>import com.sleepycat.bind.EntityBinding;</tt></b>
|
||||
import com.sleepycat.bind.serial.SerialBinding;
|
||||
<b class="userinput"><tt>import com.sleepycat.bind.serial.SerialSerialBinding;</tt></b>
|
||||
...
|
||||
|
||||
public class SampleViews
|
||||
{
|
||||
...
|
||||
public SampleViews(SampleDatabase db)
|
||||
{
|
||||
ClassCatalog catalog = db.getClassCatalog();
|
||||
SerialBinding partKeyBinding =
|
||||
new SerialBinding(catalog, PartKey.class);
|
||||
<b class="userinput"><tt> EntityBinding partValueBinding =
|
||||
new PartBinding(catalog, PartKey.class, PartData.class);</tt></b>
|
||||
SerialBinding supplierKeyBinding =
|
||||
new SerialBinding(catalog, SupplierKey.class);
|
||||
<b class="userinput"><tt> EntityBinding supplierValueBinding =
|
||||
new SupplierBinding(catalog, SupplierKey.class,
|
||||
SupplierData.class);</tt></b>
|
||||
SerialBinding shipmentKeyBinding =
|
||||
new SerialBinding(catalog, ShipmentKey.class);
|
||||
<b class="userinput"><tt> EntityBinding shipmentValueBinding =
|
||||
new ShipmentBinding(catalog, ShipmentKey.class,
|
||||
ShipmentData.class);</tt></b>
|
||||
SerialBinding cityKeyBinding =
|
||||
new SerialBinding(catalog, String.class);
|
||||
...
|
||||
}
|
||||
} </pre>
|
||||
<p>
|
||||
The entity bindings will be used in the next section to
|
||||
construct stored map objects.
|
||||
</p>
|
||||
<p>
|
||||
The <tt class="classname">PartBinding</tt> class is defined below.
|
||||
</p>
|
||||
<a id="entity_partbinding"></a>
|
||||
<pre class="programlisting">public class SampleViews
|
||||
{
|
||||
...
|
||||
<b class="userinput"><tt> private static class PartBinding extends SerialSerialBinding {
|
||||
private PartBinding(ClassCatalog classCatalog,
|
||||
Class keyClass,
|
||||
Class dataClass)
|
||||
{
|
||||
super(classCatalog, keyClass, dataClass);
|
||||
}
|
||||
|
||||
public Object entryToObject(Object keyInput, Object dataInput)
|
||||
{
|
||||
PartKey key = (PartKey) keyInput;
|
||||
PartData data = (PartData) dataInput;
|
||||
return new Part(key.getNumber(), data.getName(), data.getColor(),
|
||||
data.getWeight(), data.getCity());
|
||||
}
|
||||
|
||||
public Object objectToKey(Object object)
|
||||
{
|
||||
Part part = (Part) object;
|
||||
return new PartKey(part.getNumber());
|
||||
}
|
||||
|
||||
public Object objectToData(Object object)
|
||||
{
|
||||
Part part = (Part) object;
|
||||
return new PartData(part.getName(), part.getColor(),
|
||||
part.getWeight(), part.getCity());
|
||||
}
|
||||
}</tt></b>
|
||||
...
|
||||
} </pre>
|
||||
<p>
|
||||
In general, an entity binding is any class that implements the
|
||||
<a href="../../java/com/sleepycat/bind/EntityBinding.html" target="_top">EntityBinding</a>
|
||||
|
||||
interface, just as an ordinary binding is any class that implements
|
||||
the
|
||||
<a href="../../java/com/sleepycat/bind/EntryBinding.html" target="_top">EntryBinding</a>
|
||||
|
||||
interface. In the prior examples the built-in
|
||||
<a href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
|
||||
|
||||
class (which implements
|
||||
<a href="../../java/com/sleepycat/bind/EntryBinding.html" target="_top">EntryBinding</a>)
|
||||
was used and no application-defined binding classes were needed.
|
||||
</p>
|
||||
<p>
|
||||
In this example, application-defined binding classes are used
|
||||
that extend the
|
||||
<a href="../../java/com/sleepycat/bind/serial/SerialSerialBinding.html" target="_top">SerialSerialBinding</a>
|
||||
|
||||
abstract base class. This base class implements
|
||||
<a href="../../java/com/sleepycat/bind/EntityBinding.html" target="_top">EntityBinding</a>
|
||||
|
||||
and provides the conversions between key/value bytes and key/value
|
||||
objects, just as the
|
||||
<a href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
|
||||
|
||||
class does. The application-defined entity class implements the
|
||||
abstract methods defined in the base class that map between
|
||||
key/value objects and entity objects.
|
||||
</p>
|
||||
<p>
|
||||
Three abstract methods are implemented for each entity binding.
|
||||
The <tt class="methodname">entryToObject()</tt> method takes as input the key and data
|
||||
objects, which have been deserialized automatically by the base
|
||||
class. As output, it returns the combined <tt class="classname">Part</tt> entity.
|
||||
</p>
|
||||
<p>
|
||||
The <tt class="methodname">objectToKey()</tt> and <tt class="methodname">objectToData()</tt> methods take an
|
||||
entity object as input. As output they return the part key or data
|
||||
object that is extracted from the entity object. The key or data
|
||||
will then be serialized automatically by the base class.
|
||||
</p>
|
||||
<p>
|
||||
The <tt class="classname">SupplierBinding</tt> and <tt class="classname">ShipmentBinding</tt> classes
|
||||
are very similar to the <tt class="classname">PartBinding</tt> class.
|
||||
</p>
|
||||
<a id="entity_supplierbinding"></a>
|
||||
<pre class="programlisting">public class SampleViews
|
||||
{
|
||||
...
|
||||
<b class="userinput"><tt> private static class SupplierBinding extends SerialSerialBinding {
|
||||
private SupplierBinding(ClassCatalog classCatalog,
|
||||
Class keyClass,
|
||||
Class dataClass)
|
||||
{
|
||||
super(classCatalog, keyClass, dataClass);
|
||||
}
|
||||
|
||||
public Object entryToObject(Object keyInput, Object dataInput)
|
||||
{
|
||||
SupplierKey key = (SupplierKey) keyInput;
|
||||
SupplierData data = (SupplierData) dataInput;
|
||||
return new Supplier(key.getNumber(), data.getName(),
|
||||
data.getStatus(), data.getCity());
|
||||
}
|
||||
|
||||
public Object objectToKey(Object object)
|
||||
{
|
||||
Supplier supplier = (Supplier) object;
|
||||
return new SupplierKey(supplier.getNumber());
|
||||
}
|
||||
|
||||
public Object objectToData(Object object)
|
||||
{
|
||||
Supplier supplier = (Supplier) object;
|
||||
return new SupplierData(supplier.getName(), supplier.getStatus(),
|
||||
supplier.getCity());
|
||||
}
|
||||
}
|
||||
|
||||
private static class ShipmentBinding extends SerialSerialBinding {
|
||||
private ShipmentBinding(ClassCatalog classCatalog,
|
||||
Class keyClass,
|
||||
Class dataClass)
|
||||
{
|
||||
super(classCatalog, keyClass, dataClass);
|
||||
}
|
||||
|
||||
public Object entryToObject(Object keyInput, Object dataInput)
|
||||
{
|
||||
ShipmentKey key = (ShipmentKey) keyInput;
|
||||
ShipmentData data = (ShipmentData) dataInput;
|
||||
return new Shipment(key.getPartNumber(), key.getSupplierNumber(),
|
||||
data.getQuantity());
|
||||
}
|
||||
|
||||
public Object objectToKey(Object object)
|
||||
{
|
||||
Shipment shipment = (Shipment) object;
|
||||
return new ShipmentKey(shipment.getPartNumber(),
|
||||
shipment.getSupplierNumber());
|
||||
}
|
||||
|
||||
public Object objectToData(Object object)
|
||||
{
|
||||
Shipment shipment = (Shipment) object;
|
||||
return new ShipmentData(shipment.getQuantity());
|
||||
}
|
||||
}</tt></b>
|
||||
...
|
||||
} </pre>
|
||||
</div>
|
||||
<div class="navfooter">
|
||||
<hr />
|
||||
<table width="100%" summary="Navigation footer">
|
||||
<tr>
|
||||
<td width="40%" align="left"><a accesskey="p" href="Entity.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="collectionswithentities.html">Next</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="40%" align="left" valign="top">Chapter 4.
|
||||
Using Entity Classes
|
||||
</td>
|
||||
<td width="20%" align="center">
|
||||
<a accesskey="h" href="index.html">Home</a>
|
||||
</td>
|
||||
<td width="40%" align="right" valign="top">
|
||||
Creating Collections with Entity Bindings
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user