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对象
说明:
(1)findByPrimaryKey()首先在缓冲区中查找与主键相匹配的实体对象;如果没有找到,再在数据库中查找,找到后,会把该实体对象放入缓存中。
(2)返回的对象是整个实体对象,而不是主键,因此可能会比较耗时。
(3)得到实体对象后,使用get方法可以访问该对象中的所有其它属性。
2. 公共方法
说明:
(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
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
相关文章推荐
- ADF-BC中EO常用操作代码之二:修改和删除EO
- ADF-BC中EO常用操作代码之三:增加EO(1)
- 使用ADF-BC 实现查询功能之八:通过代码动态设置Criteria条件参数
- 使用ADF-BC 实现查询功能之四:通过代码动态设置Where条件参数
- Php mysql 常用代码、CURD操作以及简单查询
- ASP编程中连接数据库和数据库操作的常用代码
- ASP编程中连接数据库和数据库操作的常用代码
- 操作SQL Server Mobile数据库的常用C#代码
- asp 获取access系统表,查询等操作代码
- 常用数据库建表操作SQL代码
- SQL 后台操作常用代码
- 操作SQL Server Mobile数据库的常用C#代码
- 操作 SQL Server Mobile 2005 数据库的常用 C# 代码
- 操作SQL Server Mobile数据库的常用C#代码
- ASP.NET日期操作常用代码
- 日期操作常用代码
- 操作 SQL Server Mobile 2005 数据库的常用 C# 代码
- 操作SQL Server Mobile数据库的常用C#代码
- 操作SQL Server Mobile数据库的常用C#代码
- 转 操作 SQL Server Mobile 2005 数据库的常用 C# 代码