After a rather tedious debugging session, I've isolated the problem to org.hibernate.reflection.java.JavaXClass.getDeclaredFieldProperties(...) method.
Code:
private List<XProperty> getDeclaredFieldProperties(Filter filter) {
List<XProperty> result = new LinkedList<XProperty>();
for ( Field f : toClass().getDeclaredFields() ) {
if ( JavaXProperty.isProperty( f, getTypeEnvironment().bind( f.getGenericType() ), filter ) ) {
result.add( getFactory().getXProperty( f, this ) );
}
}
return result;
}
Obviously toClass().getDeclaredFields() called on class B do not return fields declared in class A, so respective field mappings in orm.xml are ignored.
So, I've changed the code (same with getDeclaredMethodProperties) to lookup superclass defined fields too:
Code:
private List<XProperty> getDeclaredFieldProperties(Filter filter) {
List<XProperty> result = new LinkedList<XProperty>();
Class clazz = toClass ();
while (null != clazz) {
for ( Field f : clazz.getDeclaredFields() ) {
if ( JavaXProperty.isProperty( f, getTypeEnvironment().bind( f.getGenericType() ), filter ) ) {
result.add( getFactory().getXProperty( f, this ) );
}
}
clazz = clazz.getSuperclass ();
}
return result;
}
That fixed the immediate problem, but another one cropped up:
In org.hibernate.reflection.java.EJB3OverridenAnnotationReader constructor, classname is set with the following:
Code:
...
else if ( el instanceof Field ) {
Field field = (Field) el;
className = field.getDeclaringClass().getName();
...
Consequently initAnnotations() fail to locate the correct mapping since it's looking for
entity A.id in orm.xml, whereas it's defined as entity B.id.
I see no easy way to fix this. It's possible to propagate the correct entity class in context to AnnotationReader, but it's rather convoluted and possibly break other things if (I) attempted.
This mapping worked fine under Toplink Essentials, and as far as I've seen the jpa specification doesn't prohibit doing so (then again the spec is rather silent on many things).
So, what would be the best way to proceed? If there's a workaround, I'd be happy to hear it, though I'm rather reluctant to change the mappings since this pattern is repeated on a huge number of entities. I can submit a bug report and/or try to patch it myself and submit for your review, if you think that's a bug.
Thanks.