Monday, January 10, 2011

How to prevent delete on master if child record exists in Oracle ADF

Hi,

In this post we will see how to stop deletion of Master record if child record exists in the database without writing much code.

In this example we have only Departments entity and Employee view object i created a ViewLink between DepartmentsView and EmployeesView and accessing the ViewAccessor in Departments entity.

Later i override the remove() method as follows


  public void remove() {
    RowIterator ri = getEmployeesView();
    int count = ri.getRowCount();
    if (count > 0) {
      throw new JboException("You cannot delete this record, Child record exists!!!");
    } else {
      super.remove();
    }

  }

Download the sample code.

feel free to add more suggestions.

Have a nice day,
Zeeshan Baig

7 comments:

  1. You the man! This helped out a lot. The only difference in my code is I didnt have a view link setup, i just had an accessor provided from the assocation.

    Is there a way to do this in Groovy? Like in Entity Object business rules?

    ReplyDelete
  2. I don't see where you do it declaratively. I've looked at the sample code. Please point it out.

    ReplyDelete
    Replies
    1. Hi,

      Looks like the word "declarative" is a typing mistake here. I have removed it :)

      Zeeshan

      Delete
    2. Thanks for pointing out though.

      Delete
  3. IS there any way to set any constraint in Oracle to restrict delete operation on parent record if dependent child exists

    ReplyDelete
    Replies
    1. That is the default behavior of Primary Key and Foreign Key Constraint no need to do anything.

      If you want to delete all child records automatically when you delete parent record then add CASCADE clause in the constraint.

      Zeeshan

      Delete