I'm trying to map Generic types in Hibernate. I've searched and couldn't find clear answers how to do it. Help will be much appreciated. Here are some example classes:
public class Person {
...
}
public class PersonA extends Person {
...
}
public class PersonB extends Person {
...
}
public class PersonHolder<P extends Person> {
private P person;
...
}
public class People {
private Map<String, PersonHolder<PersonA> > aPeople;
private Map<String, PersonHold开发者_StackOverflow中文版er<PersonB> > bPeople;
...
}
I'm using native Hibernate mapping (XML, no annotations). I already have Hibernate mapping for Person, PersonA and PersonB, but I would like to know if it's possible to map class PersonHolder's instantiation in a generic way without explicitly create generic-implementing sub-classes such as:
public class PersonAHolder extends PersonHolder<PersonA> {
...
}
(and mapping this class as an ordinary class).
Thanks!
AFAIK that is unfortunately not possible. In bytecode there's no difference between PersonHolder<PersonA>
and PersonHolder<PersonB>
due to type erasure and thus Hibernate would not be able to distinguish between them.
Edit: to clarify, I'm talking about direct instances of PersonHolder
. If you have subclasses that define a concrete generic type, you can get the generic type information. But that would require to have subclasses, which contradicts the question:
I would like to know if it's possible to map class PersonHolder's instantiation in a generic way without explicitly create generic-implementing sub-classes such as:
精彩评论