views:

153

answers:

1

Given a simple entity relationship:

@Entity
public class Single {

  @OneToMany
  public Set<Multiple> multiples;
}

How does Hibernate find out that the generic type of multiples is Multiple? This information is impossible to find with the standard Reflection API.

I'm looking through the source code, but don't really know where to start.

+2  A: 

But it is possible to find out using reflection API. Take a look at Field.getGenericType():

Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
  Type[] genericArguments = ((ParameterizedType) type).getActualTypeArguments();
}
ChssPly76
You're right, but that's weird: I thought that info was lost at runtime. Is this article on reflection generics out of date? http://www.artima.com/weblogs/viewpost.jsp?thread=208860
Mwanji Ezana
That article has some examples similar to the code I've posted above. Certain generics information is lost at runtime; statically declared field / method declarations are not. Take a look at Java Generics FAQ for mode details: http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
ChssPly76
Thanks! The obvious difference is looking directly at the field vs. looking at the generic interfaces or superclasses. For some reason, I never thought to simply look at the field.
Mwanji Ezana