diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index bdd7c68499..5ff2bd768f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -2495,7 +2495,7 @@ public final class AnnotationBinder { value.setColumns( columns ); value.setPersistentClassName( persistentClassName ); value.setMappings( mappings ); - value.setType( inferredData.getProperty(), inferredData.getClassOrElement() ); + value.setType( inferredData.getProperty(), inferredData.getClassOrElement(), persistentClassName ); value.setAccessType( propertyAccessor ); id = value.make(); } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index 1e663cb879..560d3ec935 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -1288,7 +1288,7 @@ public abstract class CollectionBinder { column.setTable( collValue.getCollectionTable() ); } elementBinder.setColumns( elementColumns ); - elementBinder.setType( property, elementClass ); + elementBinder.setType( property, elementClass, collValue.getOwnerEntityName() ); elementBinder.setPersistentClassName( propertyHolder.getEntityName() ); elementBinder.setAccessType( accessType ); collValue.setElement( elementBinder.make() ); @@ -1441,4 +1441,4 @@ public abstract class CollectionBinder { public void setLocalGenerators(HashMap localGenerators) { this.localGenerators = localGenerators; } -} +} \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java index d62ddc3fb0..4883499bf5 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java @@ -282,7 +282,7 @@ public class MapBinder extends CollectionBinder { elementBinder.setExplicitType( mapKeyTypeAnnotation.value() ); } else { - elementBinder.setType( property, elementClass ); + elementBinder.setType( property, elementClass, this.collection.getOwnerEntityName() ); } elementBinder.setPersistentClassName( propertyHolder.getEntityName() ); elementBinder.setAccessType( accessType ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java index b4251816b7..27e9235a89 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java @@ -185,7 +185,7 @@ public class PropertyBinder { simpleValueBinder.setReturnedClassName( returnedClassName ); simpleValueBinder.setColumns( columns ); simpleValueBinder.setPersistentClassName( containerClassName ); - simpleValueBinder.setType( property, returnedClass ); + simpleValueBinder.setType( property, returnedClass, containerClassName ); simpleValueBinder.setMappings( mappings ); simpleValueBinder.setReferencedEntityName( referencedEntityName ); simpleValueBinder.setAccessType( accessType ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java index 1a5c7b90c8..1fede49a0f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java @@ -140,10 +140,11 @@ public class SimpleValueBinder { //TODO execute it lazily to be order safe - public void setType(XProperty property, XClass returnedClass) { + public void setType(XProperty property, XClass returnedClass, String declaringClassName) { if ( returnedClass == null ) { + // we cannot guess anything return; - } //we cannot guess anything + } XClass returnedClassOrElement = returnedClass; boolean isArray = false; if ( property.isArray() ) { @@ -237,6 +238,17 @@ public class SimpleValueBinder { } else if ( ( !key && property.isAnnotationPresent( Enumerated.class ) ) || ( key && property.isAnnotationPresent( MapKeyEnumerated.class ) ) ) { + final Class attributeJavaType = mappings.getReflectionManager().toClass( returnedClassOrElement ); + if ( !Enum.class.isAssignableFrom( attributeJavaType ) ) { + throw new AnnotationException( + String.format( + "Attribute [%s.%s] was annotated as enumerated, but its java type is not an enum [%s]", + declaringClassName, + xproperty.getName(), + attributeJavaType.getName() + ) + ); + } type = EnumType.class.getName(); explicitType = type; } @@ -625,4 +637,4 @@ public class SimpleValueBinder { public void setAccessType(AccessType accessType) { this.accessType = accessType; } -} \ No newline at end of file +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/enums/InvalidEnumeratedJavaTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/enums/InvalidEnumeratedJavaTypeTest.java new file mode 100644 index 0000000000..a26dd02205 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/enums/InvalidEnumeratedJavaTypeTest.java @@ -0,0 +1,61 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.enums; + +import javax.persistence.Entity; +import javax.persistence.Enumerated; +import javax.persistence.Id; + +import org.hibernate.AnnotationException; +import org.hibernate.cfg.Configuration; + +import org.junit.Test; + +import org.hibernate.testing.junit4.BaseUnitTestCase; + +import static org.junit.Assert.fail; + +/** + * @author Steve Ebersole + */ +public class InvalidEnumeratedJavaTypeTest extends BaseUnitTestCase { + @Test + public void testInvalidMapping() { + final Configuration cfg = new Configuration(); + try { + cfg.addAnnotatedClass( TheEntity.class ); + cfg.buildMappings(); + fail( "Was expecting failure" ); + } + catch (AnnotationException expected) { + System.out.println( expected ); + } + } + + @Entity + public static class TheEntity { + @Id private Long id; + @Enumerated private Boolean yesNo; + } +}