Friday, January 25, 2013

EO Code Snippets for ADF

Creating an entity instance

    public DepartmentEOImpl createDeptEntity() {
        //Find the entity definition from the implementation class
        EntityDefImpl departmentEODef = DepartmentEOImpl.getDefinitionObject();
        //Create the blank entity instance
        DepartmentEOImpl newDept = (DepartmentEOImpl)departmentEODef.createInstance2(this.getDBTransaction(), null);
        newDept.setDepartmentId(1000);
        try {
            getDBTransaction().commit();
        } catch (JboException ex) {
            getDBTransaction().rollback();
            throw ex;
        }
        return newDept;
    }

Find and update an entity row

    public void findAndUpdateDeptEntity(Integer deptId) {
        //Create Key
        Key key = DepartmentEOImpl.createPrimaryKey(deptId);
        //Find the entity row
        DepartmentEOImpl deptRow =
            (DepartmentEOImpl)DepartmentEOImpl.getDefinitionObject().findByPrimaryKey(getDBTransaction(), key);
        deptRow.setDepartmentName("IT Admin");
    }

Remove an entity row

    public void findAndRemoveDeptEntity(Integer deptId) {
        Key key = DepartmentEOImpl.createPrimaryKey(deptId);
        DepartmentEOImpl deptRow = (DepartmentEOImpl)DepartmentEOImpl.getDefinitionObject().findByPrimaryKey(getDBTransaction(), key);
        deptRow.remove();
    }

Find the entity object by using primary key

    private DepartmentEOImpl findDepartmentById(int deptId) {
        //Find the entity definition from the implementation class
        EntityDefImpl departmentEODef = DepartmentEOImpl.getDefinitionObject();
        //Create the Key object
        Key orderKey = DepartmentEOImpl.createPrimaryKey(new Integer(deptId));
        //Find and return the desired instance
        return (DepartmentEOImpl)departmentEODef.findByPrimaryKey(getDBTransaction(), orderKey);
    }

To programmatically access the destination entities using the association accessor generated on entity implementation class

    private DepartmentEOImpl findDepartmentById(int deptId) {
        EntityDefImpl deptEODef = DepartmentEOImpl.getDefinitionObject();
        //Find Creates the Key to find Department
        Key deptIdKey = DepartmentEOImpl.createPrimaryKey(new Integer(deptId));
        //Find the Department entity using deptId
        DepartmentEOImpl deptEOImpl = (DepartmentEOImpl)deptEODef.findByPrimaryKey(getDBTransaction(), deptIdKey);
        //Access Employees for this departament using association accessor getEmpEO() generated on DepartmentEOImpl class
        RowIterator rowIter = DepartmentEOImpl.getEmpEO();
        while (rowIter.hasNext()) {
            Row row = rowIter.next();
            //Row represent Emp entity instance
            //Business logic goes here
        }
    }

No comments:

Post a Comment