Library Version 18.1.25

Creating Tuple-Serial Entity Bindings
Prev | Chapter 5. Using Tuples | Next

## Creating Tuple-Serial Entity Bindings

In the prior example serial keys and serial values were used, and the SerialSerialBinding base class was used for entity bindings. In this example, tuple keys and serial values are used and therefore the TupleSerialBinding base class is used for entity bindings.

As with any entity binding, a key and value is converted to an entity in the TupleSerialBinding.entryToObject method, and from an entity to a key and value in the TupleSerialBinding.objectToKey and TupleSerialBinding.objectToData methods. But since keys are stored as tuples, not as serialized objects, key fields are read and written using the TupleInput and TupleOutput parameters.

The `SampleViews` class contains the modified entity binding classes that were defined in the prior example: `PartBinding`, `SupplierBinding` and `ShipmentBinding`.

    import com.sleepycat.bind.serial.TupleSerialBinding;
    import com.sleepycat.bind.tuple.TupleInput;
    import com.sleepycat.bind.tuple.TupleOutput;
    ...
    public class SampleViews
    {
        ...
        private static class PartBinding extends TupleSerialBinding
        {
            private PartBinding(ClassCatalog classCatalog, Class dataClass)
            {
                super(classCatalog, dataClass);
            }
            public Object entryToObject(TupleInput keyInput, Object dataInput)
            {
                String number = keyInput.readString();
                PartData data = (PartData) dataInput;
                return new Part(number, data.getName(), data.getColor(),
                                data.getWeight(), data.getCity());
            }
            public void objectToKey(Object object, TupleOutput output)
            {
                Part part = (Part) object;
                output.writeString(part.getNumber());
            }
            public Object objectToData(Object object)
            {
                Part part = (Part) object;
                return new PartData(part.getName(), part.getColor(),
                                     part.getWeight(), part.getCity());
            }
        }
        ...
        private static class SupplierBinding extends TupleSerialBinding
        {
            private SupplierBinding(ClassCatalog classCatalog, Class dataClass)
            {
                super(classCatalog, dataClass);
            }
            public Object entryToObject(TupleInput keyInput, Object dataInput)
            {
                String number = keyInput.readString();
                SupplierData data = (SupplierData) dataInput;
                return new Supplier(number, data.getName(),
                                    data.getStatus(), data.getCity());
            }
            public void objectToKey(Object object, TupleOutput output)
            {
                Supplier supplier = (Supplier) object;
                output.writeString(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 TupleSerialBinding
        {
            private ShipmentBinding(ClassCatalog classCatalog, Class dataClass)
            {
                super(classCatalog, dataClass);
            }
            public Object entryToObject(TupleInput keyInput, Object dataInput)
            {
                String partNumber = keyInput.readString();
                String supplierNumber = keyInput.readString();
                ShipmentData data = (ShipmentData) dataInput;
                return new Shipment(partNumber, supplierNumber,
                                    data.getQuantity());
            }
            public void objectToKey(Object object, TupleOutput output)
            {
                Shipment shipment = (Shipment) object;
                output.writeString(shipment.getPartNumber());
                output.writeString(shipment.getSupplierNumber());
            }
            public Object objectToData(Object object)
            {
                Shipment shipment = (Shipment) object;
                return new ShipmentData(shipment.getQuantity());
            }
        }
        ...
    }

Prev | Up | Next
Creating Tuple Key Bindings | Home | Using Sorted Collections