I am developing an application using Hibernate and am trying to model the following scenario:
I have an Activity Abstract class, 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 am using Single Table inheritance (so all implementing objects will use the same table) and have set a DiscriminatorColumn.
Now I have two objects that extend 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 {
...
Again note that for both implementations i have set a DiscriminatorValue.
Finally, I want to have a Person object, and the person can have a list of Status and a list of ToDo – I also want to capture the bi-directional relationship, so am modelling it using the mappedBy configuration, but in both cases using the “raisedBy” field that exists in the super class “Activity”:
public abstract class Person {
@ManyToMany(mappedBy="raisedBy", targetEntity=Activity.class)
private List<ToDo> todoItems = new ArrayList<ToDo>();
As i am using mappedBy with the member variable “raisedBy” from the super class I have also specified the targetEntity (otherwise it is not able to find the field in the ToDo object).
The problem is when I try to call getTodoItems() – it is actually just returning all “Activity” objects linked by “raisedBy” to the current person. e.g. it throws a cast exception because it is expecting the list of ToDos but Hibernate is returning Status objects in the list as well.
I was hoping the mappedBy config along with DiscriminatorValue would be enough to make this work – has anyone come across this or resolved it?
Thanks
EDIT
I have just found this post:
Can someone point me in the direction of a good @Where overview and example? could 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>();
Add the annotation :
@Where(clause = “activityType= ‘Todo'”)
But it’s a hibernate annotation, not JPA