Controlling Entity posting order or commit order in ADF

Hi All,

In this blog I am going to show how you can control the entity posting order to database or in simple way you can say how we can control commit order of Entity Objects in ADF.



Scenario : Suppose if you have two table like EMPLOYEES and DEPARTMENTS. You have created Entity Object EmployeesEO and DepartmentsEO. Now in doDML INSERT method of the EmployeesEO you want to insert the row in DepartmentEO and commit it to database. Now if you have some trigger on EMPLOYEES table which is checking the newly inserted row and throwing error if it is not there.

Suppose your trigger code is like this:

  CREATE OR REPLACE TRIGGER "HR"."EMP_DEPT_TRG"

  BEFORE INSERT ON employees

  FOR EACH ROW

  DECLARE

  pcount NUMBER := 0;

  BEGIN



 IF (:NEW.DEPARTMENT_ID != 0) THEN

    SELECT COUNT(1)

         INTO pcount

         FROM DEPARTMENTS

        WHERE DEPARTMENT_ID = :NEW.DEPARTMENT_ID;



     IF (pcount = 0) THEN

        RAISE_APPLICATION_ERROR(-20006,'Department ID for this Employee is not existing.');

     END IF;

     else if (:NEW.DEPARTMENT_ID IS NULL) THEN

      RAISE_APPLICATION_ERROR(-20006,'Department ID for this Employee is not existing.');

      END IF;

  END IF;

  END;


So now when you try to insert the record in EMPLOYEES table from ADF and since there is no corresponsing DEPARTMENT_ID in the DEPARTMENTS table. It will throw the exception which is written inside the trigger as shown below:





Although I have written the code for inserting the record in DepartmentsEO but still it is throwing error as the data is not committed to DEPARTMENTS table and it is trying to insert the Employees record before department. So the trigger on department is getting failed and is throwing error.
Following is the code I have written in doDML method of EmployeesEO to insert record in DepartmentsEO.

    protected void doDML(int operation, TransactionEvent e) {

        if(null==getDepartmentId()){

                           setDepartmentId(getSequenceValue());

                           DepartmentsEOImpl deptRow=(DepartmentsEOImpl)getDepartmentsEO().createRow();

                            deptRow.setDepartmentId(getDepartmentId());

                            deptRow.setDepartmentName("TEST DEP");

                            getDepartmentsEO().insertRow(deptRow);                        }

        super.doDML(operation, e);

    }

    public Number getSequenceValue(){

            SequenceImpl seq=new SequenceImpl("DEPARTMENTS_SEQ",getDBTransaction());

            return seq.getSequenceNumber();

        }

So in this case we have to control the sequence or order of commit in EO. To do so we have to call the postChanges() method on DepartmentsEO before calling the super.doDML() method.
So our code will look like this in this case:

    protected void doDML(int operation, TransactionEvent e) {
        if(null==getDepartmentId()){
                           setDepartmentId(getSequenceValue());
                           DepartmentsEOImpl deptRow=(DepartmentsEOImpl)getDepartmentsEO().createRow();
                            deptRow.setDepartmentId(getDepartmentId());
                            deptRow.setDepartmentName("TEST DEP");
                            getDepartmentsEO().insertRow(deptRow);
                           deptRow.postChanges(e);
                        }
        super.doDML(operation, e);
    }

Now after calling this postChanges() method on DepartmentsEO it will ensure commit the record in Departments first and then it will commit the record in Employees.

 Now if you will try to insert the record in EmployeesVO then it will be successfully inserted.


That's all about it. 

Happy Coding :)

Comments

Popular posts from this blog

Setting up the environment for Angular2 and Hello World Example in Angular2

Showing number of rows or row count on top and bottom of table in ADF.

Build a Simple ReactJS application using react-cli