官术网_书友最值得收藏!

Using doDML() to enforce a detail record for a new master record

In this recipe, we will consider a simple technique that we can use to enforce having detailed records when inserting a new master record in an entity association relationship. The use case demonstrates how to enforce creating at least one employee at the time when a new department is created.

Getting ready

We will use the HR database schema and the HRComponents workspace that we have created in previous recipes in this chapter.

How to do it...

  1. Open the DepartmentImpl custom entity implementation class and override the doDML() method using the Override Methods dialog.
  2. Add the following code to the doDML() method before the call to super.doDML():
    // check for insert
    if (DML_INSERT == operation) {
      // get the department employees accessor
      RowIterator departmentEmployees = this.getDepartmentEmployees();
      // check for any employees
      if (!departmentEmployees.hasNext()) {
        // avoid inserting the department if there are no employees for it
        throw new ExtJboException("00006");
      }
    }

How it works...

In the overridden doDML(), we only check for insert operations. This is indicated by comparing the DML operation flag which is passed as a parameter to doDML() to the DML_INSERT flag. Then we get the department employees from the DepartmentEmployees accessor by calling getDepartmentEmployees() . The DepartmentEmployees accessor was set up during the creation of the HRComponents workspace earlier in this chapter. We check whether the RowIterator returned has any rows by calling hasNext() on it. If this is not the case, that is, there are no employees associated with the specific department that we are about to insert, we alert the user by throwing an ExtJboException exception. The ExtJboException exception is part of the SharedComponets workspace and it was developed in the Using a custom exception class recipe back in Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations.

When testing the application module with the ADF Model Tester, we get the following error message when we try to insert a new department without any associated employees:

How it works...

Note

Note that in case that an exception is thrown during DML, which could result in partial data being posted to the database.

See also

  • Using a custom exception class, Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations
  • Overriding remove() to delete associated children entities, Chapter 1, Pre-requisites to Success: ADF Project Setup and Foundations
主站蜘蛛池模板: 尼木县| 苍梧县| 阜阳市| 酒泉市| 正宁县| 九寨沟县| 临漳县| 建平县| 襄樊市| 徐水县| 苏尼特左旗| 兴义市| 潞城市| 江城| 东光县| 霍州市| 玛多县| 比如县| 雷州市| 江孜县| 惠安县| 黄浦区| 图片| 临西县| 商洛市| 肥东县| 定日县| 裕民县| 邢台县| 宁夏| 康保县| 珲春市| 台湾省| 宜都市| 盘山县| 北京市| 扎囊县| 天水市| 西畴县| 鄢陵县| 宿松县|