Wednesday, April 21, 2010

Bit #14 - Using a Key to locate a Row in a View Object

Instead of iterating a View Object using a RowSetIterator - as described in Bit #4 - Iterating a View Object using a secondary RowSetIterator, you can locate a row directly using the ViewObject method findByKey(). This will work as long as you indicate a View Object attribute as a Key Attribute. To use this method, you will need to first instantiate a jbo.Key object and then pass it as an argument to findByKey(). findByKey() will return an array of rows that match the Key object. Special attention should be given when constructing Key objects for multi-part keys and for View Objects that are based on more than one Entity Objects. These cases are explained in detail in the Oracle Fusion Middleware Java API Reference for Oracle ADF Model documentation referenced below.

Example:

        // in the context of the ApplModuleImpl
      
        // locate the employee's department
        Number departmentId =
            ((EmployeesRowImpl)(this.getEmployees().getCurrentRow())).getDepartmentId();
        Key keyDepartment = new Key(new Object[] { departmentId });
      
        // the second argument indicates the maximum number of rows to return
        Row[] departments = this.getDepartments().findByKey(keyDepartment, 1);
        if (departments != null && departments.length > 0) {
            DepartmentsRowImpl department = (DepartmentsRowImpl)departments[0];
          
            // you can access the Department's attributes here....
        }

Context:

Application Module Implementation Class
View Object Implementation Class

Reference:

Oracle Fusion Middleware Java API Reference for Oracle ADF Model, findByKey

3 comments:

  1. What in case when the View Object row hasn't defined the key? How I can create the key based on some column on View Object? Thanks

    ReplyDelete
  2. A View Object should always define a Key column. You can indicate whether an attribute is a Key attribute by clicking on the Key Attribute checkbox in JDeveloper.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...