HHH-7567 - Better checking when @Enumerated involved

This commit is contained in:
Steve Ebersole 2012-09-04 12:17:34 -05:00
parent 076eccb7a2
commit 4450b127b2
6 changed files with 81 additions and 8 deletions

View File

@ -2495,7 +2495,7 @@ public final class AnnotationBinder {
value.setColumns( columns ); value.setColumns( columns );
value.setPersistentClassName( persistentClassName ); value.setPersistentClassName( persistentClassName );
value.setMappings( mappings ); value.setMappings( mappings );
value.setType( inferredData.getProperty(), inferredData.getClassOrElement() ); value.setType( inferredData.getProperty(), inferredData.getClassOrElement(), persistentClassName );
value.setAccessType( propertyAccessor ); value.setAccessType( propertyAccessor );
id = value.make(); id = value.make();
} }

View File

@ -1288,7 +1288,7 @@ public abstract class CollectionBinder {
column.setTable( collValue.getCollectionTable() ); column.setTable( collValue.getCollectionTable() );
} }
elementBinder.setColumns( elementColumns ); elementBinder.setColumns( elementColumns );
elementBinder.setType( property, elementClass ); elementBinder.setType( property, elementClass, collValue.getOwnerEntityName() );
elementBinder.setPersistentClassName( propertyHolder.getEntityName() ); elementBinder.setPersistentClassName( propertyHolder.getEntityName() );
elementBinder.setAccessType( accessType ); elementBinder.setAccessType( accessType );
collValue.setElement( elementBinder.make() ); collValue.setElement( elementBinder.make() );

View File

@ -282,7 +282,7 @@ public class MapBinder extends CollectionBinder {
elementBinder.setExplicitType( mapKeyTypeAnnotation.value() ); elementBinder.setExplicitType( mapKeyTypeAnnotation.value() );
} }
else { else {
elementBinder.setType( property, elementClass ); elementBinder.setType( property, elementClass, this.collection.getOwnerEntityName() );
} }
elementBinder.setPersistentClassName( propertyHolder.getEntityName() ); elementBinder.setPersistentClassName( propertyHolder.getEntityName() );
elementBinder.setAccessType( accessType ); elementBinder.setAccessType( accessType );

View File

@ -185,7 +185,7 @@ public class PropertyBinder {
simpleValueBinder.setReturnedClassName( returnedClassName ); simpleValueBinder.setReturnedClassName( returnedClassName );
simpleValueBinder.setColumns( columns ); simpleValueBinder.setColumns( columns );
simpleValueBinder.setPersistentClassName( containerClassName ); simpleValueBinder.setPersistentClassName( containerClassName );
simpleValueBinder.setType( property, returnedClass ); simpleValueBinder.setType( property, returnedClass, containerClassName );
simpleValueBinder.setMappings( mappings ); simpleValueBinder.setMappings( mappings );
simpleValueBinder.setReferencedEntityName( referencedEntityName ); simpleValueBinder.setReferencedEntityName( referencedEntityName );
simpleValueBinder.setAccessType( accessType ); simpleValueBinder.setAccessType( accessType );

View File

@ -140,10 +140,11 @@ public class SimpleValueBinder {
//TODO execute it lazily to be order safe //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 ) { if ( returnedClass == null ) {
// we cannot guess anything
return; return;
} //we cannot guess anything }
XClass returnedClassOrElement = returnedClass; XClass returnedClassOrElement = returnedClass;
boolean isArray = false; boolean isArray = false;
if ( property.isArray() ) { if ( property.isArray() ) {
@ -237,6 +238,17 @@ public class SimpleValueBinder {
} }
else if ( ( !key && property.isAnnotationPresent( Enumerated.class ) ) else if ( ( !key && property.isAnnotationPresent( Enumerated.class ) )
|| ( key && property.isAnnotationPresent( MapKeyEnumerated.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(); type = EnumType.class.getName();
explicitType = type; 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;
}
}