Skip to content

Commit

Permalink
HHH-13948 - EnhancedSetterImpl should define writeReplace
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole authored and Sanne committed Apr 16, 2020
1 parent ba0902d commit d4d6ae7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Expand Up @@ -6,14 +6,16 @@
*/
package org.hibernate.property.access.spi;

import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
import java.io.Serializable;
import java.lang.reflect.Field;

import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
import org.hibernate.engine.spi.CompositeOwner;
import org.hibernate.engine.spi.CompositeTracker;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.engine.spi.SessionFactoryImplementor;

import java.lang.reflect.Field;
import org.hibernate.property.access.internal.AbstractFieldSerialForm;

/**
* A specialized Setter implementation for handling setting values into
Expand All @@ -25,9 +27,9 @@
* @author Luis Barreiro
*/
public class EnhancedSetterImpl extends SetterFieldImpl {

private final String propertyName;

@SuppressWarnings("rawtypes")
public EnhancedSetterImpl(Class containerClass, String propertyName, Field field) {
super( containerClass, propertyName, field );
this.propertyName = propertyName;
Expand All @@ -46,9 +48,34 @@ public void set(Object target, Object value, SessionFactoryImplementor factory)
// This marks the attribute as initialized, so it doesn't get lazy loaded afterwards
if ( target instanceof PersistentAttributeInterceptable ) {
PersistentAttributeInterceptor interceptor = ( (PersistentAttributeInterceptable) target ).$$_hibernate_getInterceptor();
if ( interceptor != null && interceptor instanceof LazyAttributeLoadingInterceptor ) {
interceptor.attributeInitialized( propertyName );
if ( interceptor instanceof BytecodeLazyAttributeInterceptor ) {
( (BytecodeLazyAttributeInterceptor) interceptor ).attributeInitialized( propertyName );
}
}
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// serialization

private Object writeReplace() {
return new SerialForm( getContainerClass(), propertyName, getField() );
}

@SuppressWarnings("rawtypes")
private static class SerialForm extends AbstractFieldSerialForm implements Serializable {
private final Class containerClass;
private final String propertyName;


private SerialForm(Class containerClass, String propertyName, Field field) {
super( field );
this.containerClass = containerClass;
this.propertyName = propertyName;
}

private Object readResolve() {
return new EnhancedSetterImpl( containerClass, propertyName, resolveField() );
}
}
}
Expand Up @@ -6,7 +6,6 @@
*/
package org.hibernate.property.access.spi;

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -35,6 +34,18 @@ public SetterFieldImpl(Class containerClass, String propertyName, Field field) {
this.setterMethod = ReflectHelper.setterMethodOrNull( containerClass, propertyName, field.getType() );
}

public Class getContainerClass() {
return containerClass;
}

public String getPropertyName() {
return propertyName;
}

protected Field getField() {
return field;
}

@Override
public void set(Object target, Object value, SessionFactoryImplementor factory) {
try {
Expand Down

0 comments on commit d4d6ae7

Please sign in to comment.