14 Replies Latest reply on May 22, 2008 12:03 PM by Alex Grönholm

    Entity bean cascade design issue

    Alex Grönholm Novice

      I'm sure that a lot of others have run into the same problem as I have.
      Suppose you have three entity bean classes: Employee, Customer, Order.
      an Order is associated to one Employee and one Customer.
      What if an employee who has associated orders gets fired? You can't simply delete his Employee record, because the association from Order prevents it. Orders may not be deleted because you have to keep them all for statistics.
      I can think of three ways to handle this:
      1. Have the code that removes the Employee nullify the connection to the Employee
      2. Use weak references instead (store the ID of the Employee without a foreign key constraint)
      3. Use a unidirectional one-to-many association

      1. Not very scalable, since you have to remember to only remove the entities in question through one specific method, and the method has to know of every single association
      2. Can leave dangling IDs behind when the target is removed, and references require explicit queries
      3. Ugly, not spec compliant and requires an additional table per association, and association is not visible from the Order object

      Is there nothing like "ON DELETE SET NULL" for EJB 3.0 persistence, or Hibernate?
      Do I have to resort to native SQL for this?