23
23
import org .hibernate .type .BasicPluralType ;
24
24
import org .hibernate .type .BasicType ;
25
25
import org .hibernate .type .SerializableType ;
26
+ import org .hibernate .type .descriptor .WrapperOptions ;
26
27
import org .hibernate .type .descriptor .converter .internal .NamedEnumValueConverter ;
28
+ import org .hibernate .type .descriptor .converter .internal .NativeEnumValueConverter ;
27
29
import org .hibernate .type .descriptor .converter .internal .OrdinalEnumValueConverter ;
30
+ import org .hibernate .type .descriptor .converter .spi .EnumValueConverter ;
28
31
import org .hibernate .type .descriptor .java .BasicJavaType ;
29
32
import org .hibernate .type .descriptor .java .BasicPluralJavaType ;
30
33
import org .hibernate .type .descriptor .java .EnumJavaType ;
31
34
import org .hibernate .type .descriptor .java .JavaType ;
32
35
import org .hibernate .type .descriptor .java .JavaTypeHelper ;
33
36
import org .hibernate .type .descriptor .java .MutabilityPlan ;
37
+ import org .hibernate .type .descriptor .java .ObjectJavaType ;
34
38
import org .hibernate .type .descriptor .java .SerializableJavaType ;
35
39
import org .hibernate .type .descriptor .java .TemporalJavaType ;
36
40
import org .hibernate .type .descriptor .jdbc .JdbcType ;
37
41
import org .hibernate .type .descriptor .jdbc .JdbcTypeIndicators ;
42
+ import org .hibernate .type .descriptor .jdbc .NativeEnumJdbcType ;
38
43
import org .hibernate .type .descriptor .jdbc .ObjectJdbcType ;
39
44
import org .hibernate .type .spi .TypeConfiguration ;
40
45
43
48
44
49
import static org .hibernate .type .SqlTypes .SMALLINT ;
45
50
import static org .hibernate .type .SqlTypes .TINYINT ;
51
+ import static org .hibernate .type .SqlTypes .isCharacterType ;
46
52
47
53
/**
48
54
* BasicValue.Resolution resolver for cases where no explicit
@@ -331,28 +337,6 @@ public static <E extends Enum<E>, R> EnumeratedValueResolution<E,R> fromEnum(
331
337
JdbcTypeIndicators stdIndicators ,
332
338
MetadataBuildingContext context ) {
333
339
final EnumType enumStyle = stdIndicators .getEnumeratedType ();
334
-
335
- if ( enumStyle == EnumType .STRING ) {
336
- //noinspection unchecked
337
- return (EnumeratedValueResolution <E , R >) stringEnumValueResolution (
338
- enumJavaType ,
339
- explicitJavaType ,
340
- explicitJdbcType ,
341
- stdIndicators ,
342
- context
343
- );
344
- }
345
-
346
- if ( enumStyle == EnumType .ORDINAL ) {
347
- //noinspection unchecked
348
- return (EnumeratedValueResolution <E , R >) ordinalEnumValueResolution (
349
- enumJavaType ,
350
- (BasicJavaType <? extends Number >)explicitJavaType ,
351
- explicitJdbcType ,
352
- context
353
- );
354
- }
355
-
356
340
if ( enumStyle == null ) {
357
341
// NOTE : separate from the explicit ORDINAL check to facilitate
358
342
// handling native database enum types. In theory anyway - atm
@@ -367,8 +351,27 @@ public static <E extends Enum<E>, R> EnumeratedValueResolution<E,R> fromEnum(
367
351
context
368
352
);
369
353
}
370
-
371
- throw new MappingException ( "Unknown enumeration-style (JPA EnumType) : " + enumStyle );
354
+ switch ( enumStyle ) {
355
+ case STRING :
356
+ //noinspection unchecked
357
+ return (EnumeratedValueResolution <E , R >) stringEnumValueResolution (
358
+ enumJavaType ,
359
+ explicitJavaType ,
360
+ explicitJdbcType ,
361
+ stdIndicators ,
362
+ context
363
+ );
364
+ case ORDINAL :
365
+ //noinspection unchecked
366
+ return (EnumeratedValueResolution <E , R >) ordinalEnumValueResolution (
367
+ enumJavaType ,
368
+ (BasicJavaType <? extends Number >)explicitJavaType ,
369
+ explicitJdbcType ,
370
+ context
371
+ );
372
+ default :
373
+ throw new MappingException ( "Unknown enumeration-style (JPA EnumType) : " + enumStyle );
374
+ }
372
375
}
373
376
374
377
private static <E extends Enum <E >, N extends Number > EnumeratedValueResolution <E ,N > ordinalEnumValueResolution (
@@ -378,11 +381,9 @@ private static <E extends Enum<E>, N extends Number> EnumeratedValueResolution<E
378
381
MetadataBuildingContext context ) {
379
382
final JdbcType jdbcType = ordinalJdbcType ( explicitJdbcType , enumJavaType , context );
380
383
final JavaType <N > relationalJavaType = ordinalJavaType ( explicitJavaType , jdbcType , context );
381
-
382
384
return new EnumeratedValueResolution <>(
383
385
jdbcType ,
384
- new OrdinalEnumValueConverter <>( enumJavaType , jdbcType , relationalJavaType ),
385
- context
386
+ new OrdinalEnumValueConverter <>( enumJavaType , jdbcType , relationalJavaType )
386
387
);
387
388
}
388
389
@@ -418,7 +419,7 @@ private static <N extends Number> JavaType<N> ordinalJavaType(
418
419
}
419
420
}
420
421
421
- private static <E extends Enum <E >> EnumeratedValueResolution <E ,String > stringEnumValueResolution (
422
+ private static <E extends Enum <E >> EnumeratedValueResolution <E ,? > stringEnumValueResolution (
422
423
EnumJavaType <E > enumJavaType ,
423
424
BasicJavaType <?> explicitJavaType ,
424
425
JdbcType explicitJdbcType ,
@@ -427,19 +428,28 @@ private static <E extends Enum<E>> EnumeratedValueResolution<E,String> stringEnu
427
428
final JdbcType jdbcType = explicitJdbcType == null
428
429
? enumJavaType .getRecommendedJdbcType ( stdIndicators )
429
430
: explicitJdbcType ;
430
- final JavaType <String > relationalJtd = stringJavaType ( explicitJavaType , stdIndicators , context );
431
-
432
- return new EnumeratedValueResolution <>(
433
- jdbcType ,
434
- new NamedEnumValueConverter <>( enumJavaType , jdbcType , relationalJtd ),
435
- context
436
- );
437
- }
438
-
439
- private static JdbcType stringJdbcType (JdbcType explicitJdbcType , JdbcTypeIndicators stdIndicators , JavaType <String > relationalJtd ) {
440
- return explicitJdbcType != null
441
- ? explicitJdbcType
442
- : relationalJtd .getRecommendedJdbcType ( stdIndicators );
431
+ final EnumValueConverter <E ,?> converter ;
432
+ if ( jdbcType instanceof NativeEnumJdbcType ) {
433
+ converter = new NativeEnumValueConverter <>( enumJavaType , jdbcType , new ObjectJavaType () {
434
+ @ Override
435
+ public <X > X unwrap (Object value , Class <X > type , WrapperOptions options ) {
436
+ if ( String .class .equals (type ) && value instanceof Enum ) {
437
+ return (X ) ((Enum <?>) value ).name ();
438
+ }
439
+ else {
440
+ return super .unwrap (value , type , options );
441
+ }
442
+ }
443
+ } );
444
+ }
445
+ else {
446
+ converter = new NamedEnumValueConverter <>(
447
+ enumJavaType ,
448
+ jdbcType ,
449
+ stringJavaType ( explicitJavaType , stdIndicators , context )
450
+ );
451
+ }
452
+ return new EnumeratedValueResolution <>( jdbcType , converter );
443
453
}
444
454
445
455
private static JavaType <String > stringJavaType (
0 commit comments