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

187 篇文章 2 订阅
开发环境: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值