2

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, PersonHolder<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!

Nimrod
  • 153
  • 3
  • 8
  • I recently answered this same (or a very similar) question. [Take a look at it][1]. [1]: http://stackoverflow.com/questions/7000428/using-hibernate-with-generics/7001162#7001162 – Ryan Stewart Aug 16 '11 at 13:33
  • Annotations makes it extremely less difficult to debug. What you are trying to achieve is known as polymophic mapping, see http://stackoverflow.com/questions/979200/hibernate-polymorphism for more insight. You can use interfaces to handle your People class. – Bitmap Aug 16 '11 at 13:35
  • I haven't tried to debug annotations but XML debugging is fairly easy once you've got your XML straight. The downside in XML is that the parser error messages don't tell you in which file the mistake is. – Nimrod Aug 16 '11 at 13:57

1 Answers1

1

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:

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • that's actually not true. fields, method parameters, method return types and classes which extend other classes with specific types _do_ maintain that information after compilation. – jtahlborn Aug 16 '11 at 14:09
  • @jtahlborn For `PersonAHolder` that is true, you can get the information that it extended `PersonHolder` - but the OP wanted to do _WITHOUT_. Thus you can't distinguish between instances of `PersonHolder` which don't keep the reference. I did not talk about subclasses. – Thomas Aug 16 '11 at 14:13
  • @jtahlborn reification of generics in Java is not supported; some of the type info is indeed captured, but this is not a proper reification support; Thomas's answers is valid. – 01es Aug 16 '11 at 14:16
  • 1
    It *is* possible, as I pointed out in an answer, but my answer seems to have been moved to a comment. I just answered a question like this here: http://stackoverflow.com/questions/7000428/using-hibernate-with-generics/7001162#7001162 – Ryan Stewart Aug 16 '11 at 14:28
  • @Thomas - if you read my comment, i referred to situations _other_ than subclassing. in the original example, the specific generics information _will be_ available for the fields `aPeople` and `bPeople`. @01res - yes, i understand well about reification, i suggest you read up on what java actually does with generics. – jtahlborn Aug 16 '11 at 16:32
  • @jtahlborn If it is possible to get the information for `aPeople` or `bPeople` I'd love to see a solution. AFAIK generic type information is only available on a per class basis and not per instance. Please share some code/links. – Thomas Aug 17 '11 at 07:53
  • @Thomas - is this sufficient http://stackoverflow.com/questions/1868333/how-can-i-determine-the-type-of-a-generic-field-in-java ? – jtahlborn Aug 17 '11 at 12:36
  • @jtahlborn - ok, that seems valid. – Thomas Aug 17 '11 at 13:05