Hello,
I've noticed a minor issue with inheritance - if ID is declared both in the superclass and the subclass the ClassCastException is thrown. I'm not really sure if it's a bug, all i could find about IDs in the spec was:
"When an entity is defined as a subclass of another entity, the primary keys of the entities must be of the same type."
but does that mean the ID in the subclass has to be inherited or may it be declared? Would it even make sense to declare it twice?
Anyway, i've experimented a bit and same thing happens with all three types of inheritance. Here is the stack trace:
javax.persistence.PersistenceException: java.lang.ClassCastException: org.hibernate.mapping.JoinedSubclass
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:100)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:42)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:29)
<snip>
Caused by: java.lang.ClassCastException: org.hibernate.mapping.JoinedSubclass
at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1274)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:843)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:562)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:174)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:825)
at org.hibernate.ejb.HibernatePersistence.handleCallbacks(HibernatePersistence.java:189)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:180)
at org.hibernate.ejb.HibernatePersistence.createFactory(HibernatePersistence.java:77)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:93)
... 19 more
and here is the code line that causes the problem (org.hibernate.cfg.AnnotationBinder)
1274: RootClass rootClass = (RootClass) propertyHolder.getPersistentClass();
the propertryHolder holds instance of org.hibernate.cfg.ClassPropertyHolder.
I'm not much into the guts of Hibernate, but i tryed to get deeper. I noticed the usage of EntityBinder class and the fact that if i set it's ignoreIdAnnotations property to true, more or less before this call takes place (AnnotationBinder):
562: processElementAnnotations(propertyHolder,
563: subclassAndSingleTableStrategy ? Nullability.FORCED_NULL : Nullability.NO_CONSTRAINT,
564: propertyAnnotatedElement.element,
565: propertyAnnotatedElement.inferredData, classGenerators, classGeneratorTables, entityBinder, false, mappings);
(of course when the id is being processed), Hibernate will treat it as an ordinary property and create a nullable column in the database.
As i've said i'm not really sure what should happen in such situation, but even if it's not allowed to declare the id in subclasses, the ClassCastException is rather ugly way of saying this :)
Hope this helps.
|