Sunday, October 9, 2011

Validating The Entity Collection When You Commit the Transaction

In this post I'm sharing an approach to trigger the validation once for the entire entity collection when you commit the transaction. Steps :
  • Define a method validator for the entity object. In the Add Validation Rule dialog, select the Validation Execution tab, and choose Defer the execution to Transaction Level 
  • Implement the validation method in your entity implementation class. An example is shown below:
    /**
     * Validation method for Departments.
     */
    public boolean validateDepartments(ArrayList ctxList) {

        Iterator iter = ctxList.iterator();
        while (iter.hasNext()) {
            JboValidatorContext entValContext = (JboValidatorContext)iter.next();
            DepartmentsImpl deptEO = (DepartmentsImpl)entValContext.getSource();
            if (deptEO.getDepartmentName() == null) {
                // if Dept is null, throw error- this is just an example to illustrate the usage of this method
                return false;
            }
        }

        return true;
    }
  • Ope the page which uses the above entity, keep SkipValidation="skipDataControls" in the page definition file
Download

You can download the sample workspace from here. [Runs with Oracle JDeveloper 11.1.2.1.0 (11g R2PS1) + HR Schema]

How to run this sample?

1. Run test.jsf
2. Nullify Department name field in multiple rows and click Commit. Validation gets triggered only once for the entire collection when you press commit.

3 comments:

Peter O'Brien said...

Does the use of SkipValidation="skipDataControls" mean that all entity level validators are deferred or just those that have transactionlevel = true?

Jobinesh said...

Peter,
I can see SkipValidation="skipDataControls" skips both EO levl an transaction level validations in my sample. Waiting for dev team's response on this . Will keep you posted.

Jobinesh said...

This is a bug, please contact Oracle support if you are facing this issue in your App - this line is not for you Peter :-)