Wednesday, December 18, 2013

Force another EO to execute before an EO

Today, I experienced an interesting problem, I want to post another EO while executing an EO.

Example Problem is,

Consider a table Employee & EmployeeRel

Here while creating the an EmployeeRel record, I may choose an existing employee or create a new employee and set the s_empId., But If we do so, we will get the Integrity constraint exception, because the EmployeeRel record insert query is executed before the new Employee insert query.(We are not using association here for the new Employee)

So, We need to execute the new Employee insert query before EmployeeRel record. ADF framework allows us to use the life cycle method PostChanges() to forece the new Employee insert query execution. 

So override the postChanges() in the EmployeeRelImpl as follows,

    public void postChanges(TransactionEvent e) {
      if ((getPostState() == STATUS_NEW) || (getPostState() == STATUS_MODIFIED)) {
        EmployeeImpl secondEmp = getSecondEmployee(this.getSEmpId());
        if (secondEmp != null) {
          if (secondEmp .getPostState() == STATUS_NEW) {
            secondEmp .postChanges(e);
          }
        }
      }
      super.postChanges(e);
    }
    
    private PartyImpl getSecondEmployee(Number secondEmpId){
        Key key=EmployeeImpl .createPrimaryKey(secondEmpId);
        return (EmployeeImpl )EmployeeImpl .getDefinitionObject().findByPrimaryKey(getDBTransaction(), key);            
    }


Here is the sample DB script, I've used for my sample.

create table Emp(name varchar(20),emp_id number(10) primary key);
create table Emp_rel(rel_id number(10),f_empid number(10) REFERENCES Emp(emp_id),s_empid number(10) references Emp(emp_id));



  

Monday, December 9, 2013

ADF Association composition behaviors


If we select Implement cascade delete operation,
  • First it will generate delete query on the child entities and invoke the delete query.
  • Next it will generate delete query on the parent entity and invoke the delete query.
  • Then it will issue commit.

If we select optimize for Database cascade delete operation and in the foreign key ON DELETE CASCADE
  •  First it will generate delete query on the parent entity and invoke the delete query
  •  While deleting the parent record, it will delete child the child records at the DB level as we have implemented ON DELETE CASCADE.
  • It will issue commit.
  • It will clear the child entities from the cache for that parent record.

If we select optimize for Database cascade delete operation and in the foreign key we don’t set ON DELETE CASCADE
  • First It will generate delete query on the parent entity and invoke the delete query.
  • While deleting the parent record, it will throw Database constraint exception as we have not implemented ON DELETE CASCADE and SQL Exception will be thrown.

If we select Cascade update key attributes,
·         When updating the parent key attributes, it will update the child record’s foreign key attributes.

If we don’t select Cascade update key attributes, it will throw exceptions while trying to update the parent records primary key.

If we select update Top level history columns, it will update the parent records history columns when we are trying to add/update child records.

If we select Lock level to Lock Container, While updating the child record it will lock the immediate parent record.

If we select Lock level to Lock Container, While updating the child record it will lock the all the parent records in the hierarchy.

Sunday, December 1, 2013

Refactoring DataBindings.cpx file and Having Multiple cpx files

I found some interesting facts while I was playing with DataBindings.cpx file this weekend and I'm blogging them now after a long time.
  • Renaming the default DataBindings.cpx file.
  • Having more than one DataBindings file in a project.
       As you know the DataBindings.cpx file plays a vital role in the ADF Model layer, which is used to create the BindingContext for the Web Application by ADFBindingFilter and this stores the meta info about page defintions mappings to create the Binding Container for ADF data bound pages and DataControl usages to find the appropriate AppModules and ErrorHanler config,etc.

       JDeveloper does not provide declarative facility to rename or move the DataBindings.cpx file. To rename or move the DataBindings.cpx file, just create the new one and delete the existing one. While doing this don't forgot to update the adfm.xml file which is the registry of registries and always have the id property is same as the DataBindings cpx file name otherwise you will get error message  "DataControl is not found".

For example if you are renaming the cpx file from DataBindings.cpx to MyDataBindings.cpx, update the id as follows.

DataBindings.cpx file

<?xml version="1.0" encoding="UTF-8" ?>

<Application xmlns="http://xmlns.oracle.com/adfm/application"
             version="11.1.1.61.92" id="DataBindings" SeparateXMLFiles="false"
             Package="view" ClientType="Generic">
.........................
.........................
</Application>

MyDataBindings.cpx

<?xml version="1.0" encoding="UTF-8" ?>
<Application xmlns="http://xmlns.oracle.com/adfm/application"
             version="11.1.1.61.92" id="MyDataBindings" SeparateXMLFiles="false"
             Package="view" ClientType="Generic">
.........................
.........................
</Application>

         If you want to have more than one DataBindings.cpx file in a project, then  add the entries in the adfm.xml file, because ADFBindingFilter looks for the cpx files in the adfm.xml file. Cpx file names should be unique within the project.

Sample adfm.xml for multiple cpx files.

<?xml version="1.0" encoding="UTF-8" ?>

<MetadataDirectory xmlns="http://xmlns.oracle.com/adfm/metainf"                                      
                                                             version="11.1.1.0.0">
  <DataBindingRegistry path="view/DataBind1.cpx"/>
  <DataBindingRegistry path="view/DataBind1.cpx"/>
  <DataBindingRegistry path="view1/DataBind2.cpx"/>
</MetadataDirectory>



Note:  It is best practice to have cpx file name as DataBindings.cpx and having only one cpx file if we don't have special use cases.