Using Foreign Constraints and Cascade Delete for Entity Beans

Discussions

EJB design: Using Foreign Constraints and Cascade Delete for Entity Beans

  1. Would you use Foreign Keys with Entity Beans? This means that the database would manage cascading deletions automatically. Or would you not use Foreign Keys, which means that the data access objects need to handle cascading deletions?

    I see a problem when not using Foreign Keys. You have a one to one relationship with key in parent and full cascade. When you delete the child, you must load the parent and set the foreign key to null programatically. This costs performance and requires a finder method which otherwise (using database cascade deletions) would not be necessary.

    What do you think about it? Both questions refer to entity beans:

    1. Using Foreign Key constraints for the tables?
    2. Using Cascade Delete in SQL DDL scripts?
  2. 1. Using Foreign Key constraints for the tables?
    I always use Foreign Key constraints, where appropriate. They are too important to data integrity. After all, your database might someday be accessed by a non-EJB application.
    2. Using Cascade Delete in SQL DDL scripts?
    Here I am not so sure. I have mixed feelings about delete cascades, because they behave differently in different databases. If they application did not need to be portable between databases, I would might use them if there was a compelling reason to do so.

    One warning, though. Delete cascades in the database may interfere with the data caching strategy of your EJB server.