1111import org .hibernate .metamodel .spi .ImplicitDiscriminatorStrategy ;
1212import org .hibernate .type .AnyType ;
1313import org .hibernate .type .MappingContext ;
14+ import org .hibernate .type .MetaType ;
1415import org .hibernate .type .Type ;
1516
1617import java .util .HashMap ;
@@ -46,7 +47,6 @@ public Any(MetadataBuildingContext buildingContext, Table table) {
4647
4748 public Any (MetadataBuildingContext buildingContext , Table table , boolean annotations ) {
4849 super ( buildingContext , table );
49-
5050 if ( ! annotations ) {
5151 metaMapping = new MetaValue ( this ::applySelectableToSuper , buildingContext , table );
5252 metaMapping .setTypeName ( "string" );
@@ -56,7 +56,6 @@ public Any(MetadataBuildingContext buildingContext, Table table, boolean annotat
5656 metaMapping = null ;
5757 keyMapping = null ;
5858 }
59-
6059 }
6160
6261 public Any (Any original ) {
@@ -85,15 +84,13 @@ public Any copy() {
8584 }
8685
8786 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+ }
9794 }
9895 }
9996
@@ -134,32 +131,12 @@ public void setIdentifierType(String identifierType) {
134131 @ Override
135132 public AnyType getType () throws MappingException {
136133 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 () );
161139 }
162-
163140 return resolvedType ;
164141 }
165142
@@ -181,7 +158,6 @@ public void addFormula(Formula formula) {
181158 private void applySelectableLocally (Selectable selectable ) {
182159 // note: adding column to meta or key mapping ultimately calls back into `#applySelectableToSuper`
183160 // to add the column to the ANY super.
184-
185161 if ( discriminatorDescriptor == null && getColumnSpan () == 0 ) {
186162 if ( selectable instanceof Column ) {
187163 metaMapping .addColumn ( (Column ) selectable );
@@ -214,9 +190,7 @@ public Map<Object,String> getMetaValues() {
214190 return metaValueToEntityNameMap ;
215191 }
216192
217- @ SuppressWarnings ( "rawtypes" )
218- public void setMetaValues (Map metaValueToEntityNameMap ) {
219- //noinspection unchecked
193+ public void setMetaValues (Map <Object ,String > metaValueToEntityNameMap ) {
220194 this .metaValueToEntityNameMap = metaValueToEntityNameMap ;
221195 }
222196
@@ -242,8 +216,7 @@ public void setLazy(boolean lazy) {
242216 }
243217
244218 @ Override
245- public void setTypeUsingReflection (String className , String propertyName )
246- throws MappingException {
219+ public void setTypeUsingReflection (String className , String propertyName ) {
247220 }
248221
249222 @ Override
@@ -258,10 +231,10 @@ public boolean isSame(SimpleValue other) {
258231
259232 public boolean isSame (Any other ) {
260233 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 ;
265238 }
266239
267240 private String getTypeNameOrNull (SimpleValue simpleValue ) {
@@ -277,18 +250,17 @@ public boolean isValid(MappingContext mappingContext) throws MappingException {
277250 }
278251
279252 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 );
284256 return column .getQuotedName ( jdbcServices .getDialect () );
285257 }
286258
287259 public void setDiscriminator (BasicValue discriminatorDescriptor ) {
288260 this .discriminatorDescriptor = discriminatorDescriptor ;
289- if ( discriminatorDescriptor .getColumn () instanceof Column ) {
261+ if ( discriminatorDescriptor .getColumn () instanceof Column column ) {
290262 justAddColumn (
291- ( Column ) discriminatorDescriptor . getColumn () ,
263+ column ,
292264 discriminatorDescriptor .isColumnInsertable ( 0 ),
293265 discriminatorDescriptor .isColumnUpdateable ( 0 )
294266 );
@@ -300,16 +272,14 @@ public void setDiscriminator(BasicValue discriminatorDescriptor) {
300272
301273 public void setDiscriminatorValueMappings (Map <Object , Class <?>> discriminatorValueMappings ) {
302274 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 () ) );
306276 }
307277
308278 public void setKey (BasicValue keyDescriptor ) {
309279 this .keyDescriptor = keyDescriptor ;
310- if ( keyDescriptor .getColumn () instanceof Column ) {
280+ if ( keyDescriptor .getColumn () instanceof Column column ) {
311281 justAddColumn (
312- ( Column ) keyDescriptor . getColumn () ,
282+ column ,
313283 keyDescriptor .isColumnInsertable ( 0 ),
314284 keyDescriptor .isColumnUpdateable ( 0 )
315285 );
@@ -379,10 +349,8 @@ public void addColumn(Column column) {
379349 if ( columnName != null ) {
380350 throw new MappingException ( "ANY discriminator already contained column" );
381351 }
382-
383352 super .addColumn ( column );
384353 this .columnName = columnName ( column , getBuildingContext () );
385-
386354 selectableConsumer .accept ( column );
387355 column .setValue ( this );
388356 }
@@ -392,10 +360,8 @@ public void addColumn(Column column, boolean isInsertable, boolean isUpdatable)
392360 if ( columnName != null ) {
393361 throw new MappingException ( "ANY discriminator already contained column" );
394362 }
395-
396363 super .addColumn ( column , isInsertable , isUpdatable );
397364 this .columnName = columnName ( column , getBuildingContext () );
398-
399365 selectableConsumer .accept ( column );
400366 column .setValue ( this );
401367 }
@@ -405,10 +371,8 @@ public void addFormula(Formula formula) {
405371 if ( columnName != null ) {
406372 throw new MappingException ( "ANY discriminator already contained column" );
407373 }
408-
409374 super .addFormula ( formula );
410375 columnName = formula .getFormula ();
411-
412376 selectableConsumer .accept ( formula );
413377 }
414378
@@ -468,21 +432,18 @@ public void setTypeName(String typeName) {
468432 @ Override
469433 public void addColumn (Column column ) {
470434 super .addColumn ( column );
471-
472435 selectableConsumer .accept ( column );
473436 }
474437
475438 @ Override
476439 public void addColumn (Column column , boolean isInsertable , boolean isUpdatable ) {
477440 super .addColumn ( column , isInsertable , isUpdatable );
478-
479441 selectableConsumer .accept ( column );
480442 }
481443
482444 @ Override
483445 public void addFormula (Formula formula ) {
484446 super .addFormula ( formula );
485-
486447 selectableConsumer .accept ( formula );
487448 }
488449 }
0 commit comments