I have the same or similar question.
Say I have a generic class with property soldiers:
Code:
@Entity
public class Troop<E extends Soldier> {
List<E> soldiers = new ArrayList<E>();
@ManyToOne
public List<E> getSoldiers() ...
public E createAndAddSoldier(Class<E> clazz, String name) {...
And say I have 20 subtypes of soldier.
I want to constrain the type of soldiers in a troop to one subtype (or descendant of subtype ) E .
For a findall to return correctly parameterized troop instances I guess hibernate would need a discriminator column to identify which type to substitute for E.
I haven't come across anything in JPA or Hibernate mapping config that can allow me to do this. I am looking at a solution for the one-end of the one-to-many relationship. On the many end I would use inheritance mapping (one-table) with discriminator column
Code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "classtype", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("SLD")
public class Soldier {
Troop<? extends Soldier> troop;
@ManyToOne
public Troop<? extends Soldier> getTroop() {
return troop;
}
Is there a way to make this work, have hibernate return a correctly instantiated graph for this situation? I would have no problem duplicating the classtype parameter from soldier into troop table. BUT i'd rather not create 20 subtypes of troop.