6
6
7
7
import jakarta .persistence .GenerationType ;
8
8
import org .hibernate .AnnotationException ;
9
- import org .hibernate .AssertionFailure ;
10
9
import org .hibernate .Internal ;
11
10
import org .hibernate .boot .models .annotations .internal .SequenceGeneratorJpaAnnotation ;
12
11
import org .hibernate .boot .models .annotations .internal .TableGeneratorJpaAnnotation ;
13
12
import org .hibernate .id .IdentifierGenerator ;
14
- import org .hibernate .internal .util .StringHelper ;
15
13
import org .hibernate .models .spi .TypeDetails ;
16
14
17
15
import java .io .Serializable ;
27
25
import static org .hibernate .boot .model .internal .GeneratorStrategies .generatorStrategy ;
28
26
import static org .hibernate .boot .models .JpaAnnotations .SEQUENCE_GENERATOR ;
29
27
import static org .hibernate .boot .models .JpaAnnotations .TABLE_GENERATOR ;
28
+ import static org .hibernate .internal .util .StringHelper .isNotEmpty ;
30
29
import static org .hibernate .internal .util .collections .CollectionHelper .isEmpty ;
31
30
32
31
/**
@@ -49,7 +48,9 @@ public IdentifierGeneratorDefinition(
49
48
final Map <String , String > parameters ) {
50
49
this .name = name ;
51
50
this .strategy = strategy ;
52
- this .parameters = isEmpty ( parameters ) ? emptyMap () : unmodifiableMap ( parameters );
51
+ this .parameters = isEmpty ( parameters )
52
+ ? emptyMap ()
53
+ : unmodifiableMap ( parameters );
53
54
}
54
55
55
56
public IdentifierGeneratorDefinition (
@@ -98,52 +99,37 @@ public static IdentifierGeneratorDefinition createImplicit(
98
99
// If we were unable to locate an actual matching named generator assume
99
100
// a sequence/table of the given name, make one based on GenerationType.
100
101
101
- if ( generationType == null ) {
102
- generationType = GenerationType .SEQUENCE ;
103
- }
104
-
105
- switch ( generationType ) {
106
- case SEQUENCE :
107
- return buildSequenceGeneratorDefinition ( name );
108
- case TABLE :
109
- return buildTableGeneratorDefinition ( name );
110
- case IDENTITY :
111
- throw new AnnotationException (
112
- "@GeneratedValue annotation specified 'strategy=IDENTITY' and 'generator'"
113
- + " but the generator name is unnecessary"
114
- );
115
- case UUID :
116
- throw new AnnotationException (
117
- "@GeneratedValue annotation specified 'strategy=UUID' and 'generator'"
118
- + " but the generator name is unnecessary"
119
- );
120
- case AUTO :
121
- return new IdentifierGeneratorDefinition (
122
- name ,
123
- generatorStrategy ( generationType , generatorName , idType ),
124
- singletonMap ( IdentifierGenerator .GENERATOR_NAME , name )
125
- );
126
- default :
127
- throw new AssertionFailure ( "unknown generator type: " + generationType );
128
- }
102
+ return switch ( generationType == null ? GenerationType .SEQUENCE : generationType ) {
103
+ case SEQUENCE -> buildSequenceGeneratorDefinition ( name );
104
+ case TABLE -> buildTableGeneratorDefinition ( name );
105
+ case AUTO -> new IdentifierGeneratorDefinition (
106
+ name ,
107
+ generatorStrategy ( generationType , generatorName , idType ),
108
+ singletonMap ( IdentifierGenerator .GENERATOR_NAME , name )
109
+ );
110
+ case IDENTITY , UUID -> throw new AnnotationException (
111
+ "@GeneratedValue annotation specified 'strategy=" + generationType
112
+ + "' and 'generator' but the generator name is unnecessary"
113
+ );
114
+ };
129
115
}
130
116
131
117
private static IdentifierGeneratorDefinition buildTableGeneratorDefinition (String name ) {
132
- final Builder builder = new Builder ();
133
118
final TableGeneratorJpaAnnotation tableGeneratorUsage = TABLE_GENERATOR .createUsage ( null );
134
- if ( StringHelper . isNotEmpty ( name ) ) {
119
+ if ( isNotEmpty ( name ) ) {
135
120
tableGeneratorUsage .name ( name );
136
121
}
122
+ final Builder builder = new Builder ();
137
123
interpretTableGenerator ( tableGeneratorUsage , builder );
138
124
return builder .build ();
139
125
}
140
126
141
127
private static IdentifierGeneratorDefinition buildSequenceGeneratorDefinition (String name ) {
142
- final Builder builder = new Builder ();
143
128
final SequenceGeneratorJpaAnnotation sequenceGeneratorUsage = SEQUENCE_GENERATOR .createUsage ( null );
144
- if ( StringHelper . isNotEmpty ( name ) ) {
129
+ if ( isNotEmpty ( name ) ) {
145
130
sequenceGeneratorUsage .name ( name );
146
131
}
132
+ final Builder builder = new Builder ();
147
133
interpretSequenceGenerator ( sequenceGeneratorUsage , builder );
148
134
return builder .build ();
149
135
}
0 commit comments