11
11
import org .hibernate .metamodel .spi .ImplicitDiscriminatorStrategy ;
12
12
import org .hibernate .type .AnyType ;
13
13
import org .hibernate .type .MappingContext ;
14
+ import org .hibernate .type .MetaType ;
14
15
import org .hibernate .type .Type ;
15
16
16
17
import java .util .HashMap ;
@@ -46,7 +47,6 @@ public Any(MetadataBuildingContext buildingContext, Table table) {
46
47
47
48
public Any (MetadataBuildingContext buildingContext , Table table , boolean annotations ) {
48
49
super ( buildingContext , table );
49
-
50
50
if ( ! annotations ) {
51
51
metaMapping = new MetaValue ( this ::applySelectableToSuper , buildingContext , table );
52
52
metaMapping .setTypeName ( "string" );
@@ -56,7 +56,6 @@ public Any(MetadataBuildingContext buildingContext, Table table, boolean annotat
56
56
metaMapping = null ;
57
57
keyMapping = null ;
58
58
}
59
-
60
59
}
61
60
62
61
public Any (Any original ) {
@@ -85,15 +84,13 @@ public Any copy() {
85
84
}
86
85
87
86
public void addSelectable (Selectable selectable ) {
88
- if ( selectable == null ) {
89
- return ;
90
- }
91
-
92
- if ( selectable instanceof Column ) {
93
- super .justAddColumn ( (Column ) selectable );
94
- }
95
- else {
96
- super .justAddFormula ( (Formula ) selectable );
87
+ if ( selectable != null ) {
88
+ if ( selectable instanceof Column column ) {
89
+ super .justAddColumn ( column );
90
+ }
91
+ else if ( selectable instanceof Formula formula ) {
92
+ super .justAddFormula ( formula );
93
+ }
97
94
}
98
95
}
99
96
@@ -134,32 +131,12 @@ public void setIdentifierType(String identifierType) {
134
131
@ Override
135
132
public AnyType getType () throws MappingException {
136
133
if ( resolvedType == null ) {
137
- final Type discriminatorType ;
138
- if ( discriminatorDescriptor != null ) {
139
- discriminatorType = discriminatorDescriptor .getType ();
140
- }
141
- else {
142
- discriminatorType = metaMapping .getType ();
143
- }
144
-
145
- final Type identifierType ;
146
- if ( keyDescriptor != null ) {
147
- identifierType = keyDescriptor .getType ();
148
- }
149
- else {
150
- identifierType = keyMapping .getType ();
151
- }
152
-
153
- resolvedType = MappingHelper .anyMapping (
154
- discriminatorType ,
155
- identifierType ,
156
- metaValueToEntityNameMap ,
157
- implicitValueStrategy ,
158
- isLazy (),
159
- getBuildingContext ()
160
- );
134
+ final Type discriminatorType =
135
+ discriminatorDescriptor != null ? discriminatorDescriptor .getType () : metaMapping .getType ();
136
+ final Type identifierType = keyDescriptor != null ? keyDescriptor .getType () : keyMapping .getType ();
137
+ final MetaType metaType = new MetaType ( discriminatorType , implicitValueStrategy , metaValueToEntityNameMap );
138
+ resolvedType = new AnyType ( getTypeConfiguration (), metaType , identifierType , isLazy () );
161
139
}
162
-
163
140
return resolvedType ;
164
141
}
165
142
@@ -181,7 +158,6 @@ public void addFormula(Formula formula) {
181
158
private void applySelectableLocally (Selectable selectable ) {
182
159
// note: adding column to meta or key mapping ultimately calls back into `#applySelectableToSuper`
183
160
// to add the column to the ANY super.
184
-
185
161
if ( discriminatorDescriptor == null && getColumnSpan () == 0 ) {
186
162
if ( selectable instanceof Column ) {
187
163
metaMapping .addColumn ( (Column ) selectable );
@@ -214,9 +190,7 @@ public Map<Object,String> getMetaValues() {
214
190
return metaValueToEntityNameMap ;
215
191
}
216
192
217
- @ SuppressWarnings ( "rawtypes" )
218
- public void setMetaValues (Map metaValueToEntityNameMap ) {
219
- //noinspection unchecked
193
+ public void setMetaValues (Map <Object ,String > metaValueToEntityNameMap ) {
220
194
this .metaValueToEntityNameMap = metaValueToEntityNameMap ;
221
195
}
222
196
@@ -242,8 +216,7 @@ public void setLazy(boolean lazy) {
242
216
}
243
217
244
218
@ Override
245
- public void setTypeUsingReflection (String className , String propertyName )
246
- throws MappingException {
219
+ public void setTypeUsingReflection (String className , String propertyName ) {
247
220
}
248
221
249
222
@ Override
@@ -258,10 +231,10 @@ public boolean isSame(SimpleValue other) {
258
231
259
232
public boolean isSame (Any other ) {
260
233
return super .isSame ( other )
261
- && Objects .equals ( getTypeNameOrNull ( keyMapping ), getTypeNameOrNull ( other .keyMapping ) )
262
- && Objects .equals ( getTypeNameOrNull ( metaMapping ), getTypeNameOrNull ( other .metaMapping ) )
263
- && Objects .equals ( metaValueToEntityNameMap , other .metaValueToEntityNameMap )
264
- && lazy == other .lazy ;
234
+ && Objects .equals ( getTypeNameOrNull ( keyMapping ), getTypeNameOrNull ( other .keyMapping ) )
235
+ && Objects .equals ( getTypeNameOrNull ( metaMapping ), getTypeNameOrNull ( other .metaMapping ) )
236
+ && Objects .equals ( metaValueToEntityNameMap , other .metaValueToEntityNameMap )
237
+ && lazy == other .lazy ;
265
238
}
266
239
267
240
private String getTypeNameOrNull (SimpleValue simpleValue ) {
@@ -277,18 +250,17 @@ public boolean isValid(MappingContext mappingContext) throws MappingException {
277
250
}
278
251
279
252
private static String columnName (Column column , MetadataBuildingContext buildingContext ) {
280
- final JdbcServices jdbcServices = buildingContext
281
- .getBootstrapContext ()
282
- .getServiceRegistry ()
283
- .requireService ( JdbcServices .class );
253
+ final JdbcServices jdbcServices =
254
+ buildingContext .getBootstrapContext ().getServiceRegistry ()
255
+ .requireService ( JdbcServices .class );
284
256
return column .getQuotedName ( jdbcServices .getDialect () );
285
257
}
286
258
287
259
public void setDiscriminator (BasicValue discriminatorDescriptor ) {
288
260
this .discriminatorDescriptor = discriminatorDescriptor ;
289
- if ( discriminatorDescriptor .getColumn () instanceof Column ) {
261
+ if ( discriminatorDescriptor .getColumn () instanceof Column column ) {
290
262
justAddColumn (
291
- ( Column ) discriminatorDescriptor . getColumn () ,
263
+ column ,
292
264
discriminatorDescriptor .isColumnInsertable ( 0 ),
293
265
discriminatorDescriptor .isColumnUpdateable ( 0 )
294
266
);
@@ -300,16 +272,14 @@ public void setDiscriminator(BasicValue discriminatorDescriptor) {
300
272
301
273
public void setDiscriminatorValueMappings (Map <Object , Class <?>> discriminatorValueMappings ) {
302
274
metaValueToEntityNameMap = new HashMap <>();
303
- discriminatorValueMappings .forEach ( (value , entity ) -> {
304
- metaValueToEntityNameMap .put ( value , entity .getName () );
305
- } );
275
+ discriminatorValueMappings .forEach ( (value , entity ) -> metaValueToEntityNameMap .put ( value , entity .getName () ) );
306
276
}
307
277
308
278
public void setKey (BasicValue keyDescriptor ) {
309
279
this .keyDescriptor = keyDescriptor ;
310
- if ( keyDescriptor .getColumn () instanceof Column ) {
280
+ if ( keyDescriptor .getColumn () instanceof Column column ) {
311
281
justAddColumn (
312
- ( Column ) keyDescriptor . getColumn () ,
282
+ column ,
313
283
keyDescriptor .isColumnInsertable ( 0 ),
314
284
keyDescriptor .isColumnUpdateable ( 0 )
315
285
);
@@ -379,10 +349,8 @@ public void addColumn(Column column) {
379
349
if ( columnName != null ) {
380
350
throw new MappingException ( "ANY discriminator already contained column" );
381
351
}
382
-
383
352
super .addColumn ( column );
384
353
this .columnName = columnName ( column , getBuildingContext () );
385
-
386
354
selectableConsumer .accept ( column );
387
355
column .setValue ( this );
388
356
}
@@ -392,10 +360,8 @@ public void addColumn(Column column, boolean isInsertable, boolean isUpdatable)
392
360
if ( columnName != null ) {
393
361
throw new MappingException ( "ANY discriminator already contained column" );
394
362
}
395
-
396
363
super .addColumn ( column , isInsertable , isUpdatable );
397
364
this .columnName = columnName ( column , getBuildingContext () );
398
-
399
365
selectableConsumer .accept ( column );
400
366
column .setValue ( this );
401
367
}
@@ -405,10 +371,8 @@ public void addFormula(Formula formula) {
405
371
if ( columnName != null ) {
406
372
throw new MappingException ( "ANY discriminator already contained column" );
407
373
}
408
-
409
374
super .addFormula ( formula );
410
375
columnName = formula .getFormula ();
411
-
412
376
selectableConsumer .accept ( formula );
413
377
}
414
378
@@ -468,21 +432,18 @@ public void setTypeName(String typeName) {
468
432
@ Override
469
433
public void addColumn (Column column ) {
470
434
super .addColumn ( column );
471
-
472
435
selectableConsumer .accept ( column );
473
436
}
474
437
475
438
@ Override
476
439
public void addColumn (Column column , boolean isInsertable , boolean isUpdatable ) {
477
440
super .addColumn ( column , isInsertable , isUpdatable );
478
-
479
441
selectableConsumer .accept ( column );
480
442
}
481
443
482
444
@ Override
483
445
public void addFormula (Formula formula ) {
484
446
super .addFormula ( formula );
485
-
486
447
selectableConsumer .accept ( formula );
487
448
}
488
449
}
0 commit comments