mercredi 22 avril 2009

ADF BC : Two ways of inserting rows

A good habit when developing is to keep in mind the Service Oriented Architecture (SOA). With ADF, your service layer is (are) application(s) module(s). With it, you expose views and methods. Methods could be : getManagerId, setManagerNameById, ... but not only getters and setters. You could have createManager, deleteManagerById, ...

Today I'm going to explain two methods for creating a new row.

1. createRow

public String myOwnCreateInsert_Person (int pid, String pname)
{
ViewObjectImpl vo = this.getPersonsUpdatable1();

try {
Row r = vo.createRow();
r.setAttribute("PersonId", pid);
r.setAttribute("PersonName", pname);
vo.insertRow(r);
}
catch (oracle.jbo.TooManyObjectsException tmoe) {
return "error";
}

this.getDBTransaction().commit();

return "success";
}


2. createAndInitRow

public String myOwnCreateInsert_Person (int pid, String pname)
{
ViewObjectImpl vo = this.getPersonsUpdatable1();

try {
NameValuePairs nvp = new NameValuePairs();
nvp.setAttribute("PersonId", pid);
nvp.setAttribute("PersonName", pname);
vo.insertRow(vo.createAndInitRow(nvp));
}
catch (oracle.jbo.TooManyObjectsException tmoe) {
return "error";
}

this.getDBTransaction().commit();

return "success";
}


---

Not to mention the second one is my favourite ; but it's doing the exact same thing. Why ? Check here.

Also, if you insert the new row in a table that contains a foreign key column, you should add this :

catch (oracle.jbo.DMLConstraintException dmle) {
return "error";
}

1 commentaire:

Anonyme a dit…

The link you mention is invalid URL :)