天天看点

十一、BDB ManyToMany

Bdb JE对复杂数据的存储(四)、ManyToMany关系的存储 

班级类

@Entity

public class Classs {

@PrimaryKey

String cId;

@SecondaryKey(relate=Relationship.MANY_TO_ONE)

String cName;

@SecondaryKey(relate=Relationship.MANY_TO_MANY,relatedEntity=Teacher.class,onRelatedEntityDelete=DeleteAction.NULLIFY)

Set<String> setTeacher=new HashSet<String>();

public Classs(){}

public Classs(String id,String name){

    this.cId=id;

    this.cName=name;

}

老师类:

public class Teacher {

String tId;

String tName;

@SecondaryKey(relate=Relationship.MANY_TO_MANY,relatedEntity=Classs.class,onRelatedEntityDelete=DeleteAction.NULLIFY)

Set<String> setClasss=new HashSet<String>();

public Teacher(){}

public Teacher(String id,String name){

    this.tId=id;

    this.tName=name;

关系类:

public class Accessor {

PrimaryIndex<String, Classs> primaryClassById;

SecondaryIndex<String, String, Classs> classsByName;

SecondaryIndex<String, String, Classs> classsBySetTeacher;

PrimaryIndex<String, Teacher> primaryTeacherById;

SecondaryIndex<String, String, Teacher> teacherByName;

SecondaryIndex<String, String, Teacher> teacherByClasss;

Accessor(EntityStore store) throws DatabaseException{

    this.primaryClassById=store.getPrimaryIndex(String.class, Classs.class);

    this.classsByName=store.getSecondaryIndex(this.primaryClassById, String.class, "cName");

    this.classsBySetTeacher=store.getSecondaryIndex(this.primaryClassById, String.class, "setTeacher");

    this.primaryTeacherById=store.getPrimaryIndex(String.class, Teacher.class);

    this.teacherByName=store.getSecondaryIndex(this.primaryTeacherById, String.class, "tName");

    this.teacherByClasss=store.getSecondaryIndex(this.primaryTeacherById, String.class, "setClasss");

测试类:

public class testMany2Many {

public static void main(String[] args) {

    Environment env=null;

    EnvironmentConfig envconfig=new EnvironmentConfig();

    envconfig.setAllowCreate(true);

    envconfig.setTransactional(true);

    StoreConfig storeconfig=new StoreConfig();

    storeconfig.setAllowCreate(true);

    storeconfig.setTransactional(true);

    EntityStore store=null;

    try {

     env=new Environment(new File("D:/bdb/many2manyje"),envconfig);

     store=new EntityStore(env,"many2manyje",storeconfig);

     Accessor dao=new Accessor(store);

     PrimaryIndex<String, Classs> classsById=dao.primaryClassById;

     PrimaryIndex<String, Teacher> teacherById=dao.primaryTeacherById;

     Transaction txn=env.beginTransaction(null, null);

     Classs c1=new Classs("100001","三年一班");

     Classs c2=new Classs("100002","三年二班");

     Classs c3=new Classs("100003","三年三班");

     Classs c4=new Classs("100004","四年一班");

     Classs c5=new Classs("100005","四年二班");

     Classs c6=new Classs("100006","四年三班");

     Teacher t1=new Teacher("4200106310001","张微");

     Teacher t2=new Teacher("4200106310002","刘婷");

     Teacher t3=new Teacher("4200106310003","许红");

     Teacher t4=new Teacher("4200106310004","张珊");

     Teacher t5=new Teacher("4200106310005","周燕");

     Teacher t6=new Teacher("4200106310006","李蝶");

     Teacher t7=new Teacher("4200106310007","徐馨");

     Teacher t8=new Teacher("4200106310008","赵君");

     Teacher t9=new Teacher("4200106310009","刘可");

     Set<String> setT1=new HashSet<String>();

     setT1.add(t1.tId);

     setT1.add(t2.tId);

     setT1.add(t3.tId);

     Set<String> setT2=new HashSet<String>();

     setT2.add(t2.tId);

     setT2.add(t3.tId);

     setT2.add(t4.tId);

     Set<String> setT3=new HashSet<String>();

     setT3.add(t3.tId);

     setT3.add(t4.tId);

     setT3.add(t5.tId);

     Set<String> setT4=new HashSet<String>();

     setT4.add(t5.tId);

     setT4.add(t6.tId);

     setT4.add(t7.tId);

     Set<String> setT5=new HashSet<String>();

     setT5.add(t7.tId);

     setT5.add(t8.tId);

     setT5.add(t9.tId);

     c1.setTeacher=setT1;

     c2.setTeacher=setT2;

     c3.setTeacher=setT1;

     c4.setTeacher=setT3;

     c5.setTeacher=setT4;

     c6.setTeacher=setT5;

     teacherById.put(txn,t1);

     teacherById.put(txn,t2);

     teacherById.put(txn,t3);

     teacherById.put(txn,t4);

     teacherById.put(txn,t5);

     teacherById.put(txn,t6);

     teacherById.put(txn,t7);

     teacherById.put(txn,t8);

     teacherById.put(txn,t9);

     classsById.put(txn, c1);

     classsById.put(txn, c2);

     classsById.put(txn, c3);

     classsById.put(txn, c4);

     classsById.put(txn, c5);

     classsById.put(txn, c6);

     EntityCursor<Classs> ecClasss=null;

     EntityCursor<Teacher> ecTeacher=null;

     System.out.println("---------班级信息-------------------");

     ecClasss=classsById.entities(txn, null);

     for(Classs c:ecClasss){

        Iterator<String> it=c.setTeacher.iterator();

        StringBuffer strbuf=new StringBuffer();

        while(it.hasNext()){

         strbuf.append(teacherById.get(txn, it.next(), LockMode.DEFAULT).tName+" ");

        }

        System.out.println("班级ID:"+c.cId+" 班级名称:    "+c.cName+" 所属该班级的老师: "+strbuf.toString());

     }

     ecClasss.close();

     System.out.println("---------修改老师的所属班级信息-------------");

     ecTeacher=teacherById.entities(txn, null);

     for(Teacher t:ecTeacher){

        ecClasss=classsById.entities(txn, null);

        for(Classs c:ecClasss){

         if(c.setTeacher.contains(t.tId)){

            t.setClasss.add(c.cId);

         }

        ecTeacher.update(t);

        ecClasss.close();

     ecTeacher.close();

     System.out.println("----------老师信息----------");

        Iterator<String> it=t.setClasss.iterator();

         strbuf.append(classsById.get(txn,it.next(),LockMode.DEFAULT).cName+" ");

        System.out.println("老师的ID "+t.tId+" 老师的名字: "+t.tName+" 老师所属班级: "+strbuf.toString());

     //在提交txn的时候最好有把所有的游标都要关闭

     txn.commit();

     store.close();

     env.cleanLog();

     env.close();

    } catch (EnvironmentLockedException e) {

     e.printStackTrace();

    } catch (DatabaseException e) {

    }

}