BDB JE对复杂数据的储存
(二)、OneToMany关系的存储
班级类:
@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;
}
}
学生类:
@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 "学生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;
}
}
关系类:
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类:
/**
* 测试
* @author Administrator
* 班级 学生 老师
* 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得到班级里面的学生--------------------------");
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()+" 班级名: "+c.classsName+" 所有老师: "+strbuf.toString());
}
ecStudent.close();
}
ecClasss.close();
System.out.println("---------------------修改班级中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("------------通过得到班级中的setStudent得到学生的信息--------------------");
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()+" 班级名: "+c.classsName+" 所有老师: "+strbuf.toString());
}
}
ecClasss.close();
txn.commit();
entityStore.close();
env.cleanLog();
env.close();
}catch(Exception e){
e.printStackTrace();
}
}
}