十一、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;
}
}
老師類:

@Entity
public class Teacher {

@PrimaryKey
String tId;

@SecondaryKey(relate=Relationship.MANY_TO_ONE)
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("----------老師信息----------");
     ecTeacher=teacherById.entities(txn, null);
     for(Teacher t:ecTeacher){
        Iterator<String> it=t.setClasss.iterator();
        StringBuffer strbuf=new StringBuffer();
        while(it.hasNext()){
         strbuf.append(classsById.get(txn,it.next(),LockMode.DEFAULT).cName+" ");
        }
        System.out.println("老師的ID "+t.tId+" 老師的名字: "+t.tName+" 老師所屬班級: "+strbuf.toString());
     }
     ecTeacher.close();
     //在提交txn的時候最好有把所有的遊標都要關閉
     txn.commit();
     store.close();
     env.cleanLog();
     env.close();
    } catch (EnvironmentLockedException e) {
     e.printStackTrace();
    } catch (DatabaseException e) {
     e.printStackTrace();
    }
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章