Skip to content

Commit d3de746

Browse files
committed
HHH-10308 : Don't make deep copy of property with AttributeConverter if Java type is known to be immutable
(cherry picked from commit 780c7c2)
1 parent f5197a3 commit d3de746

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/AttributeConverterTypeAdapter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.persistence.AttributeConverter;
1010

1111
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
12+
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
1213
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
1314
import org.hibernate.type.descriptor.java.MutabilityPlan;
1415
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
@@ -32,8 +33,9 @@ public class AttributeConverterTypeAdapter<T> extends AbstractSingleColumnStanda
3233
private final Class jdbcType;
3334
private final AttributeConverter<? extends T,?> attributeConverter;
3435

35-
private final AttributeConverterMutabilityPlanImpl<T> mutabilityPlan;
36+
private final MutabilityPlan<T> mutabilityPlan;
3637

38+
@SuppressWarnings("unchecked")
3739
public AttributeConverterTypeAdapter(
3840
String name,
3941
String description,
@@ -49,7 +51,10 @@ public AttributeConverterTypeAdapter(
4951
this.jdbcType = jdbcType;
5052
this.attributeConverter = attributeConverter;
5153

52-
this.mutabilityPlan = new AttributeConverterMutabilityPlanImpl<T>( attributeConverter );
54+
this.mutabilityPlan =
55+
entityAttributeJavaTypeDescriptor.getMutabilityPlan().isMutable() ?
56+
new AttributeConverterMutabilityPlanImpl<T>( attributeConverter ) :
57+
ImmutableMutabilityPlan.INSTANCE;
5358

5459
log.debug( "Created AttributeConverterTypeAdapter -> " + name );
5560
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,19 @@ public <T> JavaTypeDescriptor<T> getDescriptor(Class<T> cls) {
122122

123123
public static class FallbackJavaTypeDescriptor<T> extends AbstractTypeDescriptor<T> {
124124
@SuppressWarnings("unchecked")
125-
protected FallbackJavaTypeDescriptor(Class<T> type) {
126-
// MutableMutabilityPlan would be the "safest" option, but we do not necessarily know how to deepCopy etc...
127-
super( type, ImmutableMutabilityPlan.INSTANCE );
125+
protected FallbackJavaTypeDescriptor(final Class<T> type) {
126+
// MutableMutabilityPlan is the "safest" option, but we do not necessarily know how to deepCopy etc...
127+
super(
128+
type,
129+
new MutableMutabilityPlan<T>() {
130+
@Override
131+
protected T deepCopyNotNull(T value) {
132+
throw new HibernateException(
133+
"Not known how to deep copy value of type: [" + type.getName() + "]"
134+
);
135+
}
136+
}
137+
);
128138
}
129139

130140
@Override

hibernate-core/src/test/java/org/hibernate/test/converter/DirtyCheckingTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414

1515
import org.hibernate.Session;
1616

17+
import org.hibernate.persister.entity.EntityPersister;
1718
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
1819
import org.junit.Test;
1920

2021
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertFalse;
23+
import static org.junit.Assert.assertTrue;
2124

2225
/**
2326
* @author Steve Ebersole
@@ -109,6 +112,13 @@ public void dirtyCheckAgainstNewNumberInstance() {
109112
session.close();
110113
}
111114

115+
@Test
116+
public void checkConverterMutabilityPlans() {
117+
final EntityPersister persister = sessionFactory().getEntityPersister( SomeEntity.class.getName() );
118+
assertFalse( persister.getPropertyType( "number" ).isMutable() );
119+
assertTrue( persister.getPropertyType( "name" ).isMutable() );
120+
}
121+
112122
@Override
113123
protected Class[] getAnnotatedClasses() {
114124
return new Class[] {SomeEntity.class};

0 commit comments

Comments
 (0)