HHH-10308 : Don't make deep copy of property with AttributeConverter if Java type is known to be immutable

(cherry picked from commit 780c7c2fb6)
This commit is contained in:
Gail Badner 2015-11-19 22:29:14 -08:00
parent f5197a3f04
commit d3de746bd9
3 changed files with 30 additions and 5 deletions

View File

@ -9,6 +9,7 @@ package org.hibernate.type.descriptor.converter;
import javax.persistence.AttributeConverter;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
@ -32,8 +33,9 @@ public class AttributeConverterTypeAdapter<T> extends AbstractSingleColumnStanda
private final Class jdbcType;
private final AttributeConverter<? extends T,?> attributeConverter;
private final AttributeConverterMutabilityPlanImpl<T> mutabilityPlan;
private final MutabilityPlan<T> mutabilityPlan;
@SuppressWarnings("unchecked")
public AttributeConverterTypeAdapter(
String name,
String description,
@ -49,7 +51,10 @@ public class AttributeConverterTypeAdapter<T> extends AbstractSingleColumnStanda
this.jdbcType = jdbcType;
this.attributeConverter = attributeConverter;
this.mutabilityPlan = new AttributeConverterMutabilityPlanImpl<T>( attributeConverter );
this.mutabilityPlan =
entityAttributeJavaTypeDescriptor.getMutabilityPlan().isMutable() ?
new AttributeConverterMutabilityPlanImpl<T>( attributeConverter ) :
ImmutableMutabilityPlan.INSTANCE;
log.debug( "Created AttributeConverterTypeAdapter -> " + name );
}

View File

@ -122,9 +122,19 @@ public class JavaTypeDescriptorRegistry {
public static class FallbackJavaTypeDescriptor<T> extends AbstractTypeDescriptor<T> {
@SuppressWarnings("unchecked")
protected FallbackJavaTypeDescriptor(Class<T> type) {
// MutableMutabilityPlan would be the "safest" option, but we do not necessarily know how to deepCopy etc...
super( type, ImmutableMutabilityPlan.INSTANCE );
protected FallbackJavaTypeDescriptor(final Class<T> type) {
// MutableMutabilityPlan is the "safest" option, but we do not necessarily know how to deepCopy etc...
super(
type,
new MutableMutabilityPlan<T>() {
@Override
protected T deepCopyNotNull(T value) {
throw new HibernateException(
"Not known how to deep copy value of type: [" + type.getName() + "]"
);
}
}
);
}
@Override

View File

@ -14,10 +14,13 @@ import javax.persistence.Id;
import org.hibernate.Session;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Steve Ebersole
@ -109,6 +112,13 @@ public class DirtyCheckingTest extends BaseNonConfigCoreFunctionalTestCase {
session.close();
}
@Test
public void checkConverterMutabilityPlans() {
final EntityPersister persister = sessionFactory().getEntityPersister( SomeEntity.class.getName() );
assertFalse( persister.getPropertyType( "number" ).isMutable() );
assertTrue( persister.getPropertyType( "name" ).isMutable() );
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {SomeEntity.class};