In the last blog post, we have saved and read individual objects in a real JE. In this lesson, we will challenge data accessors to save and store multiple objects.
Data accessor may be the first time you've heard about it. Don't worry, it's just encapsulating the code.
Okay, let's not say much. Let's start.
First, we need a tool class to manage the initialization and closure of our Environment and EntityStore.
The code details are as follows:
import java.io.File;
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;
/**
* Berkeley Database Java Edition
* Environment Manager
* @author fairy
* **/
public class BDBEnvironmentMangager {
private Environment myEnvironment;
private EntityStore myEntityStore;
//Empty constructor
public BDBEnvironmentMangager() {}
/**
* Initialize the BDB environment
* */
public void setup(File envHome, boolean readOnly)
throws DatabaseException {
//Create a BDB environment configuration object
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
//Create a Data Storage Configuration Object
StoreConfig myStoreConfig = new StoreConfig();
//Set whether the environment is read-only, true read-only and false read-write
myEnvConfig.setReadOnly(readOnly);
//Set whether the data storage configuration is read-only, true read-only and false read-write
myStoreConfig.setReadOnly(readOnly);
//If the environment does not exist whether to rebuild or not, true allows rebuilding and false cannot be rebuilt.
myEnvConfig.setAllowCreate(!readOnly);
//If the storage configuration does not exist whether to rebuild or not, true allows rebuilding and false cannot be rebuilt
myStoreConfig.setAllowCreate(!readOnly);
// Open environment and entity store
myEnvironment = new Environment(envHome, myEnvConfig);
myEntityStore = new EntityStore(myEnvironment, "EntityStore", myStoreConfig);
}
// Close the store and environment.
public void close() {
//Determine whether the stored object is empty
if (myEntityStore != null) {
try {
//Attempt to close storage objects
myEntityStore.close();
} catch(DatabaseException dbe) {
System.err.println("Error closing store: " +dbe.toString());
System.exit(-1);
}
}
//Judging whether the environment is empty
if (myEnvironment != null) {
try {
// Closing environment
myEnvironment.close();
} catch(DatabaseException dbe) {
System.err.println("Error closing MyDbEnv: " + dbe.toString());
System.exit(-1);
}
}
}
//Getter and Setter
public Environment getMyEnvironment() {
return myEnvironment;
}
public void setMyEnvironment(Environment myEnvironment) {
this.myEnvironment = myEnvironment;
}
public EntityStore getMyEntityStore() {
return myEntityStore;
}
public void setMyEntityStore(EntityStore myEntityStore) {
this.myEntityStore = myEntityStore;
}
}
Then we need an entity class, and I define an entity class called Product.
The code is as follows:
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.Relationship;
import com.sleepycat.persist.model.SecondaryKey;
@Entity
public class Product {
//Primary key
@PrimaryKey
private Long productId;
private String productName;
//One-to-one relationship
@SecondaryKey(relate=Relationship.ONE_TO_ONE)
private Integer catelogId;
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Integer getCatelogId() {
return catelogId;
}
public void setCatelogId(Integer catelogId) {
this.catelogId = catelogId;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
Then we started to create our data accessors.
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import com.xingyun.model.Product;
public class ProductDA {
// Index Accessors Index Accessors
public PrimaryIndex<Long,Product> pIdx;//Level I Index, Primary Key
public SecondaryIndex<Long,Long,Product> sIdx;//Two level index
public ProductDA(EntityStore entityStore) {
// Get the primary key of the Product entity class, the first-level index
pIdx = entityStore.getPrimaryIndex(Long.class, Product.class);
// First level index
// Return type of get method for secondary index field
// Name of index field of secondary city
//Primary key, field type of secondary index, name of secondary index field
sIdx = entityStore.getSecondaryIndex(pIdx, Long.class, "catelogId");
}
}
Then the last step begins to write our calling master method
import java.io.File;
import com.sleepycat.persist.EntityStore;
import com.xingyun.db.ProductDA;
import com.xingyun.model.Product;
import com.xingyun.util.BDBEnvironmentMangager;
public class MainTest2 {
// Configure the database environment file path,
private final static String BDB_ENV_HOME_2_FILE_PATH = "bdb_env_home2";
//Database Environment File
private final static File BDB_ENV_HOME_2_File = new File(BDB_ENV_HOME_2_FILE_PATH);
public static void main(String[] args) {
// TODO Auto-generated method stub
BDBEnvironmentMangager myBdbEnvironmentMangager=new BDBEnvironmentMangager();
//If the database environment file object does not exist
if(!BDB_ENV_HOME_2_File.exists()) {
//Create this file path
BDB_ENV_HOME_2_File.mkdirs();
}
//Database environment initialization operation
myBdbEnvironmentMangager.setup(BDB_ENV_HOME_2_File,false);
//Storing data
EntityStore entityStore=myBdbEnvironmentMangager.getMyEntityStore();
//Create a Data Accessor
ProductDA myProductDA=new ProductDA(entityStore);
//Start storing data
Product product=new Product();
product.setProductId(1L);
product.setProductName("Product Name1");
product.setCatelogId(6);
myProductDA.pIdx.put(product);
Product product2=new Product();
product2.setProductId(2L);
product2.setProductName("Product Name2");
product2.setCatelogId(8);
myProductDA.pIdx.put(product2);
//Read saved objects
Product resultProduct=new Product();
resultProduct=myProductDA.pIdx.get(1L);
System.out.println(resultProduct.getProductId());
System.out.println(resultProduct.getProductName());
System.out.println(resultProduct.getCatelogId());
Product resultProduct2=new Product();
resultProduct2=myProductDA.pIdx.get(2L);
System.out.println(resultProduct2.getProductId());
System.out.println(resultProduct2.getProductName());
System.out.println(resultProduct2.getCatelogId());
//Close EntityStore and Environment
myBdbEnvironmentMangager.close();
}
}
Operation results: