日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

九、BDB OneToMany

發(fā)布時(shí)間:2024/4/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 九、BDB OneToMany 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?(二)、OneToMany關(guān)系的存儲(chǔ)?

班級(jí)類:

  • @Entity??
  • public?class?Classs?{??
  • @PrimaryKey??
  • String?classsId;??
  • ?
  • @SecondaryKey(relate=Relationship.ONE_TO_ONE)??
  • String?classsName;??
  • ?
  • @SecondaryKey(relate=Relationship.ONE_TO_MANY,relatedEntity=Student.class,onRelatedEntityDelete=DeleteAction.CASCADE)?
  • Set<String>?setStudent=new?HashSet<String>();??
  • ?
  • public?Classs(){??
  • }??
  • public?Classs(String?id,String?name){??
  • ??this.classsId=id;??
  • ??this.classsName=name;??
  • }??
  • ?
  • public?Classs(String?id,String?name,Set<String>?set){??
  • ??this.classsId=id;??
  • ??this.classsName=name;??
  • ??this.setStudent=set;??
  • }??
  • }??
  • 學(xué)生類:??
  • @Entity??
  • public?class?Student?{??
  • @PrimaryKey??
  • String?studentId;??
  • ?
  • @SecondaryKey(relate=Relationship.MANY_TO_ONE)??
  • String?studentName;??
  • ?
  • @SecondaryKey(relate=Relationship.MANY_TO_ONE,relatedEntity=Classs.class,onRelatedEntityDelete=DeleteAction.NULLIFY)?
  • String?classsId;??
  • ?
  • @SecondaryKey(relate=Relationship.MANY_TO_MANY,relatedEntity=Teacher.class,onRelatedEntityDelete=DeleteAction.NULLIFY)?
  • Set<String>?setTeacher=new?HashSet<String>();??
  • ?
  • Student(){??
  • }??
  • public?Student(String?id,String?name,String?cId,Set<String>?set){??
  • ??this.studentId=id;??
  • ??this.studentName=name;??
  • ??this.classsId=cId;??
  • ??this.setTeacher=set;??
  • }??
  • @Override??
  • public?String?toString()?{??
  • ??return?"學(xué)生id:"+this.studentId+"?姓名:?"+this.studentName;??
  • }??
  • }??
  • ?
  • 老師類:??
  • @Entity??
  • public?class?Teacher?{??
  • @PrimaryKey??
  • String?teacherId;??
  • ?
  • @SecondaryKey(relate=Relationship.MANY_TO_ONE)??
  • String?teacherName;??
  • ?
  • public?Teacher(){??
  • }??
  • ?
  • public?Teacher(String?id,String?name){??
  • ??this.teacherId=id;??
  • ??this.teacherName=name;??
  • }??
  • ?
  • @Override??
  • public?String?toString()?{??
  • ??return?"老師ID:"+this.teacherId+"老師Name"+this.teacherName;??
  • }??
  • }??
  • ?
  • 關(guān)系類:??
  • public?class?Accessor?{??
  • ?
  • PrimaryIndex<String,?Teacher>?teacherById;??
  • SecondaryIndex<String,?String,?Teacher>?teacherByName;??
  • ?
  • PrimaryIndex<String,?Classs>?classsById;??
  • SecondaryIndex<String,?String,?Classs>?classsByName;??
  • SecondaryIndex<String,?String,?Classs>?classsBySetStudent;??
  • ?
  • PrimaryIndex<String,?Student>?studentById;??
  • SecondaryIndex<String,?String,?Student>?studentByName;??
  • SecondaryIndex<String,?String,?Student>?studentByCid;??
  • SecondaryIndex<String,?String,?Student>?studentBySetTeacher;??
  • ?
  • public?Accessor(EntityStore?store)?throws?DatabaseException{??
  • ??teacherById=store.getPrimaryIndex(String.class,?Teacher.class);??
  • ??teacherByName=store.getSecondaryIndex(teacherById,?String.class,?"teacherName");??
  • ????
  • ??classsById=store.getPrimaryIndex(String.class,?Classs.class);??
  • ??classsByName=store.getSecondaryIndex(classsById,?String.class,?"classsName");??
  • ??classsBySetStudent=store.getSecondaryIndex(classsById,?String.class,?"setStudent");??
  • ????
  • ??studentById=store.getPrimaryIndex(String.class,?Student.class);??
  • ??studentByName=store.getSecondaryIndex(studentById,?String.class,?"studentName");??
  • ??studentByCid=store.getSecondaryIndex(studentById,?String.class,?"classsId");??
  • ??studentBySetTeacher=store.getSecondaryIndex(studentById,?String.class,?"setTeacher");??
  • }??
  • }??
  • ?
  • test類:??
  • /**??
  • *?測(cè)試??
  • *?@author?Administrator??
  • *?班級(jí)??學(xué)生??老師??
  • *?1------->*?1----->*??
  • *??
  • */??
  • public?class?testOne2Many?{??
  • ?
  • 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?entityStore=null;??
  • ??try?{??
  • ???env=new?Environment(new?File("d://bdb//onetomanyje"),envconfig);??
  • ???entityStore=new?EntityStore(env,"Store",storeconfig);??
  • ???Accessor?dao=new?Accessor(entityStore);??
  • ????
  • ???PrimaryIndex<String,?Teacher>?primaryByTeacher=dao.teacherById;??
  • ???PrimaryIndex<String,?Classs>?primaryByClasss=dao.classsById;??
  • ???PrimaryIndex<String,?Student>?primaryByStudent=dao.studentById;??
  • ???SecondaryIndex<String,?String,?Student>?studentByCid=dao.studentByCid;??
  • ???Transaction?txn=env.beginTransaction(null,?null);??
  • ????
  • ???Teacher?t1=new?Teacher("100001","王偉");??
  • ???Teacher?t2=new?Teacher("100002","趙奇");??
  • ???Teacher?t3=new?Teacher("100003","劉利");??
  • ????
  • ???Set<String>?setTeacher1=new?HashSet<String>();??
  • ???setTeacher1.add("100001");??
  • ???setTeacher1.add("100002");??
  • ????
  • ???Set<String>?setTeacher2=new?HashSet<String>();??
  • ???setTeacher2.add("100001");??
  • ???setTeacher2.add("100003");??
  • ????
  • ???Set<String>?setTeacher3=new?HashSet<String>();??
  • ???setTeacher3.add("100003");??
  • ???setTeacher3.add("100002");??
  • ????
  • ???Student?stu1=new?Student("4200106310001","張三","000001",setTeacher1);??
  • ???Student?stu2=new?Student("4200106310002","李四","000001",setTeacher1);??
  • ???Student?stu3=new?Student("4200106310003","張三","000001",setTeacher1);??
  • ???Student?stu4=new?Student("4200106310004","王五","000001",setTeacher1);??
  • ????
  • ???Student?stu5=new?Student("4200106310005","趙六","000002",setTeacher2);??
  • ???Student?stu6=new?Student("4200106310006","王五","000002",setTeacher2);??
  • ???Student?stu7=new?Student("4200106310007","李四","000002",setTeacher2);??
  • ???Student?stu8=new?Student("4200106310008","李利","000002",setTeacher2);??
  • ????
  • ???Student?stu9=new?Student("4200106310009","徐咪","000003",setTeacher3);??
  • ???Student?stu10=new?Student("4200106310010","劉洪","000003",setTeacher3);??
  • ???Student?stu11=new?Student("4200106310011","吳鋒","000003",setTeacher3);??
  • ???Student?stu12=new?Student("4200106310012","許珊","000003",setTeacher3);??
  • ????
  • ???Classs?c1=new?Classs("000001","一年一班");??
  • ???Classs?c2=new?Classs("000002","一年二班");??
  • ???Classs?c3=new?Classs("000003","一年三班");??
  • ????
  • ???primaryByTeacher.put(txn,?t1);??
  • ???primaryByTeacher.put(txn,?t2);??
  • ???primaryByTeacher.put(txn,?t3);??
  • ????
  • ???primaryByClasss.put(txn,?c1);??
  • ???primaryByClasss.put(txn,?c2);??
  • ???primaryByClasss.put(txn,?c3);??
  • ????
  • ???primaryByStudent.put(txn,?stu1);??
  • ???primaryByStudent.put(txn,?stu2);??
  • ???primaryByStudent.put(txn,?stu3);??
  • ???primaryByStudent.put(txn,?stu4);??
  • ???primaryByStudent.put(txn,?stu5);??
  • ???primaryByStudent.put(txn,?stu6);??
  • ???primaryByStudent.put(txn,?stu7);??
  • ???primaryByStudent.put(txn,?stu8);??
  • ???primaryByStudent.put(txn,?stu9);??
  • ???primaryByStudent.put(txn,?stu10);??
  • ???primaryByStudent.put(txn,?stu11);??
  • ???primaryByStudent.put(txn,?stu12);??
  • ????
  • ???EntityCursor<Student>?ecStudent=null;??
  • ???EntityCursor<Classs>?ecClasss=null;??
  • ????
  • ???System.out.println("----------------通過在student中的cid得到班級(jí)里面的學(xué)生--------------------------");??
  • ???ecClasss=primaryByClasss.entities(txn,null);??
  • ???for(Classs?c:ecClasss){??
  • ????System.out.println("--------------"+c.classsName+"--------------------"+c.setStudent.toString());??
  • ????ecStudent=studentByCid.subIndex(c.classsId).entities(txn,?null);??
  • ????for(Student?s:ecStudent){??
  • ?????StringBuffer?strbuf=new?StringBuffer();??
  • ?????Iterator<String>?it=s.setTeacher.iterator();??
  • ?????while(it.hasNext()){??
  • ??????strbuf.append(primaryByTeacher.get(txn,?it.next(),?LockMode.DEFAULT).teacherName+"?");??
  • ?????}??
  • ?????System.out.println(s.toString()+"??班級(jí)名:?"+c.classsName+"?所有老師:?"+strbuf.toString());??
  • ????}??
  • ????ecStudent.close();??
  • ???}??
  • ???ecClasss.close();??
  • ????
  • ???System.out.println("---------------------修改班級(jí)中SetStudent-------------------------------");??
  • ???ecClasss=primaryByClasss.entities(txn,null);??
  • ???for(Classs?c:ecClasss){??
  • ????ecStudent=studentByCid.subIndex(c.classsId).entities(txn,?null);??
  • ????for(Student?s:ecStudent){??
  • ?????c.setStudent.add(s.studentId);??
  • ????}??
  • ????ecClasss.update(c);??
  • ????ecStudent.close();??
  • ???}??
  • ???ecClasss.close();??
  • ????
  • ???System.out.println("------------通過得到班級(jí)中的setStudent得到學(xué)生的信息--------------------");??
  • ???ecClasss=primaryByClasss.entities(txn,null);??
  • ???for(Classs?c:ecClasss){??
  • ????System.out.println("--------------"+c.classsName+"--------------------"+c.setStudent.toString());??
  • ????Iterator<String>?it=c.setStudent.iterator();??
  • ????while(it.hasNext()){??
  • ?????StringBuffer?strbuf=new?StringBuffer();??
  • ?????Student?s=primaryByStudent.get(txn,it.next(),LockMode.DEFAULT);??
  • ?????Iterator<String>?i=s.setTeacher.iterator();??
  • ?????while(i.hasNext()){??
  • ??????strbuf.append(primaryByTeacher.get(txn,?i.next(),?LockMode.DEFAULT).teacherName+"?");??
  • ?????}??
  • ?????System.out.println(s.toString()+"??班級(jí)名:?"+c.classsName+"?所有老師:?"+strbuf.toString());??
  • ????}??
  • ???}??
  • ???ecClasss.close();??
  • ???txn.commit();??
  • ???entityStore.close();??
  • ???env.cleanLog();??
  • ???env.close();??
  • ??}catch(Exception?e){??
  • ???e.printStackTrace();??
  • ??}??
  • }??
  • }????
  • ?

    轉(zhuǎn)載于:https://blog.51cto.com/gjbxx110/615486

    總結(jié)

    以上是生活随笔為你收集整理的九、BDB OneToMany的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。