HHH-7815 Throw AnnotationException if annotated array attribute is not

indexable
This commit is contained in:
Brett Meyer 2013-06-22 12:20:54 -04:00
parent 8e9ea2aaec
commit c7d716f2ac
2 changed files with 59 additions and 9 deletions

View File

@ -27,11 +27,13 @@ import java.util.EnumSet;
import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationInstance;
import org.hibernate.AnnotationException;
import org.hibernate.cfg.NotYetImplementedException; import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.metamodel.internal.Binder; import org.hibernate.metamodel.internal.Binder;
import org.hibernate.metamodel.internal.source.annotations.attribute.MappedAttribute; import org.hibernate.metamodel.internal.source.annotations.attribute.MappedAttribute;
import org.hibernate.metamodel.internal.source.annotations.attribute.PluralAssociationAttribute; import org.hibernate.metamodel.internal.source.annotations.attribute.PluralAssociationAttribute;
import org.hibernate.metamodel.internal.source.annotations.entity.ConfiguredClass; import org.hibernate.metamodel.internal.source.annotations.entity.ConfiguredClass;
import org.hibernate.metamodel.internal.source.annotations.util.HibernateDotNames;
import org.hibernate.metamodel.internal.source.annotations.util.JPADotNames; import org.hibernate.metamodel.internal.source.annotations.util.JPADotNames;
import org.hibernate.metamodel.internal.source.annotations.util.JandexHelper; import org.hibernate.metamodel.internal.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.spi.source.AttributeSource; import org.hibernate.metamodel.spi.source.AttributeSource;
@ -41,6 +43,7 @@ import org.hibernate.metamodel.spi.source.IdentifierSource;
import org.hibernate.metamodel.spi.source.IndexedPluralAttributeSource; import org.hibernate.metamodel.spi.source.IndexedPluralAttributeSource;
import org.hibernate.metamodel.spi.source.MappingException; import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.PluralAttributeIndexSource; import org.hibernate.metamodel.spi.source.PluralAttributeIndexSource;
import org.hibernate.metamodel.spi.source.PluralAttributeSource;
import org.hibernate.metamodel.spi.source.SimpleIdentifierSource; import org.hibernate.metamodel.spi.source.SimpleIdentifierSource;
import org.hibernate.metamodel.spi.source.SingularAttributeSource; import org.hibernate.metamodel.spi.source.SingularAttributeSource;
@ -66,7 +69,14 @@ public class IndexedPluralAttributeSourceImpl extends PluralAttributeSourceImpl
attribute.getContext().getOrigin() attribute.getContext().getOrigin()
); );
} }
// TODO: add checks for inconsistent annotations
if ( attribute.getPluralAttributeNature().equals(PluralAttributeSource.Nature.ARRAY )
&& !attribute.annotations().containsKey( JPADotNames.ORDER_COLUMN )
&& !attribute.annotations().containsKey( HibernateDotNames.INDEX_COLUMN ) ) {
throw new AnnotationException( "The array attribute '" + attribute.getName()
+ "' must be annotated with @OrderColumn or @IndexColumn!" );
}
if ( attribute.isSequentiallyIndexed() ) { if ( attribute.isSequentiallyIndexed() ) {
final Binder.DefaultNamingStrategy defaultNamingStrategy = new Binder.DefaultNamingStrategy() { final Binder.DefaultNamingStrategy defaultNamingStrategy = new Binder.DefaultNamingStrategy() {
@Override @Override

View File

@ -23,16 +23,27 @@
*/ */
package org.hibernate.test.annotations.array; package org.hibernate.test.annotations.array;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.test.annotations.array.Contest.Month;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Properties;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.AnnotationException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.metamodel.MetadataBuilder;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.test.annotations.array.Contest.Month;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/** /**
* @author Emmanuel Bernard * @author Emmanuel Bernard
@ -68,8 +79,37 @@ public class ArrayTest extends BaseCoreFunctionalTestCase {
s.close(); s.close();
} }
@Test
public void testNoIndexAnnotationFailure() {
Properties properties = constructProperties();
BootstrapServiceRegistry bootRegistry = buildBootstrapServiceRegistry();
StandardServiceRegistry serviceRegistry = buildServiceRegistry( bootRegistry, properties );
MetadataSources sources = new MetadataSources( bootRegistry );
sources.addAnnotatedClass( NoIndexArrayEntity.class );
MetadataBuilder metadataBuilder = sources.getMetadataBuilder(serviceRegistry);
boolean caught = false;
try {
metadataBuilder.build();
}
catch ( AnnotationException e ) {
caught = true;
assertTrue( e.getMessage().contains( "must be annotated with @OrderColumn or @IndexColumn" ) );
}
assertTrue( caught );
}
@Override @Override
protected Class[] getAnnotatedClasses() { protected Class[] getAnnotatedClasses() {
return new Class[] { Competitor.class, Contest.class, Contest.Month.class }; return new Class[] { Competitor.class, Contest.class, Contest.Month.class };
} }
@Entity
public static class NoIndexArrayEntity {
@Id
@GeneratedValue
public long id;
@ElementCollection
public NoIndexArrayEntity[] subElements;
}
} }