66
77import java .util .function .Supplier ;
88
9- import org .hibernate .MappingException ;
109import org .hibernate .engine .spi .IdentifierValue ;
1110import org .hibernate .engine .spi .VersionValue ;
1211import org .hibernate .mapping .KeyValue ;
@@ -35,28 +34,30 @@ public static IdentifierValue getUnsavedIdentifierValue(
3534 JavaType <?> idJavaType ,
3635 Getter getter ,
3736 Supplier <?> templateInstanceAccess ) {
38- final String unsavedValue = bootIdMapping .getNullValue ();
39- if ( unsavedValue == null ) {
40- if ( getter != null && templateInstanceAccess != null ) {
41- // use the id value of a newly instantiated instance as the unsaved-value
42- final Object defaultValue = getter .get ( templateInstanceAccess .get () );
43- return new IdentifierValue ( defaultValue );
44- }
45- else if ( idJavaType instanceof PrimitiveJavaType <?> primitiveJavaType ) {
46- return new IdentifierValue ( primitiveJavaType .getDefaultValue () );
47- }
48- else {
49- return IdentifierValue .NULL ;
50- }
37+ return switch ( bootIdMapping .getNullValueSemantic () ) {
38+ case null -> inferUnsavedIdentifierValue ( idJavaType , getter , templateInstanceAccess );
39+ case UNDEFINED -> IdentifierValue .UNDEFINED ;
40+ case NULL -> IdentifierValue .NULL ;
41+ case ANY -> IdentifierValue .ANY ;
42+ case NONE -> IdentifierValue .NONE ;
43+ case VALUE -> new IdentifierValue ( idJavaType .fromString ( bootIdMapping .getNullValue () ) );
44+ default -> throw new IllegalArgumentException ( "Illegal null-value semantic: "
45+ + bootIdMapping .getNullValueSemantic () );
46+ };
47+ }
48+
49+ private static IdentifierValue inferUnsavedIdentifierValue (
50+ JavaType <?> idJavaType , Getter getter , Supplier <?> templateInstanceAccess ) {
51+ if ( getter != null && templateInstanceAccess != null ) {
52+ // use the id value of a newly instantiated instance as the unsaved-value
53+ final Object defaultValue = getter .get ( templateInstanceAccess .get () );
54+ return new IdentifierValue ( defaultValue );
55+ }
56+ else if ( idJavaType instanceof PrimitiveJavaType <?> primitiveJavaType ) {
57+ return new IdentifierValue ( primitiveJavaType .getDefaultValue () );
5158 }
5259 else {
53- return switch ( unsavedValue ) {
54- case "null" -> IdentifierValue .NULL ;
55- case "undefined" -> IdentifierValue .UNDEFINED ;
56- case "none" -> IdentifierValue .NONE ;
57- case "any" -> IdentifierValue .ANY ;
58- default -> new IdentifierValue ( idJavaType .fromString ( unsavedValue ) );
59- };
60+ return IdentifierValue .NULL ;
6061 }
6162 }
6263
@@ -72,31 +73,32 @@ public static <T> VersionValue getUnsavedVersionValue(
7273 VersionJavaType <T > versionJavaType ,
7374 Getter getter ,
7475 Supplier <?> templateInstanceAccess ) {
75- final String unsavedValue = bootVersionMapping .getNullValue ();
76- if ( unsavedValue == null ) {
77- if ( getter != null && templateInstanceAccess != null ) {
78- final Object defaultValue = getter .get ( templateInstanceAccess .get () );
79- // if the version of a newly instantiated object is null
80- // or a negative number, use that value as the unsaved-value,
81- // otherwise assume it's the initial version set by program
82- return isNullInitialVersion ( defaultValue )
83- ? new VersionValue ( defaultValue )
84- : VersionValue .UNDEFINED ;
85- }
86- else {
87- return VersionValue .UNDEFINED ;
88- }
76+ return switch ( bootVersionMapping .getNullValueSemantic () ) {
77+ case null -> inferUnsavedVersionValue ( versionJavaType , getter , templateInstanceAccess );
78+ case UNDEFINED -> VersionValue .UNDEFINED ;
79+ case NULL -> VersionValue .NULL ;
80+ case NEGATIVE -> VersionValue .NEGATIVE ;
81+ // this should not happen since the DTD prevents it
82+ case VALUE -> new VersionValue ( versionJavaType .fromString ( bootVersionMapping .getNullValue () ) );
83+ default -> throw new IllegalArgumentException ( "Illegal null-value semantic: "
84+ + bootVersionMapping .getNullValueSemantic () );
85+ };
86+ }
87+
88+ private static VersionValue inferUnsavedVersionValue (
89+ VersionJavaType <?> versionJavaType , Getter getter , Supplier <?> templateInstanceAccess ) {
90+ if ( getter != null && templateInstanceAccess != null ) {
91+ final Object defaultValue = getter .get ( templateInstanceAccess .get () );
92+ // if the version of a newly instantiated object is null
93+ // or a negative number, use that value as the unsaved-value,
94+ // otherwise assume it's the initial version set by program
95+ return isNullInitialVersion ( defaultValue )
96+ ? new VersionValue ( defaultValue )
97+ : VersionValue .UNDEFINED ;
8998 }
9099 else {
91- // this should not happen since the DTD prevents it
92- return switch ( unsavedValue ) {
93- case "undefined" -> VersionValue .UNDEFINED ;
94- case "null" -> VersionValue .NULL ;
95- case "negative" -> VersionValue .NEGATIVE ;
96- default -> throw new MappingException ( "Could not parse version unsaved-value: " + unsavedValue );
97- };
100+ return VersionValue .UNDEFINED ;
98101 }
99-
100102 }
101103
102104 private UnsavedValueFactory () {
0 commit comments