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));