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

### Defining entity classes

As described in the prior section, entity classes are combined key/value classes that are managed by entity bindings. In this example the Part, Supplier and Shipment classes are entity classes. These classes contain fields that are a union of the fields of the key and value classes that were defined earlier for each store.

In general, entity classes may be defined in any way desired by the application. The entity binding, which is also defined by the application, is responsible for mapping between key/value objects and entity objects.

The Part, Supplier and Shipment classes are defined below.

An important difference between the entity classes defined here and the key and value classes defined earlier is that the entity classes are not serializable (do not implement the Serializable interface). This is because the entity classes are not directly stored. The entity binding decomposes an entity object into key and value objects, and only the key and value objects are serialized for storage.

One advantage of using entities can already be seen in the toString method of the classes below. These return debugging output for the combined key and value, and will be used later to create a listing of the database that is more readable than in the prior examples.

>     public class Part
>     {
>         private 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;
>         }

>         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
>     {
>         private 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;
>         }

>         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
>     {
>         private String partNumber;
>         private String supplierNumber;
>         private int quantity;

>         public Shipment(String partNumber, String supplierNumber, int quantity)
>         {
>             this.partNumber = partNumber;
>             this.supplierNumber = supplierNumber;
>             this.quantity = quantity;
>         }

>         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 + '';
>         }
>     }

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