Probably a common problem, but anyway:
Let’s say I have a simple entity Task with a list of Activity entities:
@Entity
@Table(name = "task")
public class Task {
@Id
@Column(name = "id")
private Integer id;
@OneToMany
@JoinColumn(name = "task_id")
private List<Activity> activities;
}
And Activity entity is pretty simple:
@Entity
@Table(name = "activity")
public class Activity {
@Id
@Column(name = "id")
private Integer id;
@Column(name = "activity_type")
private int type;
}
And here I want to do some magic — based on Activity type I want hibernate to stuff Task.activities with different Activity implementation, for example if Activity.type == 1, I want to have ActivityAImpl there, if Activity.type == 2 – it should be ActivityBImpl and so on.. Basically all my activities would implement a single interface with a some method “execute()”.
Hopefully there is a simple solution for that.
Thanks for your help!
Take a look at @MappedSuperClass and @Discriminator annotions. I guess that is what you are looking for. Here is an example.