Chapter 6. A DPL Example

Table of Contents

Vendor.java
Inventory.java
MyDbEnv
DataAccessor.java
ExampleDatabasePut.java
ExampleInventoryRead.java

In order to illustrate DPL usage, we provide a complete working example in this chapter. This example reads and writes inventory and vendor information for a mythical business. The application consists of the following classes:

Be aware that this example can be found in your JE distribution in the following location:

JE_HOME/examples/persist/gettingStarted

where JE_HOME is the location where you placed your JE distribution.

Vendor.java

The simplest class that our example wants to store contains vendor contact information. This class contains no secondary indices so all we have to do is identify it as an entity class and identify the field in the class used for the primary key.

In the following example, we identify the vendor data member as containing the primary key. This data member is meant to contain a vendor's name. Because of the way we will use our EntityStore, the value provided for this data member must be unique within the store or runtime errors will result.

When used with the DPL, our Vendor class appears as follows. Notice that the @Entity annotation appears immediately before the class declaration, and the @PrimaryKey annotation appears immediately before the vendor data member declaration.

package persist.gettingStarted;

import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;

@Entity
public class Vendor {

    private String address;
    private String bizPhoneNumber;
    private String city;
    private String repName;
    private String repPhoneNumber;
    private String state;

    // Primary key is the vendor's name
    // This assumes that the vendor's name is
    // unique in the database.
    @PrimaryKey
    private String vendor;

    private String zipcode;

    public void setRepName(String data) {
        repName = data;
    }

    public void setAddress(String data) {
        address = data;
    }

    public void setCity(String data) {
        city = data;
    }

    public void setState(String data) {
        state = data;
    }

    public void setZipcode(String data) {
        zipcode = data;
    }

    public void setBusinessPhoneNumber(String data) {
        bizPhoneNumber = data;
    }

    public void setRepPhoneNumber(String data) {
        repPhoneNumber = data;
    }

    public void setVendorName(String data) {
        vendor = data;
    }

    public String getRepName() {
        return repName;
    }

    public String getAddress() {
        return address;
    }

    public String getCity() {
        return city;
    }

    public String getState() {
        return state;
    }

    public String getZipcode() {
        return zipcode;
    }

    public String getBusinessPhoneNumber() {
        return bizPhoneNumber;
    }

    public String getRepPhoneNumber() {
        return repPhoneNumber;
    }
} 

For this class, the vendor value is set for an individual Vendor class object by the setVendorName() method. If our example code fails to set this value before storing the object, the data member used to store the primary key is set to a null value. This would result in a runtime error.