(二)、OneToMany关系的存储

班级类:

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

转载于:https://blog.51cto.com/gjbxx110/615486

九、BDB OneToMany相关推荐

  1. 2021年大数据Kafka(九):kafka消息存储及查询机制原理

    全网最详细的大数据Kafka文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 kafka消息存储及查询机制原理 一.Kafka数据存储机制 ...

  2. 2021年大数据HBase(九):Apache Phoenix的安装

    全网最详细的大数据HBase文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 前言 系列历史文章 安装Phoenix 一.下载 二.安装 1.上传安装包 ...

  3. 2021年大数据Hive(九):Hive的数据压缩

    全网最详细的大数据Hive文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 前言 Hive的数据压缩 一.MR支持的压缩编码 二.压缩配置 ...

  4. 2021年大数据Hadoop(二十九):​​​​​​​关于YARN常用参数设置

    全网最详细的Hadoop文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 本系列历史文章 前言 关于yarn常用参数设置 设置container分配最小内 ...

  5. python第二十九课——文件读写(复制文件)

    自定义函数:实现文件复制操作有形参(2个) 没有返回值相似版(不用) def copyFile(src,dest):#1.打开两个文件:1个关联读操作,1个关联写操作fr=open(src,'rb') ...

  6. 第十九章——使用资源调控器管理资源(2)——使用T-SQL配置资源调控器

    第十九章--使用资源调控器管理资源(2)--使用T-SQL配置资源调控器 原文: 第十九章--使用资源调控器管理资源(2)--使用T-SQL配置资源调控器 前言: 在前一章已经演示了如何使用SSMS来 ...

  7. MySQL面试题 | 附答案解析(九)

    锁 对MySQL的锁了解吗当数据库有并发事务的时候,可能会产生数据的不一致,这时候需要一些机制来保证访问的次序,锁机制就是这样的一个机制.就像酒店的房间,如果大家随意进出,就会出现多人抢夺同一个房间的 ...

  8. autoware中lgsvl Simulator安装与使用:LGsvl Simulator 2021.2.1版(九)

    autoware安装与使用:LGsvl Simulator 2021.2.1版(windows10)(九) 介绍如何在windows下安装LGsvl Simulator 2021.2.1版 环境:wi ...

  9. bmp文件头_「正点原子FPGA连载」第十九章SD卡读BMP图片LCD显示

    1)摘自[正点原子]领航者 ZYNQ 之嵌入式开发指南 2)实验平台:正点原子领航者ZYNQ开发板 3)平台购买地址:https://item.taobao.com/item.htm?&id= ...

最新文章

  1. [异常解决] MPU6050启动异常读出陀螺仪和加速度计的值全为0的解决办法
  2. 发现一个木马,竟然偷传我珍藏几十G的视频!
  3. 如何选择免费网站监测工具?国外mon.itor.us还是国内监控宝!
  4. 各种震撼的慢镜头,奇怪的知识又增加了!​
  5. linux的mount和umount指令使用
  6. Win11怎么设置耳机和音响一起响
  7. css中margin:0 auto没作用
  8. 201521123062《Java程序设计》第10周学习总结
  9. 每日一课(10/75)CPU资源和存储器 之 专用寄存器的作用
  10. 未知宽高div水平垂直居中的3种方法
  11. PLC(二)西门子S7-200PLC基础知识
  12. 阶乘的0 【南阳 oj 题目84】
  13. 学习R语言这几本电子书就够了!
  14. 通话过程中显示呼叫失败_iphone怎么总是打电话出现呼叫失败求大神解救
  15. 【判断一个数是不是素数】
  16. 25道经典大企Python面试题总结(附答案)
  17. 两位前阿里 P10 的成长经历,让我学到这几点
  18. SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified
  19. jmeter接口自动化,你敢想,我敢玩
  20. 10月14日笔记交叉开发开发板连接,远程登录和tftp服务器配置

热门文章

  1. Interfacing to kdb+ from Java
  2. 视图中的难点:主键表 About Key-Preserved Tables
  3. Apache Tomcat 5.5 Servlet/JSP 容器
  4. Index of school
  5. webstorm主要快捷键
  6. Spring事务源码分析
  7. 苹果Xcode帮助文档阅读指南
  8. Matplotlib 2016-04-15
  9. XMLHttpResponse 在项目里面的运用
  10. Eclipse 常用快捷键