コードでのビュー属性値の設定

次のコード例では、ビジネス・コンポーネントの属性を設定することにより、データベース表内の値を更新しています。この例では、汎用アプリケーション・モジュールを使用し、属性を設定するためにRow.setAttributeをコールしています。

コードの次の点に注意してください。

package d2e;
import oracle.jbo.*;
import oracle.jbo.server.*;
import oracle.jbo.domain.Number;
public class SetAttrDemo {
public static void main(String[] args) {
    // Helper routine connects to the generic application module.
ApplicationModule appMod =
QueryDemo.getGenericAppMod(JboContext.PLATFORM_LOCAL);
    // Specify the XML file that defines the view object.
// Format: <package>
String voDefFile = "d2e.EmpView";
    // Identify the view object. Must be a valid Java identifier.
String voName = "demoEmpVO";
    // Create the view object within the context defined 
// by the application module.
ViewObject vo = appMod.createViewObject(voName, voDefFile);
Number newVal = new Number(6543);
if (updateAttr(vo, newVal)) {
      try {
// Commit changes to the database, making
// updated data available to other application modules.
appMod.getTransaction().commit();
System.out.println("\n Transaction committed. \n");
}
catch (JboException ave) {
        // Handle Entity-level validation exceptions here.
System.out.println("\n (Entity) Invalid value: " + newVal);
throw ave;
}
}
}
  public static boolean updateAttr(ViewObject vo, Object newVal) {
boolean res = false;
       // Optional: specify a WHERE clause to narrow the query.
vo.setWhereClause("EMPNO = 7369");
       // Execute the query, set new attribute value. Only one row 
// should be returned, so use vo.first().
vo.executeQuery();
while (vo.hasNext()) {
Row row = vo.first();
          try {
row.setAttribute("Sal", newVal);
}
catch (JboException ave) {
          // Handle Attribute-level validation exceptions here.
System.out.println("\n (Attribute) Invalid value: " + newVal);
throw ave;
}
res = true;
}
return res;
}
}