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

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

The entity classes in this example are redefined such that they can be used both as serializable value classes and as entity classes. Compared to the prior example there are three changes to the Part, Supplier and Shipment entity classes:

- Each class now implements the Serializable interface.
- The key fields in each class are declared as transient.
- A package-private setKey method is added to each class for initializing the transient key fields. This method will be called from the entity bindings.

>     import java.io.Serializable;
>     ...
>     public class Part implements Serializable
>     {
>         private transient 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;
>         }

>         final void setKey(String number)
>         {
>             this.number = number;
>         }

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

>         void setKey(String number)
>         {
>             this.number = number;
>         }

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

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

>         void setKey(String partNumber, String supplierNumber)
>         {
>             this.partNumber = partNumber;
>             this.supplierNumber = supplierNumber;
>         }

>         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.