Hello!
I have problem with mapping @Any properties.
(My entity works as expected if I leave out the @Any property.)
Code:
@Entity
public class MyEntity implements Serializable {
...
private Property mainProperty;
@Any(metaColumn = @Column(name = "property_type"), fetch = FetchType.EAGER)
@AnyMetaDef(idType = "integer", metaType = "string", metaValues = {
@MetaValue(value = "S", targetEntity = StringProperty.class),
@MetaValue(value = "I", targetEntity = IntegerProperty.class) }
)
@JoinColumn(name = "property_id")
public Property getMainProperty() {
return mainProperty;
}
public void setMainProperty(Property mainProperty) {
this.mainProperty = mainProperty;
}
It is the same mapping as in the Hibernate Annotations documentation at
http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#entity-hibspec-singleassoc-any.
I get the following exception during ddl generation:
Code:
Caused by: org.hibernate.AnnotationException: @Any requires an explicit @JoinColumn(s): com.example.MyEntity.mainProperty
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1231)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:425)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:131)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:224)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:291)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 78 more
I debugged EJB3OverridenAnnotationReader, and I found that at line 280 the annotations of the property are filtered by
Code:
for (Annotation annotation : annotations) {
if ( !annotationToXml.containsKey( annotation.annotationType() ) ) {
//unknown annotations are left over
annotationList.add( annotation );
}
This filter skips the @JoinColumn annotation that's why only the @Any and @AnyMetaDef will be added to annotationList. This results in @JoinColumn to be "lost" for further processing of @Any, and the application fails at AnnotationBinder@1231
Code:
else if ( joinColumns == null && property.isAnnotationPresent( org.hibernate.annotations.Any.class ) ) {
throw new AnnotationException( "@Any requires an explicit @JoinColumn(s): "
+ StringHelper.qualify( propertyHolder.getPath(), property.getName() ) );
}
because
property.isAnnotationPresent() is not aware of @JoinColumn at AnnotationBinder@1154.
Is this a bug, or should I configure something?
Thanks for your help in advance!
Regards:
Norbi
libs:
hibernate-core-3.3.1.GA
hibernate-annotations-3.4.0.GA
hibernate-entitymanager-3.4.0.GA