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
Download the sample code.
feel free to add more suggestions.
Have a nice day,
Zeeshan Baig
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
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.
ReplyDeleteIs there a way to do this in Groovy? Like in Entity Object business rules?
I don't see where you do it declaratively. I've looked at the sample code. Please point it out.
ReplyDeleteHi,
DeleteLooks like the word "declarative" is a typing mistake here. I have removed it :)
Zeeshan
Thanks for pointing out though.
DeleteThanks Zee for your Help
ReplyDeleteIS there any way to set any constraint in Oracle to restrict delete operation on parent record if dependent child exists
ReplyDeleteThat is the default behavior of Primary Key and Foreign Key Constraint no need to do anything.
DeleteIf you want to delete all child records automatically when you delete parent record then add CASCADE clause in the constraint.
Zeeshan