九、BDB OneToMany

 (二)、OneToMany关系的存储 

班级类:

  
  
  
  
  1. @Entity  
  2. public class Classs {  
  3. @PrimaryKey  
  4. String classsId;  
  5.  
  6. @SecondaryKey(relate=Relationship.ONE_TO_ONE)  
  7. String classsName;  
  8.  
  9. @SecondaryKey(relate=Relationship.ONE_TO_MANY,relatedEntity=Student.class,onRelatedEntityDelete=DeleteAction.CASCADE) 
  10. Set<String> setStudent=new HashSet<String>();  
  11.  
  12. public Classs(){  
  13. }  
  14. public Classs(String id,String name){  
  15.   this.classsId=id;  
  16.   this.classsName=name;  
  17. }  
  18.  
  19. public Classs(String id,String name,Set<String> set){  
  20.   this.classsId=id;  
  21.   this.classsName=name;  
  22.   this.setStudent=set;  
  23. }  
  24. }  
  25. 学生类:  
  26. @Entity  
  27. public class Student {  
  28. @PrimaryKey  
  29. String studentId;  
  30.  
  31. @SecondaryKey(relate=Relationship.MANY_TO_ONE)  
  32. String studentName;  
  33.  
  34. @SecondaryKey(relate=Relationship.MANY_TO_ONE,relatedEntity=Classs.class,onRelatedEntityDelete=DeleteAction.NULLIFY) 
  35. String classsId;  
  36.  
  37. @SecondaryKey(relate=Relationship.MANY_TO_MANY,relatedEntity=Teacher.class,onRelatedEntityDelete=DeleteAction.NULLIFY) 
  38. Set<String> setTeacher=new HashSet<String>();  
  39.  
  40. Student(){  
  41. }  
  42. public Student(String id,String name,String cId,Set<String> set){  
  43.   this.studentId=id;  
  44.   this.studentName=name;  
  45.   this.classsId=cId;  
  46.   this.setTeacher=set;  
  47. }  
  48. @Override  
  49. public String toString() {  
  50.   return "学生id:"+this.studentId+" 姓名: "+this.studentName;  
  51. }  
  52. }  
  53.  
  54. 老师类:  
  55. @Entity  
  56. public class Teacher {  
  57. @PrimaryKey  
  58. String teacherId;  
  59.  
  60. @SecondaryKey(relate=Relationship.MANY_TO_ONE)  
  61. String teacherName;  
  62.  
  63. public Teacher(){  
  64. }  
  65.  
  66. public Teacher(String id,String name){  
  67.   this.teacherId=id;  
  68.   this.teacherName=name;  
  69. }  
  70.  
  71. @Override  
  72. public String toString() {  
  73.   return "老师ID:"+this.teacherId+"老师Name"+this.teacherName;  
  74. }  
  75. }  
  76.  
  77. 关系类:  
  78. public class Accessor {  
  79.  
  80. PrimaryIndex<String, Teacher> teacherById;  
  81. SecondaryIndex<String, String, Teacher> teacherByName;  
  82.  
  83. PrimaryIndex<String, Classs> classsById;  
  84. SecondaryIndex<String, String, Classs> classsByName;  
  85. SecondaryIndex<String, String, Classs> classsBySetStudent;  
  86.  
  87. PrimaryIndex<String, Student> studentById;  
  88. SecondaryIndex<String, String, Student> studentByName;  
  89. SecondaryIndex<String, String, Student> studentByCid;  
  90. SecondaryIndex<String, String, Student> studentBySetTeacher;  
  91.  
  92. public Accessor(EntityStore store) throws DatabaseException{  
  93.   teacherById=store.getPrimaryIndex(String.class, Teacher.class);  
  94.   teacherByName=store.getSecondaryIndex(teacherById, String.class"teacherName");  
  95.     
  96.   classsById=store.getPrimaryIndex(String.class, Classs.class);  
  97.   classsByName=store.getSecondaryIndex(classsById, String.class"classsName");  
  98.   classsBySetStudent=store.getSecondaryIndex(classsById, String.class"setStudent");  
  99.     
  100.   studentById=store.getPrimaryIndex(String.class, Student.class);  
  101.   studentByName=store.getSecondaryIndex(studentById, String.class"studentName");  
  102.   studentByCid=store.getSecondaryIndex(studentById, String.class"classsId");  
  103.   studentBySetTeacher=store.getSecondaryIndex(studentById, String.class"setTeacher");  
  104. }  
  105. }  
  106.  
  107. test类:  
  108. /**  
  109. * 测试  
  110. * @author Administrator  
  111. * 班级  学生  老师  
  112. * 1------->* 1----->*  
  113.  
  114. */  
  115. public class testOne2Many {  
  116.  
  117. public static void main(String[] args) {  
  118.   Environment env=null;  
  119.   EnvironmentConfig envconfig=new EnvironmentConfig();  
  120.   envconfig.setAllowCreate(true);  
  121.   envconfig.setTransactional(true);  
  122.   StoreConfig storeconfig=new StoreConfig();  
  123.   storeconfig.setAllowCreate(true);  
  124.   storeconfig.setTransactional(true);  
  125.   EntityStore entityStore=null;  
  126.   try {  
  127.    env=new Environment(new File("d://bdb//onetomanyje"),envconfig);  
  128.    entityStore=new EntityStore(env,"Store",storeconfig);  
  129.    Accessor dao=new Accessor(entityStore);  
  130.     
  131.    PrimaryIndex<String, Teacher> primaryByTeacher=dao.teacherById;  
  132.    PrimaryIndex<String, Classs> primaryByClasss=dao.classsById;  
  133.    PrimaryIndex<String, Student> primaryByStudent=dao.studentById;  
  134.    SecondaryIndex<String, String, Student> studentByCid=dao.studentByCid;  
  135.    Transaction txn=env.beginTransaction(nullnull);  
  136.     
  137.    Teacher t1=new Teacher("100001","王伟");  
  138.    Teacher t2=new Teacher("100002","赵奇");  
  139.    Teacher t3=new Teacher("100003","刘利");  
  140.     
  141.    Set<String> setTeacher1=new HashSet<String>();  
  142.    setTeacher1.add("100001");  
  143.    setTeacher1.add("100002");  
  144.     
  145.    Set<String> setTeacher2=new HashSet<String>();  
  146.    setTeacher2.add("100001");  
  147.    setTeacher2.add("100003");  
  148.     
  149.    Set<String> setTeacher3=new HashSet<String>();  
  150.    setTeacher3.add("100003");  
  151.    setTeacher3.add("100002");  
  152.     
  153.    Student stu1=new Student("4200106310001","张三","000001",setTeacher1);  
  154.    Student stu2=new Student("4200106310002","李四","000001",setTeacher1);  
  155.    Student stu3=new Student("4200106310003","张三","000001",setTeacher1);  
  156.    Student stu4=new Student("4200106310004","王五","000001",setTeacher1);  
  157.     
  158.    Student stu5=new Student("4200106310005","赵六","000002",setTeacher2);  
  159.    Student stu6=new Student("4200106310006","王五","000002",setTeacher2);  
  160.    Student stu7=new Student("4200106310007","李四","000002",setTeacher2);  
  161.    Student stu8=new Student("4200106310008","李利","000002",setTeacher2);  
  162.     
  163.    Student stu9=new Student("4200106310009","徐咪","000003",setTeacher3);  
  164.    Student stu10=new Student("4200106310010","刘洪","000003",setTeacher3);  
  165.    Student stu11=new Student("4200106310011","吴锋","000003",setTeacher3);  
  166.    Student stu12=new Student("4200106310012","许珊","000003",setTeacher3);  
  167.     
  168.    Classs c1=new Classs("000001","一年一班");  
  169.    Classs c2=new Classs("000002","一年二班");  
  170.    Classs c3=new Classs("000003","一年三班");  
  171.     
  172.    primaryByTeacher.put(txn, t1);  
  173.    primaryByTeacher.put(txn, t2);  
  174.    primaryByTeacher.put(txn, t3);  
  175.     
  176.    primaryByClasss.put(txn, c1);  
  177.    primaryByClasss.put(txn, c2);  
  178.    primaryByClasss.put(txn, c3);  
  179.     
  180.    primaryByStudent.put(txn, stu1);  
  181.    primaryByStudent.put(txn, stu2);  
  182.    primaryByStudent.put(txn, stu3);  
  183.    primaryByStudent.put(txn, stu4);  
  184.    primaryByStudent.put(txn, stu5);  
  185.    primaryByStudent.put(txn, stu6);  
  186.    primaryByStudent.put(txn, stu7);  
  187.    primaryByStudent.put(txn, stu8);  
  188.    primaryByStudent.put(txn, stu9);  
  189.    primaryByStudent.put(txn, stu10);  
  190.    primaryByStudent.put(txn, stu11);  
  191.    primaryByStudent.put(txn, stu12);  
  192.     
  193.    EntityCursor<Student> ecStudent=null;  
  194.    EntityCursor<Classs> ecClasss=null;  
  195.     
  196.    System.out.println("----------------通过在student中的cid得到班级里面的学生--------------------------");  
  197.    ecClasss=primaryByClasss.entities(txn,null);  
  198.    for(Classs c:ecClasss){  
  199.     System.out.println("--------------"+c.classsName+"--------------------"+c.setStudent.toString());  
  200.     ecStudent=studentByCid.subIndex(c.classsId).entities(txn, null);  
  201.     for(Student s:ecStudent){  
  202.      StringBuffer strbuf=new StringBuffer();  
  203.      Iterator<String> it=s.setTeacher.iterator();  
  204.      while(it.hasNext()){  
  205.       strbuf.append(primaryByTeacher.get(txn, it.next(), LockMode.DEFAULT).teacherName+" ");  
  206.      }  
  207.      System.out.println(s.toString()+"  班级名: "+c.classsName+" 所有老师: "+strbuf.toString());  
  208.     }  
  209.     ecStudent.close();  
  210.    }  
  211.    ecClasss.close();  
  212.     
  213.    System.out.println("---------------------修改班级中SetStudent-------------------------------");  
  214.    ecClasss=primaryByClasss.entities(txn,null);  
  215.    for(Classs c:ecClasss){  
  216.     ecStudent=studentByCid.subIndex(c.classsId).entities(txn, null);  
  217.     for(Student s:ecStudent){  
  218.      c.setStudent.add(s.studentId);  
  219.     }  
  220.     ecClasss.update(c);  
  221.     ecStudent.close();  
  222.    }  
  223.    ecClasss.close();  
  224.     
  225.    System.out.println("------------通过得到班级中的setStudent得到学生的信息--------------------");  
  226.    ecClasss=primaryByClasss.entities(txn,null);  
  227.    for(Classs c:ecClasss){  
  228.     System.out.println("--------------"+c.classsName+"--------------------"+c.setStudent.toString());  
  229.     Iterator<String> it=c.setStudent.iterator();  
  230.     while(it.hasNext()){  
  231.      StringBuffer strbuf=new StringBuffer();  
  232.      Student s=primaryByStudent.get(txn,it.next(),LockMode.DEFAULT);  
  233.      Iterator<String> i=s.setTeacher.iterator();  
  234.      while(i.hasNext()){  
  235.       strbuf.append(primaryByTeacher.get(txn, i.next(), LockMode.DEFAULT).teacherName+" ");  
  236.      }  
  237.      System.out.println(s.toString()+"  班级名: "+c.classsName+" 所有老师: "+strbuf.toString());  
  238.     }  
  239.    }  
  240.    ecClasss.close();  
  241.    txn.commit();  
  242.    entityStore.close();  
  243.    env.cleanLog();  
  244.    env.close();  
  245.   }catch(Exception e){  
  246.    e.printStackTrace();  
  247.   }  
  248. }  
  249. }    

 

你可能感兴趣的:(职场,休闲,bdb,bdb,JE,JE)