!752 lib 1004017 // Berkeley DB import com.sleepycat.je.DatabaseException; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; import com.sleepycat.persist.EntityStore; import com.sleepycat.persist.StoreConfig; import com.sleepycat.persist.model.Entity; import com.sleepycat.persist.model.PrimaryKey; import com.sleepycat.persist.model.SecondaryKey; import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE; import com.sleepycat.je.DatabaseException; import com.sleepycat.persist.EntityStore; import com.sleepycat.persist.PrimaryIndex; import com.sleepycat.persist.SecondaryIndex; public static class SimpleDA { // Open the indices public SimpleDA(EntityStore store) throws DatabaseException { // Primary key for ExampleEntity classes pIdx = store.getPrimaryIndex( String.class, ExampleEntity.class); // Secondary key for ExampleEntity classes // Last field in the getSecondaryIndex() method must be // the name of a class member; in this case, an // ExampleEntity.class data member. sIdx = store.getSecondaryIndex( pIdx, String.class, "aSecondaryKey"); } // Index Accessors PrimaryIndex pIdx; SecondaryIndex sIdx; } @Entity public static class ExampleEntity { // The primary key must be unique in the database. @PrimaryKey private String aPrimaryKey; @SecondaryKey(relate=MANY_TO_ONE) private String aSecondaryKey; void setPKey(S x) { aPrimaryKey = x; } void setSKey(S x) { aSecondaryKey = x; } } p { Environment myEnv; EntityStore store; bool readOnly = false; File envHome = mkdir(getProgramDir("#1004019")); print("Opening Berkely DB in " + envHome.getAbsolutePath()); EnvironmentConfig myEnvConfig = new EnvironmentConfig(); StoreConfig storeConfig = new StoreConfig(); myEnvConfig.setAllowCreate(!readOnly); storeConfig.setAllowCreate(!readOnly); // Open the environment and entity store myEnv = new Environment(envHome, myEnvConfig); store = new EntityStore(myEnv, "EntityStore", storeConfig); print("Retrieving stuff"); SimpleDA da = new SimpleDA(store); ExampleEntity sec1 = da.pIdx.get("keyone"); ExampleEntity sec2 = da.pIdx.get("keytwo"); print("keyone => " + structure(sec1)); print("keytwo => " + structure(sec2)); print("Closing"); if (store != null) store.close(); if (myEnv != null) myEnv.close(); }