HHH-13891 : ProxyFactory should not be built if any ID or property getter/setter methods are final

(cherry picked from commit a54d657d98)
This commit is contained in:
Gail Badner 2020-03-06 16:50:21 -08:00 committed by gbadner
parent c1f79fd304
commit 3005b4bc60
3 changed files with 22 additions and 22 deletions

View File

@ -343,12 +343,6 @@ public interface CoreMessageLogger extends BasicLogger {
@Message(value = "Found mapping document in jar: %s", id = 109)
void foundMappingDocument(String name);
@LogMessage(level = ERROR)
@Message(value = "Getters of lazy classes cannot be final: %s.%s", id = 112)
void gettersOfLazyClassesCannotBeFinal(
String entityName,
String name);
@LogMessage(level = WARN)
@Message(value = "GUID identifier generated: %s", id = 113)
void guidGenerated(String result);
@ -774,12 +768,6 @@ public interface CoreMessageLogger extends BasicLogger {
@Message(value = "Sessions opened: %s", id = 242)
void sessionsOpened(long sessionOpenCount);
@LogMessage(level = ERROR)
@Message(value = "Setters of lazy classes cannot be final: %s.%s", id = 243)
void settersOfLazyClassesCannotBeFinal(
String entityName,
String name);
@LogMessage(level = WARN)
@Message(value = "@Sort not allowed for an indexed collection, annotation ignored.", id = 244)
void sortAnnotationIndexedCollection();

View File

@ -11,6 +11,7 @@ import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
@ -84,14 +85,21 @@ public final class ProxyFactoryHelper {
Class clazz = persistentClass.getMappedClass();
while ( properties.hasNext() ) {
Property property = (Property) properties.next();
Method method = property.getGetter( clazz ).getMethod();
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
LOG.gettersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() );
}
method = property.getSetter( clazz ).getMethod();
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
LOG.settersOfLazyClassesCannotBeFinal( persistentClass.getEntityName(), property.getName() );
}
validateGetterSetterMethodProxyability( "Getter", property.getGetter( clazz ).getMethod() );
validateGetterSetterMethodProxyability( "Setter", property.getSetter( clazz ).getMethod() );
}
}
public static void validateGetterSetterMethodProxyability(String getterOrSetter, Method method ) {
if ( method != null && Modifier.isFinal( method.getModifiers() ) ) {
throw new HibernateException(
String.format(
"%s methods of lazy classes cannot be final: %s#%s",
getterOrSetter,
method.getDeclaringClass().getName(),
method.getName()
)
);
}
}

View File

@ -89,13 +89,17 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer {
final Set<Class> proxyInterfaces = ProxyFactoryHelper.extractProxyInterfaces( persistentClass, entityName );
ProxyFactoryHelper.validateProxyability( persistentClass );
Method proxyGetIdentifierMethod = ProxyFactoryHelper.extractProxyGetIdentifierMethod( idGetter, proxyInterface );
Method proxySetIdentifierMethod = ProxyFactoryHelper.extractProxySetIdentifierMethod( idSetter, proxyInterface );
ProxyFactory pf = buildProxyFactoryInternal( persistentClass, idGetter, idSetter );
try {
ProxyFactoryHelper.validateGetterSetterMethodProxyability( "Getter", proxyGetIdentifierMethod );
ProxyFactoryHelper.validateGetterSetterMethodProxyability( "Setter", proxySetIdentifierMethod );
ProxyFactoryHelper.validateProxyability( persistentClass );
pf.postInstantiate(
entityName,
mappedClass,