###

Berkeley DB Reference Guide:
  Java API Tutorial - Serializable Entity

### Serializable Entity - One serializable class for entities and values

In the prior examples that used entities (Entity and Tuple) you may have noticed the redundancy between the serializable value classes and the entity classes. An entity class by definition contains all properties of the value class as well as all properties of the key class.

When using serializable values it is possible to remove this redundancy by changing the entity class in two ways:

- Make the entity class serializable, so it can be used in place of the value class.
- Make the key fields transient, so they are not redundantly stored in the record.

The modified entity class can then serve double-duty: It can be serialized and stored as the record value, and it can be used as the entity class as usual along with the Java collections API. The PartValue, SupplierValue and ShipmentValue classes can then be removed.

The example program illustrates:

- Using transient fields in an entity class
- Using transient fields in an entity binding
- Removing the redundant value classes

Transient fields are defined in Java as fields that are not stored in the serialized form of an object. Therefore, when an object is deserialized the transient fields must be explicitly initialized. Since the entity binding is responsible for creating entity objects, it is the natural place to initialize the transient key fields.

Note that it is not strictly necessary to make the key fields of a serializable entity class transient. If this is not done, the key will simply be stored redundantly in the record's value. This extra storage may or may not be acceptable to an application. But since we are using tuple keys and an entity binding class must be implemented anyway to extract the key from the entity, it is sensible to use transient key fields to reduce the record size. Of course there may be a reason that transient fields are not desired; for example, if an application wants to serialize the entity objects for other purposes, then using transient fields should be avoided.

The complete source of the final version of the example program is included in the Berkeley DB distribution.

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