What is the best way to restrict deletes in entity beans? Say, we have two tables TableA and TableB such that TableB has child records related to TableA. Furthermore, a Restrict Delete constraint has been defined in the database so that a row from TableA cannot be deleted if it has related row(s) in TableB. Now, lets say that EntityBeanA maps to TableA and EntityBeanB maps to TableB. Given the situation, I have two alternatives to handle this in my EntityBeans:
1) Let the ejbRemove method of EntityBeanA proceed as usual: In this case a database will throw an error which, in turn, will throw a RemoveException. The problem with this approach is that we have to present the RemoveException to the user in a more meaningful way, thus, resulting in more code.
2) In the ejbRemove() method of EntityBeanA, first call a finder method for EntityBeanB passing it the primarykey for EntityBeanA. If the resulting enumeration has elements that means we have child rows and we must throw a RemoveException (or some subclass of it). However, this approach leads to potentially two database calls - one to check for the existence of any child records, and the second (if the first check passes) to actually delete the record.
Which is the better alternative here? Or, is there another alternative which takes a different approach?