0 Replies Latest reply on Oct 31, 2007 12:19 PM by filip.hrbek

    Entity inheritance and referencing other entities with casca

    filip.hrbek

      Hi,

      I cannot figure out how to handle references to other entities in subclasses in the inheritance hierarchy.

      Let's consider following example (a bit incomplete to make it easily readable):

      @Entity
      @Inheritance(strategy = InheritanceType.JOINED)
      public abstract class Man {
       ...
       public Long getId();
      
       public String getName();
       ...
      }
      
      @Entity
      @OnDelete(action=OnDeleteAction.CASCADE)
      public class Employee extends Man {
       ...
       @ManyToOne
       @OnDelete(action=OnDeleteAction.CASCADE)
       public Department getDepartment();
       ...
      }
      
      @Entity
      public class Department {
       ...
       public Long getId();
       public String getDepartmentName();
       ...
      }
      

      Data:

      Man(id=1, name="John Black")
      Employee(id=1, department_id=0)
      Department(id=0, "Production")

      Notice the cascading deletes. As soon as the department is deleted, also the employee is deleted - but as the InheritanceType.JOINED is used, only a part of the employee is actually deleted. The abstract part of the employee (in the "Man" table) remains in the database (which is bad!). Any attempt to instantiate the man named "John Black" fails on the "Cannot instantiate abstract class or interface" exception" (no surprise, is it?).

      I wonder if there is any way how to delete also the abstract part of the record automatically if the extending part of the record is deleted.

      Thanks for hints.

      Filip