[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Entity inheritance and referencing other entities with casca

filip.hrbek do-not-reply at jboss.com
Wed Oct 31 12:19:34 EDT 2007


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


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4100746#4100746

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4100746



More information about the jboss-user mailing list