### Berkeley DB Reference Guide: Java API Tutorial - Serializable Entity |

### Serializable Entity - Using transient fields in an entity binding

The entity bindings from the prior example have been changed in this example to use the entity object both as a value object and an entity object.

Before, the dataToObject method combined the deserialized value object with the key fields to create a new entity object. Now, this method uses the deserialized object directly as an entity, and initializes its key using the fields read from the key tuple.

Before, the objectToValue method constructed a new value object using information in the entity. Now it simply returns the entity. Nothing needs to be changed in the entity, since the transient key fields won't be serialized.

>     public class SampleViews
>     {
>         ...
>         private static class PartBinding extends TupleSerialBinding
>         {
>             private PartBinding(TupleFormat keyFormat,
>                                 SerialFormat valueFormat)
>             {
>                 super(keyFormat, valueFormat);
>             }

>             public Object dataToObject(TupleInput keyInput, Object valueInput)
>                 throws IOException
>             {
>                 String number = keyInput.readString();
>                 Part part = (Part) valueInput;
>                 part.setKey(number);
>                 return part;
>             }

>             public void objectToKey(Object object, TupleOutput output)
>                 throws IOException
>             {
>                 Part part = (Part) object;
>                 output.writeString(part.getNumber());
>             }

>             public Object objectToValue(Object object)
>                 throws IOException
>             {
>                 return object;
>             }
>         }

>         private static class SupplierBinding extends TupleSerialBinding
>         {
>             private SupplierBinding(TupleFormat keyFormat,
>                                     SerialFormat valueFormat)
>             {
>                 super(keyFormat, valueFormat);
>             }

>             public Object dataToObject(TupleInput keyInput, Object valueInput)
>                 throws IOException
>             {
>                 String number = keyInput.readString();
>                 Supplier supplier = (Supplier) valueInput;
>                 supplier.setKey(number);
>                 return supplier;
>             }

>             public void objectToKey(Object object, TupleOutput output)
>                 throws IOException
>             {
>                 Supplier supplier = (Supplier) object;
>                 output.writeString(supplier.getNumber());
>             }

>             public Object objectToValue(Object object)
>                 throws IOException
>             {
>                 return object;
>             }
>         }

>         private static class ShipmentBinding extends TupleSerialBinding
>         {
>             private ShipmentBinding(TupleFormat keyFormat,
>                                     SerialFormat valueFormat)
>             {
>                 super(keyFormat, valueFormat);
>             }

>             public Object dataToObject(TupleInput keyInput, Object valueInput)
>                 throws IOException
>             {
>                 String partNumber = keyInput.readString();
>                 String supplierNumber = keyInput.readString();
>                 Shipment shipment = (Shipment) valueInput;
>                 shipment.setKey(partNumber, supplierNumber);
>                 return shipment;
>             }

>             public void objectToKey(Object object, TupleOutput output)
>                 throws IOException
>             {
>                 Shipment shipment = (Shipment) object;
>                 output.writeString(shipment.getPartNumber());
>                 output.writeString(shipment.getSupplierNumber());
>             }

>             public Object objectToValue(Object object)
>                 throws IOException
>             {
>                 return object;
>             }
>         }
>     }

Copyright (c) 1996-2003 Sleepycat Software, Inc. - All rights reserved.