BDB JE对复杂数据的储存 (三)、ManyToOne关系的存储
部门类:
@Entity
public class Department {
@PrimaryKey
int departmentId;
@SecondaryKey(relate=Relationship.ONE_TO_ONE)
String departmentName;
String location;
public Department( int departmentId,String departmentName,String location){
this.departmentId=departmentId;
this.departmentName=departmentName;
this.location=location;
}
Department(){ }
}
员工类:
@Entity
public class Employee {
@PrimaryKey
int employeeId;
@SecondaryKey(relate=Relationship.MANY_TO_ONE)
String employeeName;
@SecondaryKey(relate=Relationship.MANY_TO_ONE)
float salary;
@SecondaryKey
(relate=Relationship.MANY_TO_ONE,relatedEntity=Employee. class,onRelatedEntityDelete=DeleteActio
n.NULLIFY)
Integer managerId;
@SecondaryKey
(relate=Relationship.MANY_TO_ONE,relatedEntity=Department. class,onRelatedEntityDelete=DeleteAct
ion.CASCADE)
Integer departmentId;
String address;
public Employee( int employeeId,String employeeName, float salary,Integer managerId, int
departmentId,String address){
this.employeeId=employeeId;
this.employeeName=employeeName;
this.salary=salary;
this.managerId=managerId;
this.departmentId=departmentId;
this.address=address;
}
public Employee(){}
}
操作类:
public class Accessor {
PrimaryIndex employeeById;
SecondaryIndex employeeByName;
SecondaryIndex employeeBySalary;
SecondaryIndex employeeByManagerId;
SecondaryIndex employeeByDepartmentId;
PrimaryIndex departmentById;
SecondaryIndex departmentByName;
public Accessor(EntityStore store) throws DatabaseException{
employeeById=store.getPrimaryIndex(Integer. class, Employee. class);
employeeByName=store.getSecondaryIndex(employeeById, String. class, "employeeName");
employeeBySalary=store.getSecondaryIndex(employeeById, Float. class, "salary");
employeeByManagerId=store.getSecondaryIndex(employeeById, Integer. class, "managerId");
employeeByDepartmentId=store.getSecondaryIndex(employeeById, Integer. class,
"departmentId");
departmentById=store.getPrimaryIndex(Integer. class, Department. class);
departmentByName=store.getSecondaryIndex(departmentById, String. class,
"departmentName");
}
}
public class test {
public static void main(String[] args) {
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate( true);
envConfig.setTransactional( true);
try {
Environment env = new Environment( new File( "d://bdb//many2oneje2"),
envConfig);
StoreConfig storeConfig = new StoreConfig();
storeConfig.setAllowCreate(true);
storeConfig.setTransactional(true);
EntityStore store = new EntityStore(env, "EmployeeStore",
storeConfig);
Accessor dao = new Accessor(store);
Transaction txn = env.beginTransaction(null, null);
dao.departmentById.put(txn,new
Department(1,"CEO Office","North America"));
dao.departmentById.put(txn,new Department(2,"Sales","EURO"));
dao.departmentById.put(txn,new Department(3,"HR","MEA"));
dao.departmentById.put(txn,new
Department(4,"Engineering","APAC"));
dao.departmentById.put(txn,new Department(5,"Support","LATAM"));
dao.employeeById.put(txn,new
Employee(1,"Abraham Lincoln",1000.0f,
null,1,"Washington D.C.,USA")); dao.employeeById.put(txn,new
Employee(2,"Augustus",9000.0f,1,2,"Rome,Italy"));
dao.employeeById.put(txn,new
Employee(3,"Cleopartra",7000.0f,1,3,"Cairo,Egypt"));
dao.employeeById.put(txn,new
Employee(4,"Confucius",7500.0f,1,4,"Beijing,China"));
dao.employeeById.put(txn,new
Employee(5,"Toussaint Louverture",6800.0f
,1,5,"Port-au-prince,Haiti")); dao.employeeById.put(txn,new
Employee(6,"William Shakespeare",7300.0f,2,2,"London,Englad"));
dao.employeeById.put(txn,new
Employee(7,"Vistor Hugo",7000.0f,2,2,"paris,France"));
dao.employeeById.put(txn,new
Employee(8,"Yitzhak Rabin",6500.0f,3,3,"Jerusalem,Israel"));
dao.employeeById.put(txn,new
Employee(9,"Nelson Rolihlahla Mandela"
,6400.0f,3,3,"Cape Town,South Africa"));
dao.employeeById.put(txn,new
Employee(10,"Mei ji Emperor",6600.0f,4,4,"Tokyo,Japan"));
dao.employeeById.put(txn,new
Employee(11,"Mohandas Karamchand Gandhi"
,7600.0f,4,4,"New Delhi,India")); dao.employeeById.put(txn,new
Employee
(12,"Ayrton Senna da Silva",5600.0f,5,5,"Brasilia ,Brasil"));
dao.employeeById.put(txn,new
Employee(13,"Ronahlinho De Assis Moreiar"
,6100.0f,5,5,"Brasilia ,Brasil"));
// 通过游标得到实体
System.out.println("得到部门的信息,其按ID排序,升序");
EntityCursor deptcursor = dao.departmentById.entities(
txn, null);
for (Department dept : deptcursor) {
System.out.println(dept.departmentId + " "
+ dept.departmentName);
}
deptcursor.close();
System.out.println("得到部门的信息,其按部门名称排序,升序");
deptcursor = dao.departmentByName.entities(txn, null);
for (Department dept : deptcursor) {
System.out.println(dept.departmentId + " "
+ dept.departmentName);
}
deptcursor.close();
System.out.println("******************************");
// 通过get得到实体
System.out.println("得到部门id为1的信息");
Department d = dao.departmentById.get(txn, 1, LockMode.DEFAULT);
System.out.println(d.departmentId + " " + d.departmentName);
System.out.println("--------/////////////////////------");
// 通过关键字
System.out.println("得到部门名称为sales的信息");
d = dao.departmentByName.get(txn, "Sales", LockMode.DEFAULT);
if (d != null) {
System.out.println(d.departmentId + " " + d.departmentName);
}
System.out.println("-------------------");
// 限制游标的范围
// 第一个参数代表为开始位置,按ACSII码查询,第二个参数代表(trur:>= false:>)
// 第三个参数代表为结束位置,按ACSII码结束,其为一个字符的比较,第四个为(true:<=
false:<)
deptcursor = dao.departmentByName.entities(txn, "C", true, "Sales",
true, null);
for (Department dept : deptcursor) {
System.out.println(dept.departmentName);
}
deptcursor.close();
System.out.println("----------------------------");
deptcursor = dao.departmentByName.subIndex("Sales").entities(txn,
null);
for (Department dept : deptcursor) {
System.out.println(dept.departmentId + " "
+ dept.departmentName);
}
deptcursor.close();
System.out
.println("得到部门名为Sales,其ID为2的部门信息(如果get中为其他的值的时候都会报
错,因为部门名为Sales的部门ID为2)");
d = dao.departmentByName.subIndex("Sales").get(txn, 2,
LockMode.DEFAULT);
if (d != null) {
System.out.println(d.departmentId + " " + d.departmentName);
}
// dao.departmentById.delete(1);
System.out
.println("////////////////////////////////////////////////");
// 得到第一个部门的名称
d = dao.departmentById.get(txn, 1, LockMode.DEFAULT);
System.out.println(d.departmentName);
System.out
.println("************************************************");
// 得到部门ID为1的第一个员工的名字
System.out.println("得到部门ID为2的第一个员工的名字:");
System.out.println(dao.employeeByDepartmentId.get(txn, 2,
LockMode.DEFAULT).employeeName);
System.out.println("+++++++++++++++++++++++++++++++++++++");
System.out.println("查询工资在6000-8000之间的所有员工;");
EntityCursor ecursor = dao.employeeBySalary.entities(txn,
new Float(6000), true, new Float(8000), true, null);
for (Employee e : ecursor) {
System.out.println(e.employeeId + " " + e.salary);
}
ecursor.close();
System.out.println("-------------------");
// 得到上司的编号为2的第一个员工的名称
System.out.println("得到上司的编号为2的第一个员工的名称:");
System.out.println(dao.employeeByManagerId.get(txn, 2,
LockMode.DEFAULT).employeeName);
System.out
.println("------------------------------------------------------------------
---");
// 查询公司的所有的员工
System.out.println("查询公司的所有的员工:");
System.out.println("编号 " + "名称" + " " + "工资" + " " + "地址"
+ " " + "部门ID" + " " + "上司ID");
EntityCursor employeeCursor = dao.employeeById.entities(
txn, null);
for (Employee e : employeeCursor) {
System.out.println(e.employeeId + " " + e.employeeName + " "
+ e.salary + " " + e.address + " " + e.departmentId
+ " " + e.managerId);
}
employeeCursor.close();
System.out.println("---------------------");
// 查询上司编号为1,并且工作为7000.0f的员工
System.out.println("查询上司编号为1,并且工作为7000.0f的员工:");
EntityJoin entityJoin = new EntityJoin(
dao.employeeById);
entityJoin.addCondition(dao.employeeByManagerId, 1);
entityJoin.addCondition(dao.employeeBySalary, 7000.0f);
ForwardCursor c = entityJoin.entities(txn, null);
for (Employee e : c) {
System.out.println(e.employeeId + " " + e.employeeName
+ " " + e.salary + " " + e.managerId);
}
c.close();
System.out.println("===========================================");
// 联合查询(同一部门中的所有人员)
System.out.println("部门名为Support中的所有人员");
Department department = dao.departmentByName.get(txn, "Support",
LockMode.DEFAULT);
if (department != null) {
EntityCursor ec = dao.employeeByDepartmentId
.subIndex(department.departmentId).entities(txn, null);
for (Employee emp : ec) {
System.out.println(emp.employeeId + " "
+ emp.employeeName + " " + emp.address);
}
ec.close();
}
System.out.println("相应的部门中所有员工显示:");
EntityCursor deptc = dao.departmentById.entities(txn,
null);
for (Department dd : deptc) {
EntityCursor eec = dao.employeeByDepartmentId
.subIndex(dd.departmentId).entities(txn, null);
for (Employee emp : eec) {
System.out.println(emp.employeeId + " "
+ emp.employeeName + " " + emp.address + " "
+ dd.departmentName);
}
eec.close();
}
deptc.close();
System.out
.println("删除部门名为:Support,由于用到了CASCADE,在删除部门时,会把对应的部门
里的员工也会删除掉");
deptc = dao.departmentById.entities(txn, null);
for (Department dd : deptc) {
if (dd.departmentName.trim().equals("Support")) {
// 最好用游标来进行删除和修改的操作
deptc.delete();
}
if (dd.departmentName.trim().equals("Sales")) {
dd.departmentName = dd.departmentName.toUpperCase();
deptc.update(dd);
}
}
deptc.close();
System.out.println("相应的部门中所有员工显示:");
deptc = dao.departmentById.entities(txn, null);
for (Department dd : deptc) {
EntityCursor eec = dao.employeeByDepartmentId
.subIndex(dd.departmentId).entities(txn, null);
for (Employee emp : eec) {
System.out.println(emp.employeeId + " "
+ emp.employeeName + " " + emp.address + " "
+ dd.departmentName);
}
eec.close();
}
deptc.close();
store.close();
txn.commit();
env.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Department {
@PrimaryKey
int departmentId;
@SecondaryKey(relate=Relationship.ONE_TO_ONE)
String departmentName;
String location;
public Department( int departmentId,String departmentName,String location){
this.departmentId=departmentId;
this.departmentName=departmentName;
this.location=location;
}
Department(){ }
}
员工类:
@Entity
public class Employee {
@PrimaryKey
int employeeId;
@SecondaryKey(relate=Relationship.MANY_TO_ONE)
String employeeName;
@SecondaryKey(relate=Relationship.MANY_TO_ONE)
float salary;
@SecondaryKey
(relate=Relationship.MANY_TO_ONE,relatedEntity=Employee. class,onRelatedEntityDelete=DeleteActio
n.NULLIFY)
Integer managerId;
@SecondaryKey
(relate=Relationship.MANY_TO_ONE,relatedEntity=Department. class,onRelatedEntityDelete=DeleteAct
ion.CASCADE)
Integer departmentId;
String address;
public Employee( int employeeId,String employeeName, float salary,Integer managerId, int
departmentId,String address){
this.employeeId=employeeId;
this.employeeName=employeeName;
this.salary=salary;
this.managerId=managerId;
this.departmentId=departmentId;
this.address=address;
}
public Employee(){}
}
操作类:
public class Accessor {
PrimaryIndex
SecondaryIndex
SecondaryIndex
SecondaryIndex
SecondaryIndex
PrimaryIndex
SecondaryIndex
public Accessor(EntityStore store) throws DatabaseException{
employeeById=store.getPrimaryIndex(Integer. class, Employee. class);
employeeByName=store.getSecondaryIndex(employeeById, String. class, "employeeName");
employeeBySalary=store.getSecondaryIndex(employeeById, Float. class, "salary");
employeeByManagerId=store.getSecondaryIndex(employeeById, Integer. class, "managerId");
employeeByDepartmentId=store.getSecondaryIndex(employeeById, Integer. class,
"departmentId");
departmentById=store.getPrimaryIndex(Integer. class, Department. class);
departmentByName=store.getSecondaryIndex(departmentById, String. class,
"departmentName");
}
}
public class test {
public static void main(String[] args) {
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate( true);
envConfig.setTransactional( true);
try {
Environment env = new Environment( new File( "d://bdb//many2oneje2"),
envConfig);
StoreConfig storeConfig = new StoreConfig();
storeConfig.setAllowCreate(true);
storeConfig.setTransactional(true);
EntityStore store = new EntityStore(env, "EmployeeStore",
storeConfig);
Accessor dao = new Accessor(store);
Transaction txn = env.beginTransaction(null, null);
dao.departmentById.put(txn,new
Department(1,"CEO Office","North America"));
dao.departmentById.put(txn,new Department(2,"Sales","EURO"));
dao.departmentById.put(txn,new Department(3,"HR","MEA"));
dao.departmentById.put(txn,new
Department(4,"Engineering","APAC"));
dao.departmentById.put(txn,new Department(5,"Support","LATAM"));
dao.employeeById.put(txn,new
Employee(1,"Abraham Lincoln",1000.0f,
null,1,"Washington D.C.,USA")); dao.employeeById.put(txn,new
Employee(2,"Augustus",9000.0f,1,2,"Rome,Italy"));
dao.employeeById.put(txn,new
Employee(3,"Cleopartra",7000.0f,1,3,"Cairo,Egypt"));
dao.employeeById.put(txn,new
Employee(4,"Confucius",7500.0f,1,4,"Beijing,China"));
dao.employeeById.put(txn,new
Employee(5,"Toussaint Louverture",6800.0f
,1,5,"Port-au-prince,Haiti")); dao.employeeById.put(txn,new
Employee(6,"William Shakespeare",7300.0f,2,2,"London,Englad"));
dao.employeeById.put(txn,new
Employee(7,"Vistor Hugo",7000.0f,2,2,"paris,France"));
dao.employeeById.put(txn,new
Employee(8,"Yitzhak Rabin",6500.0f,3,3,"Jerusalem,Israel"));
dao.employeeById.put(txn,new
Employee(9,"Nelson Rolihlahla Mandela"
,6400.0f,3,3,"Cape Town,South Africa"));
dao.employeeById.put(txn,new
Employee(10,"Mei ji Emperor",6600.0f,4,4,"Tokyo,Japan"));
dao.employeeById.put(txn,new
Employee(11,"Mohandas Karamchand Gandhi"
,7600.0f,4,4,"New Delhi,India")); dao.employeeById.put(txn,new
Employee
(12,"Ayrton Senna da Silva",5600.0f,5,5,"Brasilia ,Brasil"));
dao.employeeById.put(txn,new
Employee(13,"Ronahlinho De Assis Moreiar"
,6100.0f,5,5,"Brasilia ,Brasil"));
// 通过游标得到实体
System.out.println("得到部门的信息,其按ID排序,升序");
EntityCursor
txn, null);
for (Department dept : deptcursor) {
System.out.println(dept.departmentId + " "
+ dept.departmentName);
}
deptcursor.close();
System.out.println("得到部门的信息,其按部门名称排序,升序");
deptcursor = dao.departmentByName.entities(txn, null);
for (Department dept : deptcursor) {
System.out.println(dept.departmentId + " "
+ dept.departmentName);
}
deptcursor.close();
System.out.println("******************************");
// 通过get得到实体
System.out.println("得到部门id为1的信息");
Department d = dao.departmentById.get(txn, 1, LockMode.DEFAULT);
System.out.println(d.departmentId + " " + d.departmentName);
System.out.println("--------/////////////////////------");
// 通过关键字
System.out.println("得到部门名称为sales的信息");
d = dao.departmentByName.get(txn, "Sales", LockMode.DEFAULT);
if (d != null) {
System.out.println(d.departmentId + " " + d.departmentName);
}
System.out.println("-------------------");
// 限制游标的范围
// 第一个参数代表为开始位置,按ACSII码查询,第二个参数代表(trur:>= false:>)
// 第三个参数代表为结束位置,按ACSII码结束,其为一个字符的比较,第四个为(true:<=
false:<)
deptcursor = dao.departmentByName.entities(txn, "C", true, "Sales",
true, null);
for (Department dept : deptcursor) {
System.out.println(dept.departmentName);
}
deptcursor.close();
System.out.println("----------------------------");
deptcursor = dao.departmentByName.subIndex("Sales").entities(txn,
null);
for (Department dept : deptcursor) {
System.out.println(dept.departmentId + " "
+ dept.departmentName);
}
deptcursor.close();
System.out
.println("得到部门名为Sales,其ID为2的部门信息(如果get中为其他的值的时候都会报
错,因为部门名为Sales的部门ID为2)");
d = dao.departmentByName.subIndex("Sales").get(txn, 2,
LockMode.DEFAULT);
if (d != null) {
System.out.println(d.departmentId + " " + d.departmentName);
}
// dao.departmentById.delete(1);
System.out
.println("////////////////////////////////////////////////");
// 得到第一个部门的名称
d = dao.departmentById.get(txn, 1, LockMode.DEFAULT);
System.out.println(d.departmentName);
System.out
.println("************************************************");
// 得到部门ID为1的第一个员工的名字
System.out.println("得到部门ID为2的第一个员工的名字:");
System.out.println(dao.employeeByDepartmentId.get(txn, 2,
LockMode.DEFAULT).employeeName);
System.out.println("+++++++++++++++++++++++++++++++++++++");
System.out.println("查询工资在6000-8000之间的所有员工;");
EntityCursor
new Float(6000), true, new Float(8000), true, null);
for (Employee e : ecursor) {
System.out.println(e.employeeId + " " + e.salary);
}
ecursor.close();
System.out.println("-------------------");
// 得到上司的编号为2的第一个员工的名称
System.out.println("得到上司的编号为2的第一个员工的名称:");
System.out.println(dao.employeeByManagerId.get(txn, 2,
LockMode.DEFAULT).employeeName);
System.out
.println("------------------------------------------------------------------
---");
// 查询公司的所有的员工
System.out.println("查询公司的所有的员工:");
System.out.println("编号 " + "名称" + " " + "工资" + " " + "地址"
+ " " + "部门ID" + " " + "上司ID");
EntityCursor
txn, null);
for (Employee e : employeeCursor) {
System.out.println(e.employeeId + " " + e.employeeName + " "
+ e.salary + " " + e.address + " " + e.departmentId
+ " " + e.managerId);
}
employeeCursor.close();
System.out.println("---------------------");
// 查询上司编号为1,并且工作为7000.0f的员工
System.out.println("查询上司编号为1,并且工作为7000.0f的员工:");
EntityJoin
dao.employeeById);
entityJoin.addCondition(dao.employeeByManagerId, 1);
entityJoin.addCondition(dao.employeeBySalary, 7000.0f);
ForwardCursor
for (Employee e : c) {
System.out.println(e.employeeId + " " + e.employeeName
+ " " + e.salary + " " + e.managerId);
}
c.close();
System.out.println("===========================================");
// 联合查询(同一部门中的所有人员)
System.out.println("部门名为Support中的所有人员");
Department department = dao.departmentByName.get(txn, "Support",
LockMode.DEFAULT);
if (department != null) {
EntityCursor
.subIndex(department.departmentId).entities(txn, null);
for (Employee emp : ec) {
System.out.println(emp.employeeId + " "
+ emp.employeeName + " " + emp.address);
}
ec.close();
}
System.out.println("相应的部门中所有员工显示:");
EntityCursor
null);
for (Department dd : deptc) {
EntityCursor
.subIndex(dd.departmentId).entities(txn, null);
for (Employee emp : eec) {
System.out.println(emp.employeeId + " "
+ emp.employeeName + " " + emp.address + " "
+ dd.departmentName);
}
eec.close();
}
deptc.close();
System.out
.println("删除部门名为:Support,由于用到了CASCADE,在删除部门时,会把对应的部门
里的员工也会删除掉");
deptc = dao.departmentById.entities(txn, null);
for (Department dd : deptc) {
if (dd.departmentName.trim().equals("Support")) {
// 最好用游标来进行删除和修改的操作
deptc.delete();
}
if (dd.departmentName.trim().equals("Sales")) {
dd.departmentName = dd.departmentName.toUpperCase();
deptc.update(dd);
}
}
deptc.close();
System.out.println("相应的部门中所有员工显示:");
deptc = dao.departmentById.entities(txn, null);
for (Department dd : deptc) {
EntityCursor
.subIndex(dd.departmentId).entities(txn, null);
for (Employee emp : eec) {
System.out.println(emp.employeeId + " "
+ emp.employeeName + " " + emp.address + " "
+ dd.departmentName);
}
eec.close();
}
deptc.close();
store.close();
txn.commit();
env.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}