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

### Creating collections with entity bindings

Stored map objects are created in this example in the same way as in prior examples, but using entity bindings in place of value bindings. All value objects passed and returned to the Java collections API are then actually entity objects (Part, Supplier and Shipment). The application no longer deals directly with plain value objects (PartValue, SupplierValue and ShipmentValue).

Since the partValueBinding, supplierValueBinding and shipmentValueBinding were defined as entity bindings in the prior section, there are no source code changes necessary for creating the stored map objects.

>     public class SampleViews
>     {
>         ...
>         public SampleViews(SampleDatabase db)
>         {
>             ...
>             partMap =
>                 new StoredMap(db.getPartStore(),
>                               partKeyBinding, partValueBinding, true);
>             supplierMap =
>                 new StoredMap(db.getSupplierStore(),
>                               supplierKeyBinding, supplierValueBinding, true);
>             shipmentMap =
>                 new StoredMap(db.getShipmentStore(),
>                               shipmentKeyBinding, shipmentValueBinding, true);
>         }

Specifying an EntityBinding will select a different StoredMap constructor, but the syntax is the same. In general, an entity binding may be used anywhere that a value binding is used.

The following getter methods are defined for use by other classes in the example program. Instead of returning the map's entry set ( Map.entrySet ), the map's value set ( Map.values ) is returned. The entry set was convenient in prior examples because it allowed enumerating all key/value pairs in the collection. Since an entity contains the key and the value, enumerating the value set can now be used more conveniently for the same purpose.

>     public class SampleViews
>     {
>         ...
>         public StoredValueSet getPartSet()
>         {
>             return (StoredValueSet) partMap.values();
>         }

>         public StoredValueSet getSupplierSet()
>         {
>             return (StoredValueSet) supplierMap.values();
>         }

>         public StoredValueSet getShipmentSet()
>         {
>             return (StoredValueSet) shipmentMap.values();
>         }
>         ...
>     }

Notice that the collection returned by the StoredMap.values method is actually a StoredValueSet and not just a Collection as defined by the Map.values interface. As long as duplicate keys are not allowed, this collection will behave as a true set and will disallow the addition of duplicates, etc.

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