HHH-7567 - Better checking when @Enumerated involved

(cherry picked from commit 4450b127b2)
This commit is contained in:
Steve Ebersole 2012-09-04 12:17:34 -05:00
parent ca3cc50a2f
commit a1c219ee96
6 changed files with 79 additions and 6 deletions

View File

@ -2494,7 +2494,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();
}

View File

@ -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() );

View File

@ -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 );

View File

@ -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 );

View File

@ -131,10 +131,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() ) {
@ -229,6 +230,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;
}

View File

@ -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;
}
}