- Oracle JDeveloper 11gR2 Cookbook
- Nick Haralabidis
- 682字
- 2025-04-04 23:25:00
Overriding remove() to delete a parent entity in an association
In this recipe, we will present a technique that you can use in cases that you want to delete the parent entity in an association when the last child entity is deleted. An example of such a case would be to delete a department when the last department employee is deleted.
How to do it...
- Start by creating a new Fusion Web Application (ADF) workspace called
HRComponents
. - Create a database connection for the
HR
schema in the Application Resource section of the Application Navigator. - Use the Business Components from Tables selection on the New Gallery dialog to create
Business Components
objects for theDEPARTMENTS
andEMPLOYEES
tables. - Double-click on the EmpDeptFkAssoc association on the Application Navigator to bring up the Association editor, then click on the Relationship tab.
- Click on the Edit accessors button (the pen icon) in the Accessors section to bring up the Association Properties dialog.
- Change the Accessor Name in the Source Accessor section to EmployeeDepartment and click OK to continue.
- Generate custom Java implementation classes for both the
Employee
andDepartment
entity objects. - Open the
EmployeeImpl
custom Java implementation class for theEmployee
entity object and locate theremove()
method. - Replace the call to
super.remove()
with the following code:// get the associated department DepartmentImpl department = this.getEmployeeDepartment(); // get number of employees in the department int numberOfEmployees = department.getDepartmentEmployees().getRowCount(); // check whether last employee in the department if (numberOfEmployees == 1) { // delete the last employee super.remove(); // delete the department as well department.remove(); } else { // just delete the employee super.remove(); }
How it works...
If you followed the Overriding remove() to delete associated children entities recipe in this chapter, then steps 1 through 8 should look familiar. These are the basic steps to create the HRComponents
workspace, along with the business components associated with the EMPLOYEES
and DEPARTMENTS
tables in the HR
schema. These steps also create custom Java implementation classes for the Employee
and Department
entity objects and setup the EmpDeptFkAssoc
association.
The code in remove()
first gets the Department
entity row by calling the accessor getEmployeeDepartment()
method. Remember, this was the name of accessor—EmployeeDepartment
—that we setup in step 6. getEmployeeDepartment()
returns the custom DepartmentImpl
that we setup in step 7. In order to determine the number of employees in the associated Department
, we first get the Employee RowIterator
by calling getDepartmentEmployees()
on it, and then getRowCount()
on the RowIterator
. All that is done in the following statement:
int numberOfEmployees = department.getDepartmentEmployees().getRowCount();
Remember that we setup the name of the DepartmentEmployees
accessor in step 6. Next, we checked for the number of employees in the associated department, and if there was only one employee—the one we are about to delete—we first deleted it by calling super.remove()
. Then we deleted the department itself by calling department.remove()
. If more than one employee was found for the specific department, we just delete the employee by calling super.remove()
. This was done in the else
part of the if
statement.
There's more...
Note the implications of using getRowCount()
versus getEstimatedRowCount() in your
code when dealing with large result sets: getRowCount()
will perform a database count query each time it is called to return the exact number of rows in the view object. On the other hand, getEstimatedRowCount()
executes a database count query only once to fetch the view object row count to the middle layer. Then, it fetches the row count from the middle layer. The row count in the middle layer is adjusted as view object rows are added or deleted. This may not produce an accurate row count when multiple user sessions are manipulating the same view object at the same time. For more information on this topic, consult the section How to Count the Number of Rows in a Row Set in the Fusion Developer's Guide for Oracle Application Development Framework.
See also
- Overriding remove() to delete associated children entities, in this chapter