Bdb JE对复杂数据的存储

(一)、OneToOne关系的存储 
我主要是通过每个人对应一个×××号码,来实现OneToOne,我在删除人的时候我用到了级联。 
人类: 
@Entity 
public class Person { 
@PrimaryKey 
int pid; 
@SecondaryKey(relate = Relationship.MANY_TO_ONE) 
String pname; 
@SecondaryKey(relate = Relationship.ONE_TO_ONE,relatedEntity=IdCard.class,onRelatedEntityDelete=DeleteAction.CASCADE) 
int id; 
public Person() { 

public Person(int pid, String name, int id) { 
  this.pid = pid; 
  this.pname = name; 
  this.id = id; 



×××类: 
@Entity 
public class IdCard { 
@PrimaryKey 
int id; 
@SecondaryKey(relate=Relationship.ONE_TO_ONE) 
String idcard; 

public IdCard(){} 
public IdCard(int id,String idcard){ 
  this.id=id; 
  this.idcard=idcard; 


关系对应类: 

public class PAccessor { 
PrimaryIndex personByPId; 
SecondaryIndex personByPName; 
SecondaryIndex personById; 
PrimaryIndex idcardById; 
SecondaryIndex idcardByIdcard; 
public PAccessor(EntityStore store) throws DatabaseException { 
  personByPId = store.getPrimaryIndex(Integer.class, Person.class); 
  personByPName = store.getSecondaryIndex(personByPId, String.class, 
    "pname"); 
  personById = store.getSecondaryIndex(personByPId, Integer.class, "id"); 
  idcardById = store.getPrimaryIndex(Integer.class, IdCard.class); 
  idcardByIdcard = store.getSecondaryIndex(idcardById, String.class, 
    "idcard"); 



测试类: 
public class testOne2One { 

public static void main(String[] args) { 
  EnvironmentConfig envConfig=new EnvironmentConfig(); 
  envConfig.setAllowCreate(true); 
  envConfig.setTransactional(true); 
  try { 
//要在这新建文件哦 
   Environment env=new Environment(new File("d://bdb//one2oneje"),envConfig); 
   StoreConfig storeConfig=new StoreConfig(); 
   storeConfig.setAllowCreate(true); 
   storeConfig.setTransactional(true); 
   EntityStore store=new EntityStore(env,"PersonStore",storeConfig); 
   PAccessor paccessor=new PAccessor(store); 
   Transaction txn=env.beginTransaction(null, null); 
   paccessor.idcardById.put(txn,new IdCard(1,"421023198709218315")); 
   paccessor.idcardById.put(txn,new IdCard(2,"421023198710215927")); 
   paccessor.idcardById.put(txn,new IdCard(3,"421023198708282315")); 
   paccessor.idcardById.put(txn,new IdCard(4,"421123198708282315")); 
   paccessor.idcardById.put(txn,new IdCard(5,"421223198708282315")); 
   paccessor.personByPId.put(txn,new Person(1,"Bill",1)); 
   paccessor.personByPId.put(txn,new Person(2,"Mykey",3)); 
   paccessor.personByPId.put(txn,new Person(3,"Mykey",2)); 
   paccessor.personByPId.put(txn,new Person(4,"Match",4)); 
   paccessor.personByPId.put(txn,new Person(5,"Kill",5)); 
   txn.commit(); 
   
   String prefix = "persist#" + store.getStoreName() + "#"; 
   for(String name:env.getDatabaseNames()){ 
    if(name.startsWith(prefix)){ 
     System.out.println(name); 
    } 
   } 
   System.out.println("输出人的信息包括省份证号码:"); 
   EntityCursor ec=paccessor.personByPId.entities(); 
   for(Person p:ec){ 
    System.out.println(p.pid+"  "+p.pname+"  "+paccessor.idcardById.get(p.id).idcard); 
   } 
   ec.close(); 
   System.out.println("修改ID为1的×××号码:"); 
   IdCard idcard=paccessor.idcardById.get(1); 
   idcard.idcard="421023198709218316"; 
   paccessor.idcardById.putNoReturn(idcard); 
   System.out.println(paccessor.idcardById.get(1).idcard); 
   System.out.println("///////修改ID为4人的姓名,可以通过游标来修改和删除entity///////"); 
   Person person=paccessor.personByPId.get(4); 
   person.pname="berkeley"; 
   person.pname=person.pname.toUpperCase(); 
   paccessor.personByPId.putNoReturn(person); 
   System.out.println(paccessor.personByPId.get(4).pname); 
   System.out.println("/////////////查询名称为Mykey的所有人//////////////////////////"); 
   EntityJoin entityJoin=new EntityJoin(paccessor.personByPId); 
   entityJoin.addCondition(paccessor.personByPName, "Mykey"); 
   //entityJoin.addCondition(paccessor.personById, 3); 
   ForwardCursor f=entityJoin.entities(); 
   for(Person p:f){ 
    System.out.println(p.id+"   "+p.pid+"   "+p.pname); 
   } 
   f.close(); 
   System.out.println("************查询ID为1到4*****************"); 
   ec=paccessor.personByPId.entities(1, true, 4, true); 
   for(Person p:ec){ 
    System.out.println(p.pid+"  "+p.pname+"  "+paccessor.idcardById.get(p.id).idcard); 
   } 
   ec.close(); 
   System.out.println("***********查询人的名称中有也M开头的**************"); 
   ec=paccessor.personByPName.entities("Mykey", true, "N", true); 
   for(Person p:ec){ 
    System.out.println(p.pid+"  "+p.pname+"  "+paccessor.idcardById.get(p.id).idcard); 
   } 
   ec.close(); 
   System.out.println("------------------------"); 
   ec=paccessor.personByPName.subIndex("Mykey").entities(); 
   for(Person p:ec){ 
    System.out.println(p.pid+"  "+p.pname+"  "+paccessor.idcardById.get(p.id).idcard); 
   } 
   ec.close(); 
   System.out.println("************删除id为2的人******************"); 
   paccessor.personByPId.delete(2); 
   System.out.println("输出人的信息包括省份证号码:"); 
   ec=paccessor.personByPId.entities(); 
   for(Person p:ec){ 
    System.out.println(p.pid+"  "+p.pname+"  "+paccessor.idcardById.get(p.id).idcard); 
   } 
   ec.close(); 
   
   store.close(); 
   env.cleanLog(); 
   env.close(); 
  }catch (Exception e) { 
   e.printStackTrace(); 
  }