both the methods (CreateRow() and CreateAndInitRow() ) mentioned above create and returns a new record.but the difference lies in the input parameter the createAndInitRow method takes.
the CreateAndInitRow takes a parameter of type NamedValuePair and uses those values to override the default values defined for the view object at design time.
to show the impact of this difference consider the following business case:
using HR schema. we have the following:
1)employees: have manager_id,department_id
2)departments :have department_id, manager_id
when adding a new employee to a department, the manager_id of the employee is set to the manager_id of the department.
this is achieved by defining a view accessor in the employee named Department. and setting the default value of the manager_id attribute of the employeesView to expression with the value:
Department.ManagerId
and the default value of the departmentId to 10(otherwise the above expression will throw a nullpointerexception ).
to test the above I wrote:
Number empId=new Number(546);
vo= module.findViewObject("EmployeesView1");
NameValuePairs nvp=new NameValuePairs();
nvp.setAttribute("EmployeeId",empId);
nvp.setAttribute("LastName","zamer");
nvp.setAttribute("Email","zamer@gmmail.com");
nvp.setAttribute("HireDate",new Date());
nvp.setAttribute("JobId","AD_VP");
nvp.setAttribute("DepartmentId",new Number(20));
ViewRowImpl newRow=(ViewRowImpl)vo.createAndInitRow(nvp);
vo.insertRow(newRow);
newRow=(ViewRowImpl)vo.createRow();
Number empI2=new Number(547);
newRow.setAttribute(0,empI2);
newRow.setAttribute(2,"zamer2");
newRow.setAttribute(3,"zamer@gmmail.com2");
newRow.setAttribute(5,new Date());
newRow.setAttribute(6,"AD_VP");
newRow.setAttribute("DepartmentId",new Number(20));
vo.insertRow(newRow);
module.getTransaction().commit();
String [] attrNames= newRow.getAttributeNames();
vo.executeQuery();
while(vo.hasNext()){
Row row=vo.next();
if(empId.equals(row.getAttribute(0))||empI2.equals(row.getAttribute(0))){
System.out.println(test.class+".main>>row:"+attrNames[0]+":"+row.getAttribute(0)+":"+attrNames[1]+":"+row.getAttribute(1)+":"+attrNames[2]+":"+row.getAttribute(2)+":"+attrNames[3]+":"+row.getAttribute(3)+":"+attrNames[4]+":"+row.getAttribute(4)+":"+attrNames[5]+":"+row.getAttribute(5)+":"+attrNames[6]+":"+row.getAttribute(6)+":"+attrNames[7]+":"+row.getAttribute(7)+":"+attrNames[8]+":"+row.getAttribute(8)+":"+attrNames[9]+":"+row.getAttribute(9)+":"+attrNames[10]+":"+row.getAttribute(10)+":"+attrNames[11]+":"+row.getAttribute(11)+":"+attrNames[12]+":"+row.getAttribute(12));
System.out.println();
}
}
the following was printed to the output:
class testing.test.main>>row:DepartmentId:10:DepartmentName:Administration:ManagerId:200:LocationId:1700
class testing.test.main>>row:DepartmentId:20:DepartmentName:Marketing:ManagerId:201:LocationId:1800
class testing.test.main>>creating
class testing.test.main>>row:EmployeeId:546:FirstName:null:LastName:zamer:Email:zamer@gmmail.com:PhoneNumber:null:HireDate:1970-01-01:JobId:AD_VP:Salary:null:CommissionPct:null:ManagerId:201:DepartmentId:20:DeptName:null:EmpVideo:null
class testing.test.main>>row:EmployeeId:547:FirstName:null:LastName:zamer2:Email:zamer@gmmail.com2:PhoneNumber:null:HireDate:1970-01-01:JobId:AD_VP:Salary:null:CommissionPct:null:ManagerId:200:DepartmentId:20:DeptName:null:EmpVideo:null
you can see that that department 10 has manager 200 and department 20 has manager 201
when we used createAndInitRow (employee id:546) the employee had the manager id:201
while when we used createRow (employee id:547) the employee had the manager id:200
this can be explained as follows:
since the expression is on a persistent attribute, the framework computes the default value, and this happens only once (at the creation of the record) using the available values at the time.
so since the department id at the creation of the record is the default value of 10 then the accessor will fetch that department.
as for CreateAndInitRow, the passed parameters will be used to override the default values, then any of the remaining attributes expressions(the attributes not included in the NamedValuePair ) will be evaluated ,so since the department id was changed to 20 , then the accessor will fetch department with the id 20.
so you can think of it this way,
createAndInitRow set the default values of the new row at runtime using the named value pair passed to it.
No comments:
Post a Comment