diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java index b1f9ed1685..a1f89fd5ea 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java @@ -1143,7 +1143,7 @@ public abstract class CollectionBinder { Locale.ROOT, "Property '%s.%s' is not a collection and may not be a '@OneToMany', '@ManyToMany', or '@ElementCollection'", property.getDeclaringType().getName(), - property.getName() + property.resolveAttributeName() ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/collections/custom/declaredtype/UserWithUnimplementedCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/collections/custom/declaredtype/UserWithUnimplementedCollectionTest.java index 4fb6d9f4ce..4c608fee95 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/collections/custom/declaredtype/UserWithUnimplementedCollectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/collections/custom/declaredtype/UserWithUnimplementedCollectionTest.java @@ -31,46 +31,48 @@ package org.hibernate.orm.test.mapping.collections.custom.declaredtype; import org.hibernate.AnnotationException; +import org.hibernate.SessionFactory; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; + import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.DomainModelScope; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.ServiceRegistryScope; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.OneToMany; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; /** + * Test that we get an exception when an attribute whose type is not a Collection + * is annotated with any of + * The test specifically uses {@linkplain OneToMany}, but the handling is the same + * * @author Max Rydahl Andersen - * @author David Weinberg Negative test when specifying a type that can't be mapped as a collection + * @author David Weinberg */ -public class UserWithUnimplementedCollectionTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ UserWithUnimplementedCollection.class, Email.class }; - } - - @Override - protected String getCacheConcurrencyStrategy() { - return "nonstrict-read-write"; - } - - @Override - protected void buildSessionFactory() { +@ServiceRegistry +public class UserWithUnimplementedCollectionTest { + @Test + void testCollectionNotCollectionFailure(ServiceRegistryScope serviceRegistryScope) { + final MetadataSources metadataSources = new MetadataSources( serviceRegistryScope.getRegistry() ); + metadataSources.addAnnotatedClasses( UserWithUnimplementedCollection.class, Email.class ); try { - super.buildSessionFactory(); - fail( "Expected exception" ); + metadataSources.buildMetadata(); + fail( "Expecting an AnnotationException" ); } - catch (Exception e) { - assertThat( e ).isInstanceOf( AnnotationException.class ); + catch (AnnotationException e) { assertThat( e ).hasMessageEndingWith( "is not a collection and may not be a '@OneToMany', '@ManyToMany', or '@ElementCollection'" ); assertThat( e ).hasMessageContaining( ".emailAddresses" ); } } - - @Test - public void testSessionFactoryFailsToBeCreated() { - - } - }