Skip to content

Commit ad90bf2

Browse files
committed
HHH-16311 - Migrate enum handling away from UserType
1 parent 02141ce commit ad90bf2

File tree

3 files changed

+31
-67
lines changed

3 files changed

+31
-67
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/process/internal/EnumeratedValueResolution.java

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
package org.hibernate.boot.model.process.internal;
88

99
import org.hibernate.mapping.BasicValue;
10-
import org.hibernate.metamodel.mapping.JdbcMapping;
11-
import org.hibernate.type.EnumType;
12-
import org.hibernate.type.descriptor.converter.spi.EnumValueConverter;
1310
import org.hibernate.type.BasicType;
11+
import org.hibernate.type.ConvertedBasicType;
1412
import org.hibernate.type.CustomType;
13+
import org.hibernate.type.EnumType;
14+
import org.hibernate.type.descriptor.converter.spi.EnumValueConverter;
1515
import org.hibernate.type.descriptor.java.EnumJavaType;
1616
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
1717
import org.hibernate.type.descriptor.java.JavaType;
@@ -21,50 +21,36 @@
2121
import org.hibernate.type.spi.TypeConfiguration;
2222

2323
/**
24+
* Resolution for {@linkplain Enum enum} mappings
25+
*
2426
* @author Steve Ebersole
2527
*/
2628
public class EnumeratedValueResolution<E extends Enum<E>,R> implements BasicValue.Resolution<E> {
27-
private final JdbcMapping jdbcMapping;
28-
29-
private final EnumJavaType<E> domainJtd;
30-
private final JavaType<R> jdbcJtd;
31-
private final JdbcType jdbcType;
3229
private final EnumValueConverter<E,R> valueConverter;
3330

31+
private final ConvertedBasicType<E> jdbcMapping;
3432
private final CustomType<E> legacyTypeResolution;
3533

3634
public EnumeratedValueResolution(
37-
EnumJavaType<E> domainJtd,
38-
JavaType<R> jdbcJtd,
3935
JdbcType jdbcType,
4036
EnumValueConverter<E, R> valueConverter,
4137
TypeConfiguration typeConfiguration) {
42-
this.domainJtd = domainJtd;
43-
this.jdbcJtd = jdbcJtd;
44-
this.jdbcType = jdbcType;
4538
this.valueConverter = valueConverter;
4639

47-
this.jdbcMapping = buildJdbcMapping( domainJtd, jdbcType, valueConverter, typeConfiguration );
48-
this.legacyTypeResolution = new CustomType<>(
49-
new EnumType<>( domainJtd.getJavaTypeClass(), valueConverter, typeConfiguration ),
50-
typeConfiguration
51-
);
52-
}
53-
54-
private JdbcMapping buildJdbcMapping(
55-
JavaType<E> domainJtd,
56-
JdbcType jdbcType,
57-
EnumValueConverter<E, R> valueConverter,
58-
TypeConfiguration typeConfiguration) {
59-
return new ConvertedBasicTypeImpl<>(
60-
domainJtd.getJavaTypeClass().getName(),
40+
final EnumJavaType<E> domainJavaType = valueConverter.getDomainJavaType();
41+
this.jdbcMapping = new ConvertedBasicTypeImpl<>(
42+
domainJavaType.getJavaTypeClass().getName(),
6143
jdbcType,
6244
valueConverter
6345
);
46+
this.legacyTypeResolution = new CustomType<>(
47+
new EnumType<>( domainJavaType.getJavaTypeClass(), valueConverter, typeConfiguration ),
48+
typeConfiguration
49+
);
6450
}
6551

6652
@Override
67-
public JdbcMapping getJdbcMapping() {
53+
public ConvertedBasicType<E> getJdbcMapping() {
6854
return jdbcMapping;
6955
}
7056

@@ -75,17 +61,17 @@ public BasicType getLegacyResolvedBasicType() {
7561

7662
@Override
7763
public JavaType<E> getDomainJavaType() {
78-
return domainJtd;
64+
return jdbcMapping.getJavaTypeDescriptor();
7965
}
8066

8167
@Override
8268
public JavaType<?> getRelationalJavaType() {
83-
return jdbcJtd;
69+
return jdbcMapping.getJdbcJavaType();
8470
}
8571

8672
@Override
8773
public JdbcType getJdbcType() {
88-
return jdbcType;
74+
return jdbcMapping.getJdbcType();
8975
}
9076

9177
@Override

hibernate-core/src/main/java/org/hibernate/boot/model/process/internal/InferredBasicValueResolver.java

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.function.Supplier;
1313

1414
import org.hibernate.MappingException;
15+
import org.hibernate.boot.spi.MetadataBuildingContext;
1516
import org.hibernate.dialect.Dialect;
1617
import org.hibernate.mapping.BasicValue;
1718
import org.hibernate.mapping.Column;
@@ -20,14 +21,12 @@
2021
import org.hibernate.tool.schema.extract.spi.ColumnTypeInformation;
2122
import org.hibernate.type.AdjustableBasicType;
2223
import org.hibernate.type.BasicType;
23-
import org.hibernate.type.CustomType;
2424
import org.hibernate.type.SerializableType;
2525
import org.hibernate.type.descriptor.converter.internal.NamedEnumValueConverter;
2626
import org.hibernate.type.descriptor.converter.internal.OrdinalEnumValueConverter;
2727
import org.hibernate.type.descriptor.java.BasicJavaType;
2828
import org.hibernate.type.descriptor.java.BasicPluralJavaType;
2929
import org.hibernate.type.descriptor.java.EnumJavaType;
30-
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
3130
import org.hibernate.type.descriptor.java.JavaType;
3231
import org.hibernate.type.descriptor.java.JavaTypeHelper;
3332
import org.hibernate.type.descriptor.java.MutabilityPlan;
@@ -61,8 +60,10 @@ public static <T> BasicValue.Resolution<T> from(
6160
Selectable selectable,
6261
String ownerName,
6362
String propertyName,
64-
Dialect dialect,
65-
TypeConfiguration typeConfiguration) {
63+
MetadataBuildingContext buildingContext) {
64+
final Dialect dialect = buildingContext.getMetadataCollector().getDatabase().getDialect();
65+
final TypeConfiguration typeConfiguration = buildingContext.getBootstrapContext().getTypeConfiguration();
66+
6667
final JavaType<T> reflectedJtd = reflectedJtdResolver.get();
6768

6869
// NOTE : the distinction that is made below wrt `explicitJavaType` and `reflectedJtd` is
@@ -162,14 +163,14 @@ else if ( explicitJdbcType != null ) {
162163
final JavaType<?> elementJtd = containerJtd.getElementJavaType();
163164
final BasicType registeredElementType;
164165
if ( elementJtd instanceof EnumJavaType ) {
165-
final EnumeratedValueResolution resolution = InferredBasicValueResolver.fromEnum(
166-
(EnumJavaType) elementJtd,
166+
final EnumeratedValueResolution<?,?> resolution = InferredBasicValueResolver.fromEnum(
167+
(EnumJavaType<?>) elementJtd,
167168
null,
168169
null,
169170
stdIndicators,
170171
typeConfiguration
171172
);
172-
registeredElementType = resolution.getLegacyResolvedBasicType();
173+
registeredElementType = resolution.getJdbcMapping();
173174
}
174175
else if ( JavaTypeHelper.isTemporal( elementJtd ) ) {
175176
final InferredBasicValueResolution resolution = InferredBasicValueResolver.fromTemporal(
@@ -352,6 +353,11 @@ public static <E extends Enum<E>, R> EnumeratedValueResolution<E,R> fromEnum(
352353
}
353354

354355
if ( enumStyle == null ) {
356+
// NOTE : separate from the explicit ORDINAL check to facilitate
357+
// handling native database enum types. In theory anyway - atm
358+
// we cannot discern an implicit (default value) or explicit style
359+
// due to HCANN and annotation handling for default values
360+
355361
//noinspection unchecked
356362
return (EnumeratedValueResolution<E, R>) ordinalEnumValueResolution(
357363
enumJavaType,
@@ -375,8 +381,6 @@ private static <E extends Enum<E>, N extends Number> EnumeratedValueResolution<E
375381
final JdbcType jdbcType = ordinalJdbcType( explicitJdbcType, enumJavaType, typeConfiguration );
376382

377383
return new EnumeratedValueResolution<>(
378-
enumJavaType,
379-
relationalJavaType,
380384
jdbcType,
381385
new OrdinalEnumValueConverter<>( enumJavaType, jdbcType, relationalJavaType ),
382386
typeConfiguration
@@ -405,29 +409,6 @@ private static <N extends Number> JavaType<N> ordinalJavaType(JavaType<N> explic
405409
}
406410
}
407411

408-
private static <E extends Enum<E>, N extends Number> InferredBasicValueResolution<E, N> ordinalResolution(
409-
EnumJavaType<E> enumJavaType,
410-
JavaType<N> relationalJtd,
411-
JdbcType jdbcType,
412-
TypeConfiguration typeConfiguration) {
413-
final CustomType<E> customType = new CustomType<>(
414-
new org.hibernate.type.EnumType<>(
415-
enumJavaType.getJavaTypeClass(),
416-
new OrdinalEnumValueConverter<>( enumJavaType, jdbcType, relationalJtd ),
417-
typeConfiguration
418-
),
419-
typeConfiguration
420-
);
421-
return new InferredBasicValueResolution<>(
422-
customType,
423-
enumJavaType,
424-
relationalJtd,
425-
jdbcType,
426-
customType,
427-
ImmutableMutabilityPlan.instance()
428-
);
429-
}
430-
431412
private static <E extends Enum<E>> EnumeratedValueResolution<E,String> stringEnumValueResolution(
432413
EnumJavaType<E> enumJavaType,
433414
BasicJavaType<?> explicitJavaType,
@@ -438,8 +419,6 @@ private static <E extends Enum<E>> EnumeratedValueResolution<E,String> stringEnu
438419
final JdbcType jdbcType = stringJdbcType( explicitJdbcType, stdIndicators, relationalJtd );
439420

440421
return new EnumeratedValueResolution<>(
441-
enumJavaType,
442-
relationalJtd,
443422
jdbcType,
444423
new NamedEnumValueConverter<>( enumJavaType, jdbcType, relationalJtd ),
445424
typeConfiguration

hibernate-core/src/main/java/org/hibernate/mapping/BasicValue.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ public TypeConfiguration getTypeConfiguration() {
485485
column,
486486
ownerName,
487487
propertyName,
488-
getDialect(),
489-
getTypeConfiguration()
488+
getBuildingContext()
490489
);
491490
}
492491
}

0 commit comments

Comments
 (0)