diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ClassPropertyHolder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ClassPropertyHolder.java index 51f4a0c0f396..df560e87b856 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ClassPropertyHolder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ClassPropertyHolder.java @@ -402,8 +402,8 @@ else if ( value instanceof Component component ) { component.setTypeName( typeName ); } } - else if ( value instanceof SimpleValue ) { - ( (SimpleValue) value ).setTypeName( typeName ); + else if ( value instanceof SimpleValue simpleValue ) { + simpleValue.setTypeName( typeName ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java index 6ff58c421e3d..fcdd013d82b6 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java @@ -2419,8 +2419,8 @@ private static void handleGenerationTiming( GenerationTiming timing) { if ( timing != null ) { if ( (timing == GenerationTiming.INSERT || timing == GenerationTiming.UPDATE) - && property.getValue() instanceof SimpleValue - && ((SimpleValue) property.getValue()).isVersion() ) { + && property.getValue() instanceof SimpleValue simpleValue + && simpleValue.isVersion() ) { // this is enforced by DTD, but just make sure throw new MappingException( "'generated' attribute cannot be 'insert' or 'update' for version/timestamp property", @@ -2685,8 +2685,8 @@ private static void bindSimpleValueType( if ( CollectionHelper.isNotEmpty( typeResolution.parameters ) ) { simpleValue.setTypeParameters( typeResolution.parameters ); - if ( simpleValue instanceof BasicValue ) { - ( (BasicValue) simpleValue ).setExplicitTypeParams( typeResolution.parameters ); + if ( simpleValue instanceof BasicValue basicValue ) { + basicValue.setExplicitTypeParams( typeResolution.parameters ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/InverseDistributionFunction.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/InverseDistributionFunction.java index eba6bf8fdc21..edffe43cf4c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/InverseDistributionFunction.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/InverseDistributionFunction.java @@ -185,18 +185,12 @@ protected MappingModelExpressible getMappingModelExpressible( .getSortExpression() .accept( walker ); final JdbcMappingContainer expressionType = expression.getExpressionType(); - if ( expressionType instanceof BasicValuedMapping ) { - return (BasicValuedMapping) expressionType; + if ( expressionType instanceof BasicValuedMapping basicValuedMapping ) { + return basicValuedMapping; } try { - return walker.getCreationContext() - .getSessionFactory() - .getRuntimeMetamodels() - .getMappingMetamodel() - .resolveMappingExpressible( - getNodeType(), - walker.getFromClauseAccess()::getTableGroup - ); + return walker.getCreationContext().getSessionFactory().getMappingMetamodel() + .resolveMappingExpressible( getNodeType(), walker.getFromClauseAccess()::getTableGroup ); } catch (Exception e) { return null; // this works at least approximately diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayViaArgumentReturnTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayViaArgumentReturnTypeResolver.java index f3a7beaf5f39..919212d78416 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayViaArgumentReturnTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayViaArgumentReturnTypeResolver.java @@ -48,8 +48,8 @@ public ReturnableType resolveFunctionReturnType( if ( inferredType instanceof ReturnableType ) { return (ReturnableType) inferredType; } - else if ( inferredType instanceof BasicValuedMapping ) { - return (ReturnableType) ( (BasicValuedMapping) inferredType ).getJdbcMapping(); + else if ( inferredType instanceof BasicValuedMapping basicValuedMapping ) { + return (ReturnableType) basicValuedMapping.getJdbcMapping(); } } if ( impliedType != null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayViaElementArgumentReturnTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayViaElementArgumentReturnTypeResolver.java index 486c1dfa8baa..6c576d86317b 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayViaElementArgumentReturnTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayViaElementArgumentReturnTypeResolver.java @@ -52,8 +52,8 @@ public ReturnableType resolveFunctionReturnType( if ( inferredType instanceof ReturnableType ) { return (ReturnableType) inferredType; } - else if ( inferredType instanceof BasicValuedMapping ) { - return (ReturnableType) ( (BasicValuedMapping) inferredType ).getJdbcMapping(); + else if ( inferredType instanceof BasicValuedMapping basicValuedMapping ) { + return (ReturnableType) basicValuedMapping.getJdbcMapping(); } } if ( impliedType != null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ElementViaArrayArgumentReturnTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ElementViaArrayArgumentReturnTypeResolver.java index 29b1ee815088..4dbd8236ae45 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ElementViaArrayArgumentReturnTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/ElementViaArrayArgumentReturnTypeResolver.java @@ -9,7 +9,6 @@ import org.hibernate.metamodel.mapping.BasicValuedMapping; import org.hibernate.metamodel.mapping.MappingModelExpressible; -import org.hibernate.metamodel.model.domain.DomainType; import org.hibernate.metamodel.model.domain.ReturnableType; import org.hibernate.query.sqm.SqmExpressible; import org.hibernate.query.sqm.produce.function.FunctionReturnTypeResolver; @@ -41,26 +40,23 @@ public ReturnableType resolveFunctionReturnType( @Nullable SqmToSqlAstConverter converter, List> arguments, TypeConfiguration typeConfiguration) { - final MappingModelExpressible inferredType = converter == null - ? null - : converter.resolveFunctionImpliedReturnType(); + final MappingModelExpressible inferredType = + converter == null ? null : converter.resolveFunctionImpliedReturnType(); if ( inferredType != null ) { - if ( inferredType instanceof ReturnableType ) { - return (ReturnableType) inferredType; + if ( inferredType instanceof ReturnableType returnableType ) { + return returnableType; } - else if ( inferredType instanceof BasicValuedMapping ) { - return (ReturnableType) ( (BasicValuedMapping) inferredType ).getJdbcMapping(); + else if ( inferredType instanceof BasicValuedMapping basicValuedMapping ) { + return (ReturnableType) basicValuedMapping.getJdbcMapping(); } } if ( impliedType != null ) { return impliedType; } final SqmExpressible expressible = arguments.get( arrayIndex ).getExpressible(); - final DomainType type; - if ( expressible != null && ( type = expressible.getSqmType() ) instanceof BasicPluralType ) { - return ( (BasicPluralType) type ).getElementType(); - } - return null; + return expressible != null && expressible.getSqmType() instanceof BasicPluralType type + ? type.getElementType() + : null; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/JsonArrayViaElementArgumentReturnTypeResolver.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/JsonArrayViaElementArgumentReturnTypeResolver.java index 2f42642a4bea..97a5228b6c69 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/array/JsonArrayViaElementArgumentReturnTypeResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/array/JsonArrayViaElementArgumentReturnTypeResolver.java @@ -8,7 +8,6 @@ import java.util.List; import java.util.function.Supplier; -import org.hibernate.dialect.Dialect; import org.hibernate.metamodel.mapping.BasicValuedMapping; import org.hibernate.metamodel.mapping.MappingModelExpressible; import org.hibernate.metamodel.model.domain.DomainType; @@ -52,11 +51,11 @@ public ReturnableType resolveFunctionReturnType( } final MappingModelExpressible inferredType = converter.resolveFunctionImpliedReturnType(); if ( inferredType != null ) { - if ( inferredType instanceof ReturnableType ) { - return (ReturnableType) inferredType; + if ( inferredType instanceof ReturnableType returnableType ) { + return returnableType; } - else if ( inferredType instanceof BasicValuedMapping ) { - return (ReturnableType) ( (BasicValuedMapping) inferredType ).getJdbcMapping(); + else if ( inferredType instanceof BasicValuedMapping basicValuedMapping ) { + return (ReturnableType) basicValuedMapping.getJdbcMapping(); } } } @@ -79,25 +78,25 @@ public BasicValuedMapping resolveFunctionReturnType( return null; } - @SuppressWarnings("unchecked") - public static BasicType resolveJsonArrayType(DomainType elementType, TypeConfiguration typeConfiguration) { - @SuppressWarnings("unchecked") final BasicPluralJavaType arrayJavaType = (BasicPluralJavaType) typeConfiguration.getJavaTypeRegistry() - .getDescriptor( - Array.newInstance( elementType.getBindableJavaType(), 0 ).getClass() - ); - final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(); - final JdbcTypeIndicators jdbcTypeIndicators = new DelegatingJdbcTypeIndicators( typeConfiguration.getCurrentBaseSqlTypeIndicators() ) { - @Override - public Integer getExplicitJdbcTypeCode() { - return SqlTypes.JSON; - } - }; + public static BasicType resolveJsonArrayType(DomainType elementType, TypeConfiguration typeConfiguration) { + final Class arrayClass = Array.newInstance( elementType.getBindableJavaType(), 0 ).getClass(); + @SuppressWarnings("unchecked") + final BasicPluralJavaType arrayJavaType = + (BasicPluralJavaType) + typeConfiguration.getJavaTypeRegistry() + .getDescriptor( arrayClass ); + final JdbcTypeIndicators currentBaseSqlTypeIndicators = typeConfiguration.getCurrentBaseSqlTypeIndicators(); return arrayJavaType.resolveType( typeConfiguration, - dialect, - (BasicType) elementType, + currentBaseSqlTypeIndicators.getDialect(), + (BasicType) elementType, null, - jdbcTypeIndicators + new DelegatingJdbcTypeIndicators( currentBaseSqlTypeIndicators ) { + @Override + public Integer getExplicitJdbcTypeCode() { + return SqlTypes.JSON; + } + } ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/DependantValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/DependantValue.java index b0c87ae49070..e1d541179421 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/DependantValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/DependantValue.java @@ -91,8 +91,8 @@ public boolean resolve(MetadataBuildingContext buildingContext) { @Override public BasicValue.Resolution resolve() { - if ( wrappedValue instanceof BasicValue ) { - return ( (BasicValue) wrappedValue ).resolve(); + if ( wrappedValue instanceof BasicValue basicValue ) { + return basicValue.resolve(); } // not sure it is ever possible throw new UnsupportedOperationException("Trying to resolve the wrapped value but it is non a BasicValue"); diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java index b2048c6581bc..6c0e4d61b0a2 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java @@ -920,7 +920,7 @@ public boolean hasPartitionedSelectionMapping() { } for ( Property property : getProperties() ) { final Value value = property.getValue(); - if ( value instanceof BasicValue && ( (BasicValue) value ).isPartitionKey() ) { + if ( value instanceof BasicValue basicValue && basicValue.isPartitionKey() ) { return true; } } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Property.java b/hibernate-core/src/main/java/org/hibernate/mapping/Property.java index 61860fd2d292..86af377bf737 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Property.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Property.java @@ -286,7 +286,7 @@ public boolean isValid(Mapping mapping) throws MappingException { public boolean isValid(MappingContext mappingContext) throws MappingException { final Value value = getValue(); - if ( value instanceof BasicValue && ( (BasicValue) value ).isDisallowedWrapperArray() ) { + if ( value instanceof BasicValue basicValue && basicValue.isDisallowedWrapperArray() ) { throw new MappingException( "The property " + persistentClass.getEntityName() + "#" + name + " uses a wrapper type Byte[]/Character[] which indicates an issue in your domain model. " + diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java index 8ba6ac23c2c3..94e06cfc73ab 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java @@ -816,7 +816,8 @@ public void copyTypeFrom( SimpleValue sourceValue ) { @Override public boolean isSame(Value other) { - return this == other || other instanceof SimpleValue && isSame( (SimpleValue) other ); + return this == other + || other instanceof SimpleValue simpleValue && isSame( simpleValue ); } protected static boolean isSame(Value v1, Value v2) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EmbeddableMappingType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EmbeddableMappingType.java index 4ae64b769f09..b3e193f7f503 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EmbeddableMappingType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EmbeddableMappingType.java @@ -164,8 +164,7 @@ default SelectableMapping getJdbcValueSelectable(int columnIndex) { } count++; } - else if ( attributeMapping instanceof ToOneAttributeMapping ) { - final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping; + else if ( attributeMapping instanceof ToOneAttributeMapping toOneAttributeMapping ) { if ( toOneAttributeMapping.getSideNature() == ForeignKeyDescriptor.Nature.KEY ) { final ValuedModelPart keyPart = toOneAttributeMapping.getForeignKeyDescriptor().getKeyPart(); if ( keyPart instanceof BasicValuedMapping ) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java index e3b625d31a56..d6af37caff8f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java @@ -1322,8 +1322,7 @@ private static CollectionPart interpretMapKey( final IndexedCollection indexedCollection = (IndexedCollection) bootValueMapping; final Value bootMapKeyDescriptor = indexedCollection.getIndex(); - if ( bootMapKeyDescriptor instanceof BasicValue ) { - final BasicValue basicValue = (BasicValue) bootMapKeyDescriptor; + if ( bootMapKeyDescriptor instanceof BasicValue basicValue ) { final boolean insertable; final boolean updatable; if ( indexedCollection instanceof org.hibernate.mapping.Map @@ -1431,8 +1430,7 @@ private static CollectionPart interpretElement( MappingModelCreationProcess creationProcess) { final Value element = bootDescriptor.getElement(); - if ( element instanceof BasicValue ) { - final BasicValue basicElement = (BasicValue) element; + if ( element instanceof BasicValue basicElement ) { final SelectableMapping selectableMapping = SelectableMappingImpl.from( tableExpression, basicElement.getSelectables().get(0), diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractIdentifiableType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractIdentifiableType.java index 5d1db57892ec..756541243140 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractIdentifiableType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AbstractIdentifiableType.java @@ -177,8 +177,8 @@ public SimpleDomainType getIdType() { if ( idClassAttributes.size() == 1 ) { return idClassAttributes.iterator().next().getType(); } - else if ( idClassType instanceof SimpleDomainType ) { - return (SimpleDomainType) idClassType; + else if ( idClassType instanceof SimpleDomainType simpleDomainType ) { + return simpleDomainType; } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingImpl.java b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingImpl.java index 5c64dff034cc..a47478db1cc8 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingImpl.java @@ -348,8 +348,8 @@ public boolean setType(MappingModelExpressible type) { bindType = (BindableType) type; return changed; } - else if ( type instanceof BasicValuedMapping ) { - final JdbcMapping jdbcMapping = ( (BasicValuedMapping) type ).getJdbcMapping(); + else if ( type instanceof BasicValuedMapping basicValuedMapping ) { + final JdbcMapping jdbcMapping = basicValuedMapping.getJdbcMapping(); if ( jdbcMapping instanceof BindableType ) { final boolean changed = bindType != null && jdbcMapping != bindType; bindType = (BindableType) jdbcMapping; diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/mutation/internal/inline/InPredicateRestrictionProducer.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/mutation/internal/inline/InPredicateRestrictionProducer.java index 5615bd25e0ca..fa10b144158d 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/mutation/internal/inline/InPredicateRestrictionProducer.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/mutation/internal/inline/InPredicateRestrictionProducer.java @@ -49,8 +49,7 @@ public class InPredicateRestrictionProducer implements MatchingIdRestrictionProd public List produceIdExpressionList(List idsAndFks, EntityMappingType entityDescriptor) { final List inListExpressions = new ArrayList<>( idsAndFks.size() ); final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping(); - if ( identifierMapping instanceof BasicValuedModelPart ) { - final BasicValuedModelPart basicValuedModelPart = (BasicValuedModelPart) identifierMapping; + if ( identifierMapping instanceof BasicValuedModelPart basicValuedModelPart ) { for ( int i = 0; i < idsAndFks.size(); i++ ) { inListExpressions.add( new QueryLiteral<>( idsAndFks.get( i ), basicValuedModelPart ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/produce/function/StandardFunctionReturnTypeResolvers.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/produce/function/StandardFunctionReturnTypeResolvers.java index 0ea8a665a840..5e184e8f3912 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/produce/function/StandardFunctionReturnTypeResolvers.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/produce/function/StandardFunctionReturnTypeResolvers.java @@ -101,17 +101,11 @@ public BasicValuedMapping resolveFunctionReturnType( Supplier impliedTypeAccess, List arguments) { for ( SqlAstNode arg: arguments ) { - if ( ! ( arg instanceof Expression ) ) { - continue; - } - - final JdbcMappingContainer nodeType = ( (Expression) arg ).getExpressionType(); - if ( nodeType instanceof BasicValuedMapping ) { - final BasicValuedMapping argType = (BasicValuedMapping) nodeType; + if ( arg instanceof Expression expression + && expression.getExpressionType() instanceof BasicValuedMapping argType ) { return useImpliedTypeIfPossible( argType, impliedTypeAccess.get() ); } } - return impliedTypeAccess.get(); } @@ -135,7 +129,6 @@ public ReturnableType resolveFunctionReturnType( } - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Internal helpers @@ -144,18 +137,14 @@ public static boolean isAssignableTo(ReturnableType defined, ReturnableType impliedType + && defined instanceof BasicType definedType + && isAssignableTo( definedType.getJdbcMapping(), impliedType.getJdbcMapping() ); } - return isAssignableTo( - ( (BasicType) defined ).getJdbcMapping(), - ( (BasicType) implied ).getJdbcMapping() - ); } @Internal @@ -173,52 +162,48 @@ public static boolean isAssignableTo(JdbcMapping defined, JdbcMapping implied) { } @Internal - public static BasicValuedMapping useImpliedTypeIfPossible( - BasicValuedMapping defined, - BasicValuedMapping implied) { + public static BasicValuedMapping useImpliedTypeIfPossible(BasicValuedMapping defined, BasicValuedMapping implied) { if ( defined == null ) { return implied; } - - if ( implied == null ) { + else if ( implied == null ) { return defined; } - - return areCompatible( defined, implied ) ? implied : defined; + else { + return areCompatible( defined, implied ) ? implied : defined; + } } - private static boolean areCompatible( - BasicValuedMapping defined, - BasicValuedMapping implied) { + private static boolean areCompatible(BasicValuedMapping defined, BasicValuedMapping implied) { if ( defined == null || implied == null) { return true; } - - if ( defined.getJdbcMapping() == null ) { + else if ( defined.getJdbcMapping() == null ) { return true; } - - if ( implied.getJdbcMapping() == null ) { + else if ( implied.getJdbcMapping() == null ) { return true; } - - //This list of cases defines legal promotions from a SQL function return - //type specified in the function template (i.e. in the Dialect) and a type - //that is determined by how the function is used in the HQL query. In essence - //the types are compatible if the map to the same JDBC type, of if they are - //both numeric types. - return isAssignableTo( defined.getJdbcMapping(), implied.getJdbcMapping() ); + else { + //This list of cases defines legal promotions from a SQL function return + //type specified in the function template (i.e. in the Dialect) and a type + //that is determined by how the function is used in the HQL query. In essence + //the types are compatible if the map to the same JDBC type, of if they are + //both numeric types. + return isAssignableTo( defined.getJdbcMapping(), implied.getJdbcMapping() ); + } } - public static ReturnableType extractArgumentType( - List> arguments, - int position) { + public static ReturnableType extractArgumentType(List> arguments, int position) { final SqmTypedNode specifiedArgument = arguments.get( position - 1 ); final SqmExpressible specifiedArgType = getArgumentExpressible( specifiedArgument ); if ( specifiedArgType == null || specifiedArgType instanceof NullSqmExpressible ) { return null; } - else if ( !(specifiedArgType instanceof ReturnableType) ) { + else if ( specifiedArgType instanceof ReturnableType returnableType ) { + return returnableType; + } + else { throw new FunctionArgumentException( String.format( Locale.ROOT, @@ -229,9 +214,6 @@ else if ( !(specifiedArgType instanceof ReturnableType) ) { ) ); } - else { - return (ReturnableType) specifiedArgType; - } } private static SqmExpressible getArgumentExpressible(SqmTypedNode specifiedArgument) { @@ -239,52 +221,53 @@ private static SqmExpressible getArgumentExpressible(SqmTypedNode specifie return expressible != null ? expressible.getSqmType() : null; } - public static JdbcMapping extractArgumentJdbcMapping( - TypeConfiguration typeConfiguration, - List> arguments, - int position) { - final SqmTypedNode specifiedArgument = arguments.get( position - 1 ); - final SqmExpressible specifiedArgType = specifiedArgument.getNodeType(); - if ( specifiedArgType instanceof BasicType ) { - return ( (BasicType) specifiedArgType ).getJdbcMapping(); - } - else { - final BasicType basicType = typeConfiguration.getBasicTypeForJavaType( - specifiedArgType.getExpressibleJavaType().getJavaTypeClass() - ); - if ( basicType == null ) { - throw new FunctionArgumentException( - String.format( - Locale.ROOT, - "Function argument [%s] of type [%s] at specified position [%d] in call arguments was not typed as basic type", - specifiedArgument, - specifiedArgType, - position - ) - ); - } - - return basicType.getJdbcMapping(); - } - } +// public static JdbcMapping extractArgumentJdbcMapping( +// TypeConfiguration typeConfiguration, +// List> arguments, +// int position) { +// final SqmTypedNode specifiedArgument = arguments.get( position - 1 ); +// final SqmExpressible specifiedArgType = specifiedArgument.getNodeType(); +// if ( specifiedArgType instanceof BasicType ) { +// return ( (BasicType) specifiedArgType ).getJdbcMapping(); +// } +// else { +// final BasicType basicType = typeConfiguration.getBasicTypeForJavaType( +// specifiedArgType.getExpressibleJavaType().getJavaTypeClass() +// ); +// if ( basicType == null ) { +// throw new FunctionArgumentException( +// String.format( +// Locale.ROOT, +// "Function argument [%s] of type [%s] at specified position [%d] in call arguments was not typed as basic type", +// specifiedArgument, +// specifiedArgType, +// position +// ) +// ); +// } +// +// return basicType.getJdbcMapping(); +// } +// } public static BasicValuedMapping extractArgumentValuedMapping(List arguments, int position) { final SqlAstNode specifiedArgument = arguments.get( position-1 ); - final JdbcMappingContainer specifiedArgType = specifiedArgument instanceof Expression - ? ( (Expression) specifiedArgument ).getExpressionType() - : null; - - if ( specifiedArgType instanceof BasicValuedMapping ) { - return (BasicValuedMapping) specifiedArgType; + final JdbcMappingContainer specifiedArgType = + specifiedArgument instanceof Expression expression + ? expression.getExpressionType() + : null; + if ( specifiedArgType instanceof BasicValuedMapping basicValuedMapping ) { + return basicValuedMapping; + } + else { + throw new FunctionArgumentException( + String.format( + Locale.ROOT, + "Function argument [%s] at specified position [%d] in call arguments was not typed as an allowable function return type", + specifiedArgument, + position + ) + ); } - - throw new FunctionArgumentException( - String.format( - Locale.ROOT, - "Function argument [%s] at specified position [%d] in call arguments was not typed as an allowable function return type", - specifiedArgument, - position - ) - ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index d744ad13421a..d36d467f9c0e 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -5327,9 +5327,10 @@ private Expression withTreatRestriction(Expression expression, SqmPath path) } final BasicValuedPathInterpretation basicPath = (BasicValuedPathInterpretation) expression; final TableGroup tableGroup = basicPath.getTableGroup(); - final TableGroup elementTableGroup = tableGroup instanceof PluralTableGroup - ? ( (PluralTableGroup) tableGroup ).getElementTableGroup() - : tableGroup; + final TableGroup elementTableGroup = + tableGroup instanceof PluralTableGroup pluralTableGroup + ? pluralTableGroup.getElementTableGroup() + : tableGroup; final EntityPersister persister = (EntityPersister) elementTableGroup.getModelPart().getPartMappingType(); // Only need a case expression around the basic valued path for the parent treat expression // if the column of the basic valued path is shared between subclasses @@ -5633,7 +5634,6 @@ public Expression visitLiteral(SqmLiteral literal) { } else { final MappingModelExpressible inferableExpressible = getInferredValueMapping(); - if ( inferableExpressible instanceof DiscriminatorMapping discriminatorMapping ) { return entityTypeLiteral( literal, discriminatorMapping ); } @@ -5744,8 +5744,9 @@ private static Expression associationLiteral(SqmLiteral literal, EntityValued final ForeignKeyDescriptor foreignKeyDescriptor = association.getForeignKeyDescriptor(); if ( association.getSideNature() == ForeignKeyDescriptor.Nature.TARGET ) { // If the association is the target, we must use the identifier of the EntityMappingType - associationKey = association.getAssociatedEntityMappingType().getIdentifierMapping() - .getIdentifier( literal.getLiteralValue() ); + associationKey = + association.getAssociatedEntityMappingType().getIdentifierMapping() + .getIdentifier( literal.getLiteralValue() ); associationKeyPart = association.getAssociatedEntityMappingType().getIdentifierMapping(); } else { @@ -5758,17 +5759,14 @@ private static Expression associationLiteral(SqmLiteral literal, EntityValued } } else { - final EntityIdentifierMapping identifierMapping = entityValuedModelPart.getEntityMappingType() - .getIdentifierMapping(); + final EntityIdentifierMapping identifierMapping = + entityValuedModelPart.getEntityMappingType().getIdentifierMapping(); associationKeyPart = identifierMapping; associationKey = identifierMapping.getIdentifier( literal.getLiteralValue() ); } - if ( associationKeyPart instanceof BasicValuedMapping ) { - return new QueryLiteral<>( - associationKey, - (BasicValuedMapping) associationKeyPart - ); + if ( associationKeyPart instanceof BasicValuedMapping basicValuedMapping ) { + return new QueryLiteral<>( associationKey, basicValuedMapping ); } else { return sqlTuple( associationKeyPart, associationKey ); @@ -6216,16 +6214,16 @@ else if ( paramType instanceof EntityDomainType ) { final SqmExpressible paramSqmType = paramType.resolveExpressible( creationContext ); - if ( paramSqmType instanceof SqmPath ) { - final MappingModelExpressible modelPart = determineValueMapping( (SqmPath) paramSqmType ); - if ( modelPart instanceof PluralAttributeMapping ) { - return resolveInferredValueMappingForParameter( ( (PluralAttributeMapping) modelPart ).getElementDescriptor() ); + if ( paramSqmType instanceof SqmPath sqmPath ) { + final MappingModelExpressible modelPart = determineValueMapping( sqmPath ); + if ( modelPart instanceof PluralAttributeMapping pluralAttributeMapping ) { + return resolveInferredValueMappingForParameter( pluralAttributeMapping.getElementDescriptor() ); } return modelPart; } - if ( paramSqmType instanceof BasicValuedMapping ) { - return (BasicValuedMapping) paramSqmType; + if ( paramSqmType instanceof BasicValuedMapping basicValuedMapping ) { + return basicValuedMapping; } if ( paramSqmType instanceof CompositeSqmPathSource || paramSqmType instanceof EmbeddableDomainType ) { @@ -7383,17 +7381,17 @@ public Expression visitParameterizedEntityTypeExpression(SqmParameterizedEntityT public Object visitEnumLiteral(SqmEnumLiteral sqmEnumLiteral) { final MappingModelExpressible inferred = resolveInferredType(); final SqlExpressible inferredType; - if ( inferred instanceof PluralAttributeMapping ) { - final CollectionPart elementDescriptor = ((PluralAttributeMapping) inferred).getElementDescriptor(); - if ( elementDescriptor instanceof BasicValuedCollectionPart) { - inferredType = (BasicValuedCollectionPart) elementDescriptor; + if ( inferred instanceof PluralAttributeMapping pluralAttributeMapping ) { + final CollectionPart elementDescriptor = pluralAttributeMapping.getElementDescriptor(); + if ( elementDescriptor instanceof BasicValuedCollectionPart basicValuedCollectionPart ) { + inferredType = basicValuedCollectionPart; } else { inferredType = null; } } - else if ( inferred instanceof BasicValuedMapping ) { - inferredType = (BasicValuedMapping) inferred; + else if ( inferred instanceof BasicValuedMapping basicValuedMapping ) { + inferredType = basicValuedMapping; } else { inferredType = null; diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java index 6aee9a84ee15..8d1701444d44 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java @@ -1238,16 +1238,15 @@ protected void visitSetAssignmentEmulateJoin(Assignment assignment, UpdateStatem } protected boolean isStruct(JdbcMappingContainer expressionType) { - if ( expressionType instanceof EmbeddableValuedModelPart ) { - final EmbeddableMappingType embeddableMappingType = ( (EmbeddableValuedModelPart) expressionType ).getEmbeddableTypeDescriptor(); + if ( expressionType instanceof EmbeddableValuedModelPart embeddableValuedModelPart ) { + final EmbeddableMappingType embeddableMappingType = embeddableValuedModelPart.getEmbeddableTypeDescriptor(); return embeddableMappingType.getAggregateMapping() != null && embeddableMappingType.getAggregateMapping() .getJdbcMapping() .getJdbcType() .getDefaultSqlTypeCode() == SqlTypes.STRUCT; } - else if ( expressionType instanceof BasicValuedMapping ) { - final JdbcMapping jdbcMapping = ( (BasicValuedMapping) expressionType ).getJdbcMapping(); - return jdbcMapping.getJdbcType().getDefaultSqlTypeCode() == SqlTypes.STRUCT; + else if ( expressionType instanceof BasicValuedMapping basicValuedMapping ) { + return basicValuedMapping.getJdbcMapping().getJdbcType().getDefaultSqlTypeCode() == SqlTypes.STRUCT; } return false; } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/cte/CteTable.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/cte/CteTable.java index f6650ba1a937..97ac85051c7f 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/cte/CteTable.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/cte/CteTable.java @@ -71,13 +71,10 @@ public static CteTable createIdTable(String cteName, EntityMappingType entityDes final int numberOfColumns = entityDescriptor.getIdentifierMapping().getJdbcTypeCount(); final List columns = new ArrayList<>( numberOfColumns ); final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping(); - final String idName; - if ( identifierMapping instanceof SingleAttributeIdentifierMapping ) { - idName = ( (SingleAttributeIdentifierMapping) identifierMapping ).getAttributeName(); - } - else { - idName = "id"; - } + final String idName = + identifierMapping instanceof SingleAttributeIdentifierMapping + ? identifierMapping.getAttributeName() + : "id"; forEachCteColumn( idName, identifierMapping, columns::add ); return new CteTable( cteName, columns ); } @@ -122,14 +119,12 @@ public static CteTable createEntityTable(String cteName, EntityMappingType entit } public static void forEachCteColumn(String prefix, ModelPart modelPart, Consumer consumer) { - if ( modelPart instanceof BasicValuedMapping ) { - consumer.accept( new CteColumn( prefix, ( (BasicValuedMapping) modelPart ).getJdbcMapping() ) ); + if ( modelPart instanceof BasicValuedMapping basicValuedMapping ) { + consumer.accept( new CteColumn( prefix, basicValuedMapping.getJdbcMapping() ) ); } - else if ( modelPart instanceof EntityValuedModelPart ) { - final EntityValuedModelPart entityPart = ( EntityValuedModelPart ) modelPart; + else if ( modelPart instanceof EntityValuedModelPart entityPart ) { final ModelPart targetPart; - if ( modelPart instanceof Association ) { - final Association association = (Association) modelPart; + if ( modelPart instanceof Association association ) { if ( association.getForeignKeyDescriptor() == null ) { // This is expected to happen when processing a // PostInitCallbackEntry because the callbacks @@ -149,8 +144,7 @@ else if ( modelPart instanceof EntityValuedModelPart ) { } forEachCteColumn( prefix + "_" + entityPart.getPartName(), targetPart, consumer ); } - else if ( modelPart instanceof DiscriminatedAssociationModelPart ) { - final DiscriminatedAssociationModelPart discriminatedPart = (DiscriminatedAssociationModelPart) modelPart; + else if ( modelPart instanceof DiscriminatedAssociationModelPart discriminatedPart ) { final String newPrefix = prefix + "_" + discriminatedPart.getPartName() + "_"; forEachCteColumn( newPrefix + "discriminator", @@ -217,8 +211,7 @@ private static int determineModelPartStartIndex(int offset, ModelPart modelPart, } return determineModelPartStartIndex( offset, keyPart, modelPartToFind ); } - else if ( modelPart instanceof EmbeddableValuedModelPart ) { - final EmbeddableValuedModelPart embeddablePart = ( EmbeddableValuedModelPart ) modelPart; + else if ( modelPart instanceof EmbeddableValuedModelPart embeddablePart ) { final AttributeMappingsList attributeMappings = embeddablePart.getEmbeddableTypeDescriptor().getAttributeMappings(); for ( int i = 0; i < attributeMappings.size(); i++ ) { final AttributeMapping mapping = attributeMappings.get( i ); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcParameterBindingsImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcParameterBindingsImpl.java index 2d5d3a4e3e66..9d4e645636b3 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcParameterBindingsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcParameterBindingsImpl.java @@ -69,8 +69,8 @@ else if ( type instanceof BasicTypeReference ) { .getBasicTypeRegistry() .resolve( ( (BasicTypeReference) type ) ); } - else if ( type instanceof BasicValuedMapping ) { - jdbcMapping = ( (BasicValuedMapping) type ).getJdbcMapping(); + else if ( type instanceof BasicValuedMapping basicValuedMapping ) { + jdbcMapping = basicValuedMapping.getJdbcMapping(); } else { throw new IllegalArgumentException( "Could not resolve NativeQuery parameter type : `" + param + "`"); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcParameterBindings.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcParameterBindings.java index cf391fc5da6a..b318804681b9 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcParameterBindings.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcParameterBindings.java @@ -73,15 +73,10 @@ default int registerParametersForEachJdbcValue( Bindable bindable, JdbcParametersList jdbcParameters, SharedSessionContractImplementor session) { - final Object valueToBind; - if ( bindable instanceof BasicValuedMapping ) { - valueToBind = ( (BasicValuedMapping) bindable ).getJdbcMapping().getMappedJavaType().wrap( value, session ); - } - else { - valueToBind = value; - } return bindable.forEachJdbcValue( - valueToBind, + bindable instanceof BasicValuedMapping basicValuedMapping + ? basicValuedMapping.getJdbcMapping().getMappedJavaType().wrap( value, session ) + : value, offset, jdbcParameters, session.getFactory().getTypeConfiguration(), diff --git a/hibernate-core/src/main/java/org/hibernate/type/SerializableToBlobType.java b/hibernate-core/src/main/java/org/hibernate/type/SerializableToBlobType.java deleted file mode 100644 index a7728a762b17..000000000000 --- a/hibernate-core/src/main/java/org/hibernate/type/SerializableToBlobType.java +++ /dev/null @@ -1,382 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.type; - -import java.io.Serializable; -import java.sql.CallableStatement; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.Map; -import java.util.Properties; - -import org.hibernate.Hibernate; -import org.hibernate.HibernateException; -import org.hibernate.MappingException; -import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer; -import org.hibernate.engine.jdbc.Size; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.internal.util.ReflectHelper; -import org.hibernate.internal.util.collections.ArrayHelper; -import org.hibernate.type.descriptor.ValueBinder; -import org.hibernate.type.descriptor.ValueExtractor; -import org.hibernate.type.descriptor.WrapperOptions; -import org.hibernate.type.descriptor.java.JavaType; -import org.hibernate.type.descriptor.java.MutabilityPlan; -import org.hibernate.type.descriptor.java.SerializableJavaType; -import org.hibernate.type.descriptor.jdbc.BlobJdbcType; -import org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter; -import org.hibernate.type.descriptor.jdbc.JdbcType; -import org.hibernate.usertype.DynamicParameterizedType; - -/** - * @author Brett Meyer - */ -public class SerializableToBlobType implements BasicType, - ProcedureParameterExtractionAware, - ProcedureParameterNamedBinder, - DynamicParameterizedType { - - public static final String CLASS_NAME = "classname"; - - private static final long serialVersionUID = 1L; - private final Size dictatedSize = new Size(); - - // Don't use final here. Need to initialize after-the-fact - // by DynamicParameterizedTypes. - private final JdbcType jdbcType; - private JavaType javaType; - // sqlTypes need always to be in sync with sqlTypeDescriptor - private final int[] sqlTypes; - - private ValueBinder jdbcValueBinder; - private ValueExtractor jdbcValueExtractor; - private JdbcLiteralFormatter jdbcLiteralFormatter; - - public SerializableToBlobType() { - this.jdbcType = BlobJdbcType.DEFAULT; - this.sqlTypes = new int[] { jdbcType.getDdlTypeCode() }; - this.javaType = new SerializableJavaType( Serializable.class ); - - this.jdbcValueBinder = jdbcType.getBinder( javaType ); - this.jdbcValueExtractor = jdbcType.getExtractor( javaType ); - this.jdbcLiteralFormatter = jdbcType.getJdbcLiteralFormatter( javaType ); - } - - @Override - public void setParameterValues(Properties parameters) { - ParameterType reader = (ParameterType) parameters.get( PARAMETER_TYPE ); - if ( reader != null ) { - @SuppressWarnings("unchecked") - Class returnedClass = (Class) reader.getReturnedClass(); - setJavaTypeDescriptor( new SerializableJavaType<>(returnedClass) ); - } - else { - String className = parameters.getProperty( CLASS_NAME ); - if ( className == null ) { - throw new MappingException( "No class name defined for type: " + SerializableToBlobType.class.getName() ); - } - try { - @SuppressWarnings("unchecked") - Class classForName = (Class) ReflectHelper.classForName(className); - setJavaTypeDescriptor( new SerializableJavaType<>(classForName) ); - } - catch ( ClassNotFoundException e ) { - throw new MappingException( "Unable to load class from " + CLASS_NAME + " parameter", e ); - } - } - } - - @Override - public String getName() { - return getClass().getName(); - } - - @Override - public ValueExtractor getJdbcValueExtractor() { - return jdbcValueExtractor; - } - - @Override - public ValueBinder getJdbcValueBinder() { - return jdbcValueBinder; - } - - @Override - public JdbcLiteralFormatter getJdbcLiteralFormatter() { - return jdbcLiteralFormatter; - } - - @Override - public Class getJavaType() { - return this.getExpressibleJavaType().getJavaTypeClass(); - } - - public T fromString(CharSequence string) { - return javaType.fromString( string ); - } - - protected MutabilityPlan getMutabilityPlan() { - return javaType.getMutabilityPlan(); - } - - @Override - public boolean[] toColumnNullness(Object value, MappingContext mapping) { - return value == null ? ArrayHelper.FALSE : ArrayHelper.TRUE; - } - - @Override - public String[] getRegistrationKeys() { - return registerUnderJavaType() - ? new String[] { getName(), javaType.getTypeName() } - : new String[] { getName() }; - } - - protected boolean registerUnderJavaType() { - return false; - } - - protected Size getDictatedSize() { - return dictatedSize; - } - - // final implementations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - public final JavaType getJavaTypeDescriptor() { - return javaType; - } - - @Deprecated - public final void setJavaTypeDescriptor( JavaType javaType ) { - this.javaType = javaType; - this.jdbcValueBinder = getJdbcType().getBinder( javaType ); - this.jdbcValueExtractor = getJdbcType().getExtractor( javaType ); - this.jdbcLiteralFormatter = getJdbcType().getJdbcLiteralFormatter( javaType ); - } - - public final JdbcType getJdbcType() { - return jdbcType; - } - - @Override - public final Class getReturnedClass() { - return javaType.getJavaTypeClass(); - } - - @Override - public final int getColumnSpan(MappingContext mapping) throws MappingException { - return 1; - } - - @Override - public final int[] getSqlTypeCodes(MappingContext mappingContext) throws MappingException { - return sqlTypes; - } - - @Override - public final boolean isAssociationType() { - return false; - } - - @Override - public final boolean isCollectionType() { - return false; - } - - @Override - public final boolean isComponentType() { - return false; - } - - @Override - public final boolean isEntityType() { - return false; - } - - @Override - public final boolean isAnyType() { - return false; - } - - @Override - public final boolean isSame(Object x, Object y) { - return isEqual( x, y ); - } - - @Override - public final boolean isEqual(Object x, Object y, SessionFactoryImplementor factory) { - return isEqual( x, y ); - } - - @Override - @SuppressWarnings("unchecked") - public boolean isEqual(Object one, Object another) { - return javaType.areEqual( (T) one, (T) another ); - } - - @Override - @SuppressWarnings("unchecked") - public final int getHashCode(Object x) { - return javaType.extractHashCode( (T) x ); - } - - @Override - public final int getHashCode(Object x, SessionFactoryImplementor factory) { - return getHashCode( x ); - } - - @Override - @SuppressWarnings("unchecked") - public final int compare(Object x, Object y) { - return javaType.getComparator().compare( (T) x, (T) y ); - } - - @Override - public final boolean isDirty(Object old, Object current, SharedSessionContractImplementor session) { - return isDirty( old, current ); - } - - @Override - public final boolean isDirty(Object old, Object current, boolean[] checkable, SharedSessionContractImplementor session) { - return checkable[0] && isDirty( old, current ); - } - - protected final boolean isDirty(Object old, Object current) { - return !isSame( old, current ); - } - - @Override - public final boolean isModified( - Object oldHydratedState, - Object currentState, - boolean[] checkable, - SharedSessionContractImplementor session) { - return isDirty( oldHydratedState, currentState ); - } - - @Override - public final void nullSafeSet( - PreparedStatement st, - Object value, - int index, - final SharedSessionContractImplementor session) throws SQLException { - //noinspection unchecked - nullSafeSet( st, (T) value, index, (WrapperOptions) session ); - } - - protected void nullSafeSet(PreparedStatement st, T value, int index, WrapperOptions options) throws SQLException { - jdbcType.getBinder( javaType ).bind( st, value, index, options ); - } - - @Override - @SuppressWarnings("unchecked") - public final String toLoggableString(Object value, SessionFactoryImplementor factory) { - if ( value == LazyPropertyInitializer.UNFETCHED_PROPERTY || !Hibernate.isInitialized( value ) ) { - return ""; - } - return javaType.extractLoggableRepresentation( (T) value ); - } - - @Override - public final boolean isMutable() { - return getMutabilityPlan().isMutable(); - } - - @Override - @SuppressWarnings("unchecked") - public final Object deepCopy(Object value, SessionFactoryImplementor factory) { - return deepCopy( (T) value ); - } - - protected final T deepCopy(T value) { - return getMutabilityPlan().deepCopy( value ); - } - - @Override - @SuppressWarnings("unchecked") - public final Serializable disassemble(Object value, SharedSessionContractImplementor session, Object owner) throws HibernateException { - return getMutabilityPlan().disassemble( (T) value, session ); - } - - @Override - public final Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException { - return getMutabilityPlan().assemble( cached, session ); - } - - @Override - public final void beforeAssemble(Serializable cached, SharedSessionContractImplementor session) { - } - - @Override - @SuppressWarnings("unchecked") - public final Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map copyCache) { - if ( original == null && target == null ) { - return null; - } - - return javaType.getReplacement( (T) original, (T) target, session ); - } - - @Override -// @SuppressWarnings("unchecked") - public Object replace( - Object original, - Object target, - SharedSessionContractImplementor session, - Object owner, - Map copyCache, - ForeignKeyDirection foreignKeyDirection) { - return ForeignKeyDirection.FROM_PARENT == foreignKeyDirection - ? javaType.getReplacement( (T) original, (T) target, session ) - : target; - } - - @Override - public boolean canDoExtraction() { - return true; - } - - @Override - public T extract(CallableStatement statement, int startIndex, final SharedSessionContractImplementor session) throws SQLException { - return jdbcType.getExtractor( javaType ).extract( - statement, - startIndex, - session - ); - } - - @Override - public T extract(CallableStatement statement, String paramName, final SharedSessionContractImplementor session) throws SQLException { - return jdbcType.getExtractor( javaType ).extract( - statement, - paramName, - session - ); - } - - @Override - public final void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SharedSessionContractImplementor session) - throws HibernateException, SQLException { - if ( settable[0] ) { - nullSafeSet( st, value, index, session ); - } - } - - @Override - public void nullSafeSet(CallableStatement st, T value, String name, SharedSessionContractImplementor session) throws SQLException { - nullSafeSet( st, value, name, (WrapperOptions) session ); - } - - @SuppressWarnings("unchecked") - protected final void nullSafeSet(CallableStatement st, Object value, String name, WrapperOptions options) throws SQLException { - jdbcType.getBinder( javaType ).bind( st, (T) value, name, options ); - } - - @Override - public boolean canDoSetting() { - return true; - } - -} diff --git a/hibernate-core/src/main/java/org/hibernate/usertype/DynamicParameterizedType.java b/hibernate-core/src/main/java/org/hibernate/usertype/DynamicParameterizedType.java index 0ac19ecf9059..cbf83b4e7353 100644 --- a/hibernate-core/src/main/java/org/hibernate/usertype/DynamicParameterizedType.java +++ b/hibernate-core/src/main/java/org/hibernate/usertype/DynamicParameterizedType.java @@ -18,7 +18,12 @@ * * @author Janario Oliveira * @author Yanming Zhou + * + * @deprecated This very old approach was never properly implemented in all + * contexts, and never actually achieved the type safety it aimed for. Just + * use {@link ParameterizedType} for now. */ +@Deprecated(since = "7.0", forRemoval = true) public interface DynamicParameterizedType extends ParameterizedType { String PARAMETER_TYPE = "org.hibernate.type.ParameterType"; diff --git a/hibernate-core/src/main/java/org/hibernate/type/EnumType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/EnumType.java similarity index 96% rename from hibernate-core/src/main/java/org/hibernate/type/EnumType.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/EnumType.java index 3a3995b4bc9a..04035c037ddf 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/EnumType.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/EnumType.java @@ -2,7 +2,7 @@ * SPDX-License-Identifier: LGPL-2.1-or-later * Copyright Red Hat Inc. and Hibernate Authors */ -package org.hibernate.type; +package org.hibernate.orm.test; import java.io.Serializable; import java.lang.annotation.Annotation; @@ -18,8 +18,8 @@ import org.hibernate.annotations.Nationalized; import org.hibernate.dialect.Dialect; import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.internal.CoreLogging; import org.hibernate.internal.util.ReflectHelper; +import org.hibernate.type.SqlTypes; import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.java.EnumJavaType; import org.hibernate.type.descriptor.java.JavaType; @@ -31,8 +31,6 @@ import org.hibernate.usertype.EnhancedUserType; import org.hibernate.usertype.LoggableUserType; -import org.jboss.logging.Logger; - import static jakarta.persistence.EnumType.ORDINAL; import static jakarta.persistence.EnumType.STRING; import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean; @@ -49,7 +47,6 @@ @Deprecated(since="6.2", forRemoval=true) public class EnumType> implements EnhancedUserType, DynamicParameterizedType, LoggableUserType, TypeConfigurationAware, Serializable { - private static final Logger LOG = CoreLogging.logger( EnumType.class ); public static final String ENUM = "enumClass"; public static final String NAMED = "useNamed"; @@ -156,14 +153,6 @@ else if ( reader != null ) { jdbcType = descriptor.getRecommendedJdbcType( indicators ); isOrdinal = indicators.getEnumeratedType() != STRING; } - - if ( LOG.isDebugEnabled() ) { - LOG.debugf( - "Using %s-based conversion for Enum %s", - isOrdinal() ? "ORDINAL" : "NAMED", - enumClass.getName() - ); - } } private jakarta.persistence.EnumType getEnumType(ParameterType reader) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/FirstLetterConverter.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/FirstLetterConverter.java deleted file mode 100644 index fdc532e47db8..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/FirstLetterConverter.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.enumerated.custom_types; - -import org.hibernate.orm.test.annotations.enumerated.enums.FirstLetter; - -import jakarta.persistence.AttributeConverter; - -/** - * @author Steve Ebersole - */ -public class FirstLetterConverter implements AttributeConverter { - @Override - public String convertToDatabaseColumn(FirstLetter value) { - if ( value == null ) { - return null; - } - - final String name = value.name(); - return name.substring( 0, 1 ); - } - - @Override - public FirstLetter convertToEntityAttribute(String value) { - if ( value == null ) { - return null; - } - return Enum.valueOf( FirstLetter.class, value + "_LETTER" ); - - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/FirstLetterType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/FirstLetterType.java deleted file mode 100644 index 9d4108450305..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/FirstLetterType.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.enumerated.custom_types; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; - -import org.hibernate.orm.test.annotations.enumerated.enums.FirstLetter; -import org.hibernate.type.descriptor.WrapperOptions; - -/** - * @author Janario Oliveira - */ -public class FirstLetterType extends org.hibernate.type.EnumType { - - @Override - public int getSqlType() { - return Types.VARCHAR; - } - - @Override - public FirstLetter nullSafeGet(ResultSet rs, int position, WrapperOptions options) - throws SQLException { - String persistValue = (String) rs.getObject( position ); - if ( rs.wasNull() ) { - return null; - } - return Enum.valueOf( returnedClass(), persistValue + "_LETTER" ); - } - - @Override - public void nullSafeSet(PreparedStatement st, FirstLetter value, int index, WrapperOptions options) - throws SQLException { - if ( value == null ) { - st.setNull( index, getSqlType() ); - } - else { - String enumString = value.name(); - // Using setString here, rather than setObject. A few JDBC drivers - // (Oracle, DB2, and SQLServer) were having trouble converting - // the char to VARCHAR. - st.setString( index, enumString.substring( 0, 1 ) ); - } - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/LastLetterConverter.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/LastLetterConverter.java deleted file mode 100644 index aa137b5d2e1d..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/LastLetterConverter.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.enumerated.custom_types; - -import org.hibernate.orm.test.annotations.enumerated.enums.FirstLetter; - -import jakarta.persistence.AttributeConverter; - -/** - * @author Steve Ebersole - */ -public class LastLetterConverter implements AttributeConverter { - @Override - public String convertToDatabaseColumn(FirstLetter value) { - if ( value == null ) { - return null; - } - - final String name = value.name(); - return name.substring( 0, 1 ); - } - - @Override - public FirstLetter convertToEntityAttribute(String value) { - if ( value == null ) { - return null; - } - return Enum.valueOf( FirstLetter.class, value + "_LETTER" ); - - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/LastNumberType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/LastNumberType.java deleted file mode 100644 index b0c9b3d0689d..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/custom_types/LastNumberType.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.enumerated.custom_types; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; - -import org.hibernate.orm.test.annotations.enumerated.enums.LastNumber; -import org.hibernate.type.descriptor.WrapperOptions; - -/** - * @author Janario Oliveira - */ -public class LastNumberType extends org.hibernate.type.EnumType { - - @Override - public int getSqlType() { - return Types.VARCHAR; - } - - @Override - public LastNumber nullSafeGet(ResultSet rs, int position, WrapperOptions options) - throws SQLException { - String persistValue = (String) rs.getObject( position ); - if ( rs.wasNull() ) { - return null; - } - return Enum.valueOf( returnedClass(), "NUMBER_" + persistValue ); - } - - @Override - public void nullSafeSet(PreparedStatement st, LastNumber value, int index, WrapperOptions options) - throws SQLException { - if ( value == null ) { - st.setNull( index, getSqlType() ); - } - else { - String enumString = value.name(); - // Using setString here, rather than setObject. A few JDBC drivers - // (Oracle, DB2, and SQLServer) were having trouble converting - // the char to VARCHAR. - st.setString( index, enumString.substring( enumString.length() - 1 ) ); - } - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/Common.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/Common.java deleted file mode 100644 index f835472c4c93..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/Common.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.enumerated.enums; - -/** - * @author Janario Oliveira - * @author Brett Meyer - */ -public enum Common { - A1, A2, B1, B2 -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/FirstLetter.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/FirstLetter.java deleted file mode 100644 index b4590d376abb..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/FirstLetter.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.enumerated.enums; - -/** - * @author Janario Oliveira - * @author Brett Meyer - */ -public enum FirstLetter { - A_LETTER, B_LETTER, C_LETTER -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/LastNumber.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/LastNumber.java deleted file mode 100644 index fece5bc4f063..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/LastNumber.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.enumerated.enums; - -/** - * @author Janario Oliveira - * @author Brett Meyer - */ -public enum LastNumber { - NUMBER_1, NUMBER_2, NUMBER_3 -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/Trimmed.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/Trimmed.java deleted file mode 100644 index 9599059065b4..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/enums/Trimmed.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.enumerated.enums; - -/** - * @author Janario Oliveira - * @author Brett Meyer - */ -public enum Trimmed { - A, B, C -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/lob/ExplicitSerializableType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/lob/ExplicitSerializableType.java deleted file mode 100644 index 5b186100bdb8..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/lob/ExplicitSerializableType.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.lob; - -import java.io.Serializable; - -import org.hibernate.type.SerializableToBlobType; - -/** - * @author Janario Oliveira - */ -public class ExplicitSerializableType extends SerializableToBlobType { - - // TODO: Find another way to test that this type is being used by - // SerializableToBlobTypeTest#testPersist. Most AbstractStandardBasicType - // methods are final. - -// @Override -// public Object get(ResultSet rs, String name) throws SQLException { -// CommonSerializable deserialize = (CommonSerializable) super.get( rs, name ); -// deserialize.setDefaultValue( "EXPLICIT" ); -// return deserialize; -// } -// -// @Override -// public void set(PreparedStatement st, Object value, int index, SessionImplementor session) throws SQLException { -// if ( value != null ) { -// ( (CommonSerializable) value ).setDefaultValue( null ); -// } -// super.set( st, value, index, session ); -// } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/lob/ImplicitSerializableType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/lob/ImplicitSerializableType.java deleted file mode 100644 index c1b5cb85a2f6..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/lob/ImplicitSerializableType.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.orm.test.annotations.lob; - -import java.io.Serializable; - -import org.hibernate.type.SerializableToBlobType; - -/** - * @author Janario Oliveira - */ -public class ImplicitSerializableType extends SerializableToBlobType { - - // TODO: Find another way to test that this type is being used by - // SerializableToBlobTypeTest#testPersist. Most AbstractStandardBasicType - // methods are final. - -// @Override -// public Object get(ResultSet rs, String name) throws SQLException { -// CommonSerializable deserialize = (CommonSerializable) super.get( rs, name ); -// deserialize.setDefaultValue( "IMPLICIT" ); -// return deserialize; -// } -// -// @Override -// public void set(PreparedStatement st, Object value, int index, SessionImplementor session) throws SQLException { -// if ( value != null ) { -// ( (CommonSerializable) value ).setDefaultValue( null ); -// } -// super.set( st, value, index, session ); -// } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/cdi/lifecycle/ExtendedBeanManagerNotAvailableDuringTypeResolutionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/cdi/lifecycle/ExtendedBeanManagerNotAvailableDuringTypeResolutionTest.java index 5f3a61b4f375..804b932bbad1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/cdi/lifecycle/ExtendedBeanManagerNotAvailableDuringTypeResolutionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/cdi/lifecycle/ExtendedBeanManagerNotAvailableDuringTypeResolutionTest.java @@ -64,7 +64,7 @@ public static class TheEntity { " \n" + " \n" + " \n" + - " \n" + + " \n" + " " + MyEnum.class.getName() + "\n" + " \n" + " \n" + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/typedef/NamedEnumUserType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/typedef/NamedEnumUserType.java index 9bf88aaef0a7..0caef67ae062 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/typedef/NamedEnumUserType.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/typedef/NamedEnumUserType.java @@ -6,7 +6,7 @@ import java.util.Properties; -import org.hibernate.type.EnumType; +import org.hibernate.orm.test.EnumType; /** * A simple user type where we force enums to be saved by name, not ordinal diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/mapping/converted/enums/Person.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/mapping/converted/enums/Person.hbm.xml index 846f11f99237..0a020c86b829 100644 --- a/hibernate-core/src/test/resources/org/hibernate/orm/test/mapping/converted/enums/Person.hbm.xml +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/mapping/converted/enums/Person.hbm.xml @@ -16,20 +16,20 @@ - + org.hibernate.orm.test.mapping.converted.enums.Gender 12 - + org.hibernate.orm.test.mapping.converted.enums.HairColor - + org.hibernate.orm.test.mapping.converted.enums.HairColor 12 diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/mapping/converted/enums/mappings.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/mapping/converted/enums/mappings.hbm.xml index 8342032d8aa5..54c7917f1aa0 100644 --- a/hibernate-core/src/test/resources/org/hibernate/orm/test/mapping/converted/enums/mappings.hbm.xml +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/mapping/converted/enums/mappings.hbm.xml @@ -12,13 +12,13 @@ - + org.hibernate.orm.test.mapping.converted.enums.UnspecifiedEnumTypeEntity$E1 - + org.hibernate.orm.test.mapping.converted.enums.UnspecifiedEnumTypeEntity$E2 diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/quote/DataPoint.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/quote/DataPoint.hbm.xml index 3ef22085a939..c9b0bdabae57 100644 --- a/hibernate-core/src/test/resources/org/hibernate/orm/test/quote/DataPoint.hbm.xml +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/quote/DataPoint.hbm.xml @@ -11,7 +11,7 @@ "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > - + org.hibernate.orm.test.quote.DataPoint$DataPointEnum diff --git a/hibernate-core/src/test/resources/org/hibernate/query/hhh12076/Settlement.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/query/hhh12076/Settlement.hbm.xml index d1a677c3637b..5e81de5a5976 100644 --- a/hibernate-core/src/test/resources/org/hibernate/query/hhh12076/Settlement.hbm.xml +++ b/hibernate-core/src/test/resources/org/hibernate/query/hhh12076/Settlement.hbm.xml @@ -36,7 +36,7 @@ - + 12 org.hibernate.orm.test.query.hhh12076.SettlementStatus diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/BasicMetadataGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/BasicMetadataGenerator.java index 6564da8a37bd..e9327004d6b8 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/BasicMetadataGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/BasicMetadataGenerator.java @@ -14,8 +14,6 @@ import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.Value; import org.hibernate.type.BasicType; -import org.hibernate.type.CustomType; -import org.hibernate.type.EnumType; import org.hibernate.type.Type; /** @@ -58,24 +56,9 @@ public boolean addBasic( return false; } - private void mapEnumerationType(TypeSpecification typeDefinition, EnumType type, Properties parameters) { - if ( parameters.getProperty( EnumType.ENUM ) != null ) { - typeDefinition.setParameter( EnumType.ENUM, parameters.getProperty( EnumType.ENUM ) ); - } - else { - typeDefinition.setParameter( EnumType.ENUM, type.getEnumClass().getName() ); - } - if ( parameters.getProperty( EnumType.NAMED ) != null ) { - typeDefinition.setParameter( EnumType.NAMED, parameters.getProperty( EnumType.NAMED ) ); - } - else { - typeDefinition.setParameter( EnumType.NAMED, Boolean.toString( !type.isOrdinal() ) ); - } - } - private boolean isAddNestedType(Value value) { - if ( value instanceof SimpleValue ) { - return ((SimpleValue) value).getTypeParameters() != null; + if ( value instanceof SimpleValue simpleValue ) { + return simpleValue.getTypeParameters() != null; } return false; } @@ -99,19 +82,12 @@ private void applyNestedType(SimpleValue value, BasicAttribute attribute) { final TypeSpecification typeSpecification = new TypeSpecification( typeName ); attribute.setType( typeSpecification ); - Type type = value.getType(); - if ( type instanceof CustomType && ((CustomType) type).getUserType() instanceof EnumType ) { - // Proper handling of nasty legacy EnumType - mapEnumerationType( typeSpecification, (EnumType) ((CustomType) type).getUserType(), typeParameters ); - } - else { - // By default, copying all Hibernate properties - for ( Object object : typeParameters.keySet() ) { - final String keyType = (String) object; - final String property = typeParameters.getProperty( keyType ); - if ( property != null ) { - typeSpecification.setParameter( keyType, property ); - } + // By default, copying all Hibernate properties + for ( Object object : typeParameters.keySet() ) { + final String keyType = (String) object; + final String property = typeParameters.getProperty( keyType ); + if ( property != null ) { + typeSpecification.setParameter( keyType, property ); } } } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/EnumType.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/EnumType.java new file mode 100644 index 000000000000..e377337e42bc --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/EnumType.java @@ -0,0 +1,333 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.envers.test.integration.customtype; + +import jakarta.persistence.Enumerated; +import jakarta.persistence.MapKeyEnumerated; +import org.hibernate.AssertionFailure; +import org.hibernate.HibernateException; +import org.hibernate.annotations.Nationalized; +import org.hibernate.dialect.Dialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.internal.util.ReflectHelper; +import org.hibernate.type.SqlTypes; +import org.hibernate.type.descriptor.WrapperOptions; +import org.hibernate.type.descriptor.java.EnumJavaType; +import org.hibernate.type.descriptor.java.JavaType; +import org.hibernate.type.descriptor.jdbc.JdbcType; +import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; +import org.hibernate.type.spi.TypeConfiguration; +import org.hibernate.type.spi.TypeConfigurationAware; +import org.hibernate.usertype.DynamicParameterizedType; +import org.hibernate.usertype.EnhancedUserType; +import org.hibernate.usertype.LoggableUserType; + +import java.io.Serializable; +import java.lang.annotation.Annotation; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import static jakarta.persistence.EnumType.ORDINAL; +import static jakarta.persistence.EnumType.STRING; +import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean; + +/** + * Value type mapper for enumerations. + * + * @author Emmanuel Bernard + * @author Hardy Ferentschik + * @author Steve Ebersole + * + * @deprecated Use the built-in support for enums + */ +@Deprecated(since="6.2", forRemoval=true) +public class EnumType> + implements EnhancedUserType, DynamicParameterizedType, LoggableUserType, TypeConfigurationAware, Serializable { + + public static final String ENUM = "enumClass"; + public static final String NAMED = "useNamed"; + public static final String TYPE = "type"; + + private Class enumClass; + + private boolean isOrdinal; + private JdbcType jdbcType; + private EnumJavaType enumJavaType; + + private TypeConfiguration typeConfiguration; + + public EnumType() { + } + + public Class getEnumClass() { + return enumClass; + } + + @Override + public JdbcType getJdbcType(TypeConfiguration typeConfiguration) { + return jdbcType; + } + + /** + *

+ * An instance of this class is "configured" by a call to {@link #setParameterValues}, + * where configuration parameters are given as entries in a {@link Properties} object. + * There are two distinct ways an instance may be configured: + *

    + *
  • one for {@code hbm.xml}-based mapping, and + *
  • another for annotation-based or {@code orm.xml}-based mapping. + *
+ *

+ * In the case of annotations or {@code orm.xml}, a {@link ParameterType} is passed to + * {@link #setParameterValues} under the key {@value #PARAMETER_TYPE}. + *

+ * But in the case of {@code hbm.xml}, there are multiple parameters: + *

    + *
  • + * {@value #ENUM}, the name of the Java enumeration class. + *
  • + *
  • + * {@value #NAMED}, specifies if the enum should be mapped by name. + * Default is to map as ordinal. + *
  • + *
  • + * {@value #TYPE}, a JDBC type code (legacy alternative to {@value #NAMED}). + *
  • + *
+ */ + @Override + public void setParameterValues(Properties parameters) { + // IMPL NOTE: we handle 2 distinct cases here: + // 1) we are passed a ParameterType instance in the incoming Properties - generally + // speaking this indicates the annotation-binding case, and the passed ParameterType + // represents information about the attribute and annotation + // 2) we are not passed a ParameterType - generally this indicates a hbm.xml binding case. + final ParameterType reader = (ParameterType) parameters.get( PARAMETER_TYPE ); + + if ( parameters.containsKey( ENUM ) ) { + final String enumClassName = (String) parameters.get( ENUM ); + try { + enumClass = (Class) ReflectHelper.classForName( enumClassName, this.getClass() ).asSubclass( Enum.class ); + } + catch ( ClassNotFoundException exception ) { + throw new HibernateException("Enum class not found: " + enumClassName, exception); + } + } + else if ( reader != null ) { + enumClass = (Class) reader.getReturnedClass().asSubclass( Enum.class ); + } + + final JavaType descriptor = typeConfiguration.getJavaTypeRegistry().getDescriptor( enumClass ); + enumJavaType = (EnumJavaType) descriptor; + + if ( parameters.containsKey( TYPE ) ) { + int jdbcTypeCode = Integer.parseInt( (String) parameters.get( TYPE ) ); + jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor( jdbcTypeCode ); + isOrdinal = jdbcType.isInteger() + // Both, ENUM and NAMED_ENUM are treated like ordinal with respect to the ordering + || jdbcType.getDefaultSqlTypeCode() == SqlTypes.ENUM + || jdbcType.getDefaultSqlTypeCode() == SqlTypes.NAMED_ENUM; + } + else { + final LocalJdbcTypeIndicators indicators; + final Long columnLength = reader == null ? null : reader.getColumnLengths()[0]; + if ( parameters.containsKey(NAMED) ) { + indicators = new LocalJdbcTypeIndicators( + // use ORDINAL as default for hbm.xml mappings + getBoolean( NAMED, parameters ) ? STRING : ORDINAL, + false, + columnLength + ); + } + else { + indicators = new LocalJdbcTypeIndicators( + getEnumType( reader ), + isNationalized( reader ), + columnLength + ); + } + jdbcType = descriptor.getRecommendedJdbcType( indicators ); + isOrdinal = indicators.getEnumeratedType() != STRING; + } + } + + private jakarta.persistence.EnumType getEnumType(ParameterType reader) { + if ( reader != null ) { + if ( reader.isPrimaryKey() ) { + final MapKeyEnumerated enumAnn = getAnnotation( reader.getAnnotationsMethod(), MapKeyEnumerated.class ); + if ( enumAnn != null ) { + return enumAnn.value(); + } + } + final Enumerated enumAnn = getAnnotation( reader.getAnnotationsMethod(), Enumerated.class ); + if ( enumAnn != null ) { + return enumAnn.value(); + } + } + return ORDINAL; + } + + private boolean isNationalized(ParameterType reader) { + return typeConfiguration.getCurrentBaseSqlTypeIndicators().isNationalized() + || reader!=null && getAnnotation( reader.getAnnotationsMethod(), Nationalized.class ) != null; + } + + @SuppressWarnings("unchecked") + private A getAnnotation(Annotation[] annotations, Class annotationType) { + for ( Annotation annotation : annotations ) { + if ( annotationType.isInstance( annotation ) ) { + return (A) annotation; + } + } + return null; + } + + @Override + public int getSqlType() { + verifyConfigured(); + return jdbcType.getJdbcTypeCode(); + } + + @Override + public Class returnedClass() { + return enumClass; + } + + @Override + public boolean equals(T x, T y) throws HibernateException { + return x == y; + } + + @Override + public int hashCode(T x) throws HibernateException { + return x == null ? 0 : x.hashCode(); + } + + @Override + public T nullSafeGet(ResultSet rs, int position, WrapperOptions options) + throws SQLException { + verifyConfigured(); + return jdbcType.getExtractor( enumJavaType ).extract( rs, position, options ); + } + + private void verifyConfigured() { + if ( enumJavaType == null ) { + throw new AssertionFailure("EnumType (" + enumClass.getName() + ") not properly, fully configured"); + } + } + + @Override + public void nullSafeSet(PreparedStatement st, T value, int index, WrapperOptions options) + throws SQLException { + verifyConfigured(); + jdbcType.getBinder( enumJavaType ).bind( st, value, index, options ); + } + + @Override + public T deepCopy(T value) throws HibernateException { + return value; + } + + @Override + public boolean isMutable() { + return false; + } + + @Override + public Serializable disassemble(T value) throws HibernateException { + return value; + } + + @Override + public T assemble(Serializable cached, Object owner) throws HibernateException { + return (T) cached; + } + + @Override + public T replace(T original, T target, Object owner) throws HibernateException { + return original; + } + + @Override + public TypeConfiguration getTypeConfiguration() { + return typeConfiguration; + } + + @Override + public void setTypeConfiguration(TypeConfiguration typeConfiguration) { + this.typeConfiguration = typeConfiguration; + } + + @Override + public String toSqlLiteral(T value) { + verifyConfigured(); + return isOrdinal() + ? Integer.toString( value.ordinal() ) + : "'" + value.name() + "'"; + } + + @Override + public String toString(T value) { + verifyConfigured(); + return enumJavaType.toName( value ); + } + + @Override + public T fromStringValue(CharSequence sequence) { + verifyConfigured(); + return enumJavaType.fromName( sequence.toString() ); + } + + @Override @SuppressWarnings("unchecked") + public String toLoggableString(Object value, SessionFactoryImplementor factory) { + verifyConfigured(); + return enumJavaType.extractLoggableRepresentation( (T) value ); + } + + public boolean isOrdinal() { + verifyConfigured(); + return isOrdinal; + } + + private class LocalJdbcTypeIndicators implements JdbcTypeIndicators { + private final jakarta.persistence.EnumType enumType; + private final boolean nationalized; + private final Long columnLength; + + private LocalJdbcTypeIndicators(jakarta.persistence.EnumType enumType, boolean nationalized, Long columnLength) { + this.enumType = enumType; + this.nationalized = nationalized; + this.columnLength = columnLength; + } + + @Override + public TypeConfiguration getTypeConfiguration() { + return typeConfiguration; + } + + @Override + public jakarta.persistence.EnumType getEnumeratedType() { + return enumType != null ? enumType : typeConfiguration.getCurrentBaseSqlTypeIndicators().getEnumeratedType(); + } + + @Override + public boolean isNationalized() { + return nationalized; + } + + + @Override + public long getColumnLength() { + return columnLength == null ? NO_COLUMN_LENGTH : columnLength; + } + + @Override + public Dialect getDialect() { + return typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(); + } + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/ExtendedEnumTypeTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/ExtendedEnumTypeTest.java index 53ae6c0be25d..2437343d3489 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/ExtendedEnumTypeTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/customtype/ExtendedEnumTypeTest.java @@ -31,11 +31,10 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; /** - * Tests that a custom type which extends {@link org.hibernate.type.EnumType} continues to be - * recognized as an EnumType rather than a basic custom type implementation since the values + * Tests that a custom type which extends {@link org.hibernate.envers.test.integration.customtype.EnumType} + * continues to be recognized as an EnumType rather than a basic custom type implementation since the values * which envers sends to describe the type in HBM differ whether its an Enum or not. * * Without the fix, this test would not even bootstrap and would throw a MappingException. @@ -47,7 +46,7 @@ public class ExtendedEnumTypeTest extends BaseEnversJPAFunctionalTestCase { // An extended type to trigger the need for Envers to supply type information in the HBM mappings. // This should be treated the same as any other property annotated as Enumerated or uses an Enum. - public static class ExtendedEnumType extends org.hibernate.type.EnumType { + public static class ExtendedEnumType extends org.hibernate.envers.test.integration.customtype.EnumType { } @@ -176,16 +175,17 @@ private void assertEnumProperty(Class entityClass, Class typeClass, String final UserType userType = ( (CustomType) propertyType ).getUserType(); assertTyping( typeClass, userType ); - assertTyping( org.hibernate.type.EnumType.class, userType ); - - switch ( expectedType ) { - case STRING: - assertTrue( !( (org.hibernate.type.EnumType) userType ).isOrdinal() ); - break; - default: - assertTrue( ( (org.hibernate.type.EnumType) userType ).isOrdinal() ); - break; - } + assertTyping( org.hibernate.envers.test.integration.customtype.EnumType.class, userType ); + + // org,hibernate.type.EnumType used to be special-cased in the Envers code +// switch ( expectedType ) { +// case STRING: +// assertTrue( !( (org.hibernate.envers.test.integration.customtype.EnumType) userType ).isOrdinal() ); +// break; +// default: +// assertTrue( ( (org.hibernate.envers.test.integration.customtype.EnumType) userType ).isOrdinal() ); +// break; +// } } ); } } diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/components/dynamic/SimpleEntity.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/components/dynamic/SimpleEntity.java index 3e961e4512d0..3146edf6a3d7 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/components/dynamic/SimpleEntity.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/components/dynamic/SimpleEntity.java @@ -43,12 +43,10 @@ public boolean equals(Object o) { if ( this == o ) { return true; } - if ( !( o instanceof SimpleEntity ) ) { + if ( !( o instanceof SimpleEntity that ) ) { return false; } - SimpleEntity that = (SimpleEntity) o; - if ( id != null ? !id.equals( that.id ) : that.id != null ) { return false; } diff --git a/hibernate-envers/src/test/resources/mappings/customType/mappings.hbm.xml b/hibernate-envers/src/test/resources/mappings/customType/mappings.hbm.xml index abc0224eeb9f..cf2b05b047be 100644 --- a/hibernate-envers/src/test/resources/mappings/customType/mappings.hbm.xml +++ b/hibernate-envers/src/test/resources/mappings/customType/mappings.hbm.xml @@ -12,14 +12,14 @@ - + org.hibernate.orm.test.envers.entities.customtype.UnspecifiedEnumTypeEntity$E1 - + org.hibernate.orm.test.envers.entities.customtype.UnspecifiedEnumTypeEntity$E2 diff --git a/hibernate-graalvm/src/main/java/org/hibernate/graalvm/internal/StaticClassLists.java b/hibernate-graalvm/src/main/java/org/hibernate/graalvm/internal/StaticClassLists.java index 66e6be18abeb..ab59caae374d 100644 --- a/hibernate-graalvm/src/main/java/org/hibernate/graalvm/internal/StaticClassLists.java +++ b/hibernate-graalvm/src/main/java/org/hibernate/graalvm/internal/StaticClassLists.java @@ -6,7 +6,6 @@ import org.hibernate.tool.schema.internal.script.MultiLineSqlScriptExtractor; -import org.hibernate.type.EnumType; /** * The place to list all "static" types we know of that need to be possible to @@ -36,9 +35,7 @@ public static Class[] typesNeedingDefaultConstructorAccessible() { org.hibernate.id.enhanced.SequenceStyleGenerator.class, org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl.class, org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl.class, - EnumType.class, MultiLineSqlScriptExtractor.class, - }; } diff --git a/hibernate-graalvm/src/test/java/org/hibernate/graalvm/internal/StaticClassListsTest.java b/hibernate-graalvm/src/test/java/org/hibernate/graalvm/internal/StaticClassListsTest.java index 056e88d50753..cd95e366deb9 100644 --- a/hibernate-graalvm/src/test/java/org/hibernate/graalvm/internal/StaticClassListsTest.java +++ b/hibernate-graalvm/src/test/java/org/hibernate/graalvm/internal/StaticClassListsTest.java @@ -114,7 +114,6 @@ Stream> classes() { org.hibernate.id.enhanced.SequenceStyleGenerator.class, org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl.class, org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl.class, - org.hibernate.type.EnumType.class, org.hibernate.tool.schema.internal.script.MultiLineSqlScriptExtractor.class ); }