Skip to content

Commit c1a5622

Browse files
committed
HHH-9083 throw AnnotationException for out of place @immutable
1 parent 519acb2 commit c1a5622

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/annotations/attribute/AbstractSingularAttribute.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525

2626
import javax.persistence.AccessType;
2727

28+
import org.hibernate.AnnotationException;
2829
import org.hibernate.metamodel.reflite.spi.MemberDescriptor;
2930
import org.hibernate.metamodel.source.internal.AttributeConversionInfo;
3031
import org.hibernate.metamodel.source.internal.annotations.entity.ManagedTypeMetadata;
3132
import org.hibernate.metamodel.source.internal.annotations.util.ConverterAndOverridesHelper;
33+
import org.hibernate.metamodel.source.internal.annotations.util.HibernateDotNames;
3234
import org.hibernate.metamodel.source.internal.annotations.util.JPADotNames;
3335
import org.hibernate.metamodel.source.spi.AttributePath;
3436
import org.hibernate.metamodel.source.spi.AttributeRole;
3537
import org.hibernate.metamodel.spi.binding.SingularAttributeBinding;
36-
3738
import org.jboss.logging.Logger;
3839

3940
/**
@@ -55,6 +56,11 @@ protected AbstractSingularAttribute(
5556
String accessorStrategy) {
5657
super( container, attributeName, attributePath, attributeRole, backingMember, nature, accessType, accessorStrategy );
5758

59+
if ( backingMember.getAnnotations().containsKey( HibernateDotNames.IMMUTABLE ) ) {
60+
throw new AnnotationException( "@Immutable can be used on entities or collections, not "
61+
+ attributeRole.getFullPath() );
62+
}
63+
5864
ConverterAndOverridesHelper.INSTANCE.processConverters(
5965
getPath(),
6066
getNature(),

hibernate-core/src/test/java/org/hibernate/test/annotations/immutable/ImmutableTest.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,22 @@
2323
*/
2424
package org.hibernate.test.annotations.immutable;
2525

26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.Assert.assertTrue;
29+
import static org.junit.Assert.fail;
30+
2631
import java.util.ArrayList;
2732
import java.util.List;
2833

29-
import org.jboss.logging.Logger;
30-
import org.junit.Test;
31-
3234
import org.hibernate.AnnotationException;
3335
import org.hibernate.HibernateException;
3436
import org.hibernate.Session;
3537
import org.hibernate.Transaction;
36-
import org.hibernate.cfg.Configuration;
37-
38-
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
39-
import org.hibernate.testing.ServiceRegistryBuilder;
38+
import org.hibernate.metamodel.MetadataSources;
4039
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
41-
42-
import static org.junit.Assert.assertEquals;
43-
import static org.junit.Assert.assertNotNull;
44-
import static org.junit.Assert.assertTrue;
45-
import static org.junit.Assert.fail;
40+
import org.jboss.logging.Logger;
41+
import org.junit.Test;
4642

4743
/**
4844
* Tests for <code>Immutable</code> annotation.
@@ -154,13 +150,11 @@ public void testImmutableCollection() {
154150
}
155151

156152
@Test
157-
@FailureExpectedWithNewMetamodel
158153
public void testMisplacedImmutableAnnotation() {
159-
// todo : metamodel is not applying many "out of place" validations
160154
try {
161-
Configuration config = new Configuration();
162-
config.addAnnotatedClass(Foobar.class);
163-
config.buildMappings( );
155+
MetadataSources sources = new MetadataSources( serviceRegistry().getParentServiceRegistry() );
156+
sources.addAnnotatedClass(Foobar.class);
157+
sources.buildMetadata();
164158
fail();
165159
}
166160
catch (AnnotationException ae) {
@@ -170,6 +164,6 @@ public void testMisplacedImmutableAnnotation() {
170164

171165
@Override
172166
protected Class[] getAnnotatedClasses() {
173-
return new Class[] { Country.class, State.class};
167+
return new Class[] { Country.class, State.class };
174168
}
175169
}

0 commit comments

Comments
 (0)