From 91a68c6746deb060745c24d3f9c11af29c6dae29 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 16 Feb 2021 15:11:43 +0100 Subject: [PATCH] HHH-14460 Validate setter exists for persistent property also with mixed access strategy --- .../internal/PropertyAccessMixedImpl.java | 4 +- .../MissingSetterWithEnhancementTest.java | 92 +++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessMixedImpl.java b/hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessMixedImpl.java index 594930b855..23077d7b82 100644 --- a/hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessMixedImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/property/access/internal/PropertyAccessMixedImpl.java @@ -25,7 +25,7 @@ import org.hibernate.property.access.spi.SetterFieldImpl; import org.hibernate.property.access.spi.SetterMethodImpl; import static org.hibernate.internal.util.ReflectHelper.getterMethodOrNull; -import static org.hibernate.internal.util.ReflectHelper.setterMethodOrNull; +import static org.hibernate.internal.util.ReflectHelper.findSetterMethod; /** * A PropertyAccess based on mix of getter/setter method and/or field. @@ -65,7 +65,7 @@ public class PropertyAccessMixedImpl implements PropertyAccess { "Could not locate getter for property named [" + containerJavaType.getName() + "#" + propertyName + "]" ); } - Method setterMethod = setterMethodOrNull( containerJavaType, propertyName, getterMethod.getReturnType() ); + Method setterMethod = findSetterMethod( containerJavaType, propertyName, getterMethod.getReturnType() ); this.getter = propertyGetter( containerJavaType, propertyName, getterMethod ); this.setter = propertySetter( containerJavaType, propertyName, setterMethod ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java new file mode 100644 index 0000000000..4cb59b1ad8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java @@ -0,0 +1,92 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.proxy; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.MappingException; +import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; +import org.hibernate.service.ServiceRegistry; + +import org.hibernate.testing.ServiceRegistryBuilder; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * + * @author Christian Beikov + */ +@TestForIssue(jiraKey = "HHH-14460") +@RunWith( BytecodeEnhancerRunner.class ) +public class MissingSetterWithEnhancementTest { + private ServiceRegistry serviceRegistry; + + @Before + public void setUp() { + final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder(); + builder.applyClassLoader( getClass().getClassLoader() ); + serviceRegistry = new StandardServiceRegistryBuilder( builder.build() ) + .applySettings( Environment.getProperties() ) + .build(); + } + + @After + public void tearDown() { + if ( serviceRegistry != null ) { + ServiceRegistryBuilder.destroy( serviceRegistry ); + } + } + + @Test + public void testEnhancedClassMissesSetterForProperty() { + Configuration cfg = new Configuration(); + cfg.addAnnotatedClass( EntityWithMissingSetter.class ); + try (SessionFactory sf = cfg.buildSessionFactory( serviceRegistry )) { + fail( "Setter is missing for `name`. SessionFactory creation should fail." ); + } + catch (MappingException e) { + assertEquals( + "Could not locate setter method for property [" + EntityWithMissingSetter.class.getName() + "#name]", + e.getCause().getCause().getCause().getMessage() + ); + } + } + + @Entity + public static class EntityWithMissingSetter { + private Long id; + @Column + private int someInt; + + @Id + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return null; + } + + } +}