您的位置:首页 > 编程语言

ADF-BC中EO常用操作代码之一:查询EO

2014-08-20 19:06 295 查看
开发环境:JDeveloper 11.1.2.2.0 + Oracle XE Database 10gR2。

ADF-BC中的EO对象一般来说不需要写代码,但在实际开发中,有时需要增加一些额外的操作,这就需要写代码了。

这里把一些常用情景的代码贴出来,供大家参考。

为了方便测试,我把代码写在了AppModuleImpl.java中,实际使用时可以写到EO的Impl.java中。

1. 私有方法:根据主键值获取EO对象

private EmployeesImpl retrieveEmployeeById(int employeeId) {
EntityDefImpl employeeDef = EmployeesImpl.getDefinitionObject();
//Key employeeKey = EmployeesImpl.createPrimaryKey(new Number(employeeId));
Key employeeKey = EmployeesImpl.createPrimaryKey(new Integer(employeeId));
return (EmployeesImpl)employeeDef.findByPrimaryKey(getDBTransaction(), employeeKey);
}

private DepartmentsImpl retrieveDepartmentById(int departmentId) {
EntityDefImpl departmentDef = DepartmentsImpl.getDefinitionObject();
//Key departmentKey = DepartmentsImpl.createPrimaryKey(new Number(departmentId));
Key departmentKey = DepartmentsImpl.createPrimaryKey(Integer.valueOf(departmentId));
return (DepartmentsImpl)departmentDef.findByPrimaryKey(getDBTransaction(), departmentKey);
}


说明:

(1)findByPrimaryKey()首先在缓冲区中查找与主键相匹配的实体对象;如果没有找到,再在数据库中查找,找到后,会把该实体对象放入缓存中。

(2)返回的对象是整个实体对象,而不是主键,因此可能会比较耗时。

(3)得到实体对象后,使用get方法可以访问该对象中的所有其它属性。

2. 公共方法

public String retrieveDeptNameByEmpId(int employeeId) {
EmployeesImpl employee = retrieveEmployeeById(employeeId);
if (employee != null) {
DepartmentsImpl department = (DepartmentsImpl)employee.getDepartments();
if (department != null) {
return department.getDepartmentName();
} else {
return "Unassigned";
}
} else {
return null;
}
}

public int retrieveEmpCountByDeptId(int departmentId) {
DepartmentsImpl department = retrieveDepartmentById(departmentId);
if (department != null) {
RowIterator employees = department.getEmployees();
return (employees.getRowCount());
//            Row row = null;
//            while ((employees.next()) != null) {
//                EmployeesImpl emp = (EmployeesImpl)row;
//            }
} else {
return 0;
}
}


说明:

(1)EmployeeEO和DepartmentEO的关联关系是在创建时根据表的主外键关系自动生成的,无需人工定义。

3. 把公共方法暴露在AM的Client Interface中,这样运行AM就可以进行测试

4. 运行AM测试

(1)右键AM,选择Show





(2)测试 retrieveDeptNameByEmpId





(3)测试 retrieveEmpCountByDeptId





Project 下载:ADF_BC_EO.7z

参考文献:

1. http://oracleseeker.com/2008/11/05/adf_find_entity_object_by_primary_key/
2. http://oracleseeker.com/2008/11/06/access_associated_entity_by_accessor_attribute/ http://maping930883.blogspot.com/2010/04/adf051adf-bceoeo.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: