I am developing an application using Hibernate and trying to simulate the following scenario:
I have an Activity Activity class that is defined as follows:
@Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="activityType") @Table(name = "BF_ACTIVITY") public abstract class Activity extends PersistableObject { @ManyToOne @JoinColumn(name = "ASSIGNED_TO") protected Contactable assignedTo; @ManyToOne @JoinColumn(name = "RAISED_BY") protected Contactable raisedBy;
Note. I use unidirectional inheritance (so that all implementation objects will use the same table) and set the delimiter.
Now I have two objects that extend the Activity: ToDo and Status:
@Entity @Table(name = "BF_TODO") @DiscriminatorValue("Todo") public class ToDo extends Activity { ... @Entity @Table(name = "BF_STATUS") @DiscriminatorValue("Status") public class Status extends Activity { ...
Note again that for both implementations, I installed DiscriminatorValue.
Finally, I want to have a Person object, and a person can have a Status list and a ToDo list - I also want to capture a bidirectional relationship, so I model it using the mappedBy configuration, but in both cases use the "raiseBy" field that exists in the superclass " Activity":
public abstract class Person { @ManyToMany(mappedBy="raisedBy", targetEntity=Activity.class) private List<ToDo> todoItems = new ArrayList<ToDo>();
Since I use mappedBy with the member variable "raiseBy" from the superclass, I also specified targetEntity (otherwise it will not be able to find the field in the ToDo object).
The problem is that I'm trying to call getTodoItems () - it actually just returns all the Activity objects associated with raiseBy with the current person. for example, it throws a casting exception because it expects a ToDos list, but Hibernate also returns state objects in the list.
I was hoping that the mappedBy configuration along with the DiscriminatorValue would be sufficient to do the job: did anyone come across this or allow it?
thanks
EDIT
I just found this post:
Can someone point me towards a good overview and @Where example? can I just update my person as follows to use @Where with the discriminator column?
public abstract class Person { @ManyToMany(mappedBy="raisedBy", targetEntity=Activity.class) @Where(clause="activityType=Todo") private List<ToDo> todoItems = new ArrayList<ToDo>();