From c1a5622a7e1896baddca6e3c1b8db43d811350e2 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Tue, 25 Mar 2014 14:33:04 -0400 Subject: [PATCH] HHH-9083 throw AnnotationException for out of place @Immutable --- .../attribute/AbstractSingularAttribute.java | 8 ++++- .../annotations/immutable/ImmutableTest.java | 30 ++++++++----------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/annotations/attribute/AbstractSingularAttribute.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/annotations/attribute/AbstractSingularAttribute.java index 402c2d4f5e..6d5a37acf1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/annotations/attribute/AbstractSingularAttribute.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/annotations/attribute/AbstractSingularAttribute.java @@ -25,15 +25,16 @@ package org.hibernate.metamodel.source.internal.annotations.attribute; import javax.persistence.AccessType; +import org.hibernate.AnnotationException; import org.hibernate.metamodel.reflite.spi.MemberDescriptor; import org.hibernate.metamodel.source.internal.AttributeConversionInfo; import org.hibernate.metamodel.source.internal.annotations.entity.ManagedTypeMetadata; import org.hibernate.metamodel.source.internal.annotations.util.ConverterAndOverridesHelper; +import org.hibernate.metamodel.source.internal.annotations.util.HibernateDotNames; import org.hibernate.metamodel.source.internal.annotations.util.JPADotNames; import org.hibernate.metamodel.source.spi.AttributePath; import org.hibernate.metamodel.source.spi.AttributeRole; import org.hibernate.metamodel.spi.binding.SingularAttributeBinding; - import org.jboss.logging.Logger; /** @@ -55,6 +56,11 @@ public abstract class AbstractSingularAttribute String accessorStrategy) { super( container, attributeName, attributePath, attributeRole, backingMember, nature, accessType, accessorStrategy ); + if ( backingMember.getAnnotations().containsKey( HibernateDotNames.IMMUTABLE ) ) { + throw new AnnotationException( "@Immutable can be used on entities or collections, not " + + attributeRole.getFullPath() ); + } + ConverterAndOverridesHelper.INSTANCE.processConverters( getPath(), getNature(), diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/immutable/ImmutableTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/immutable/ImmutableTest.java index aaa12e01c7..1ddf2f6b48 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/immutable/ImmutableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/immutable/ImmutableTest.java @@ -23,26 +23,22 @@ */ package org.hibernate.test.annotations.immutable; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.util.ArrayList; import java.util.List; -import org.jboss.logging.Logger; -import org.junit.Test; - import org.hibernate.AnnotationException; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.cfg.Configuration; - -import org.hibernate.testing.FailureExpectedWithNewMetamodel; -import org.hibernate.testing.ServiceRegistryBuilder; +import org.hibernate.metamodel.MetadataSources; 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 static org.junit.Assert.fail; +import org.jboss.logging.Logger; +import org.junit.Test; /** * Tests for Immutable annotation. @@ -154,13 +150,11 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { } @Test - @FailureExpectedWithNewMetamodel public void testMisplacedImmutableAnnotation() { - // todo : metamodel is not applying many "out of place" validations try { - Configuration config = new Configuration(); - config.addAnnotatedClass(Foobar.class); - config.buildMappings( ); + MetadataSources sources = new MetadataSources( serviceRegistry().getParentServiceRegistry() ); + sources.addAnnotatedClass(Foobar.class); + sources.buildMetadata(); fail(); } catch (AnnotationException ae) { @@ -170,6 +164,6 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase { @Override protected Class[] getAnnotatedClasses() { - return new Class[] { Country.class, State.class}; + return new Class[] { Country.class, State.class }; } }