From c7d716f2ac44e695cf6c1d4b9039f262c57e28fd Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Sat, 22 Jun 2013 12:20:54 -0400 Subject: [PATCH] HHH-7815 Throw AnnotationException if annotated array attribute is not indexable --- .../IndexedPluralAttributeSourceImpl.java | 12 +++- .../test/annotations/array/ArrayTest.java | 56 ++++++++++++++++--- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/IndexedPluralAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/IndexedPluralAttributeSourceImpl.java index 4af8b1ed57..d94af124b9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/IndexedPluralAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/IndexedPluralAttributeSourceImpl.java @@ -27,11 +27,13 @@ import java.util.EnumSet; import org.jboss.jandex.AnnotationInstance; +import org.hibernate.AnnotationException; import org.hibernate.cfg.NotYetImplementedException; import org.hibernate.metamodel.internal.Binder; 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.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.JandexHelper; 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.MappingException; 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.SingularAttributeSource; @@ -66,7 +69,14 @@ public class IndexedPluralAttributeSourceImpl extends PluralAttributeSourceImpl 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() ) { final Binder.DefaultNamingStrategy defaultNamingStrategy = new Binder.DefaultNamingStrategy() { @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/array/ArrayTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/array/ArrayTest.java index 07c10861f4..511a13c3cf 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/array/ArrayTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/array/ArrayTest.java @@ -23,16 +23,27 @@ */ 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.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 @@ -67,9 +78,38 @@ public class ArrayTest extends BaseCoreFunctionalTestCase { tx.commit(); 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 protected Class[] getAnnotatedClasses() { return new Class[] { Competitor.class, Contest.class, Contest.Month.class }; } + + @Entity + public static class NoIndexArrayEntity { + @Id + @GeneratedValue + public long id; + + @ElementCollection + public NoIndexArrayEntity[] subElements; + } }