diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java b/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java index a5353f12c08b..d18696b988a0 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java @@ -686,7 +686,7 @@ public void contributeType(CompositeUserType type) { ); } else { - addFallbackIfNecessary( jdbcTypeRegistry, SqlTypes.INTERVAL_SECOND, SqlTypes.NUMERIC ); + addFallbackIfNecessary( jdbcTypeRegistry, SqlTypes.INTERVAL_SECOND, SqlTypes.DURATION ); } addFallbackIfNecessary( jdbcTypeRegistry, SqlTypes.INET, SqlTypes.VARBINARY ); diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/AbstractPostgreSQLStructJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/AbstractPostgreSQLStructJdbcType.java index 49bd79e360a1..ea433d40a125 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/AbstractPostgreSQLStructJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/AbstractPostgreSQLStructJdbcType.java @@ -776,6 +776,7 @@ private void serializeBasicTo( case SqlTypes.DOUBLE: case SqlTypes.DECIMAL: case SqlTypes.NUMERIC: + case SqlTypes.DURATION: jdbcJavaType.appendEncodedString( appender, jdbcJavaType.unwrap( diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/JsonHelper.java b/hibernate-core/src/main/java/org/hibernate/dialect/JsonHelper.java index 91b5b564d45e..fa6712b55f0f 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/JsonHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/JsonHelper.java @@ -194,6 +194,7 @@ else if ( mappedType instanceof BasicType ) { break; case SqlTypes.DECIMAL: case SqlTypes.NUMERIC: + case SqlTypes.DURATION: case SqlTypes.UUID: // These types need to be serialized as JSON string, but don't have a need for escaping appender.append( '"' ); diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/XmlHelper.java b/hibernate-core/src/main/java/org/hibernate/dialect/XmlHelper.java index 69e8e9fec23a..cd68779d7e14 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/XmlHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/XmlHelper.java @@ -564,6 +564,7 @@ private static void serializeValueTo(XMLAppender appender, SelectableMapping sel case SqlTypes.DOUBLE: case SqlTypes.DECIMAL: case SqlTypes.NUMERIC: + case SqlTypes.DURATION: jdbcJavaType.appendEncodedString( appender, jdbcJavaType.unwrap( diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/config/ConfigurationHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/config/ConfigurationHelper.java index 979a52271c7a..cd287a3cd4f2 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/config/ConfigurationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/config/ConfigurationHelper.java @@ -546,7 +546,7 @@ public static synchronized int getPreferredSqlTypeCodeForDuration(StandardServic return explicitSetting; } - return SqlTypes.NUMERIC; + return SqlTypes.DURATION; } @Incubating diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/TypecheckUtil.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/TypecheckUtil.java index dd84d30823cb..359701042bd1 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/TypecheckUtil.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/TypecheckUtil.java @@ -507,39 +507,24 @@ private static boolean isNumberArray(SqmExpressible expressible) { public static void assertString(SqmExpression expression) { final SqmExpressible nodeType = expression.getNodeType(); if ( nodeType != null ) { - final Class javaType = nodeType.getExpressibleJavaType().getJavaTypeClass(); - if ( javaType != String.class && javaType != char[].class ) { + final DomainType domainType = nodeType.getSqmType(); + if ( !( domainType instanceof JdbcMapping ) || !( (JdbcMapping) domainType ).getJdbcType().isStringLike() ) { throw new SemanticException( "Operand of 'like' is of type '" + nodeType.getTypeName() + - "' which is not a string (it is not an instance of 'java.lang.String' or 'char[]')" + "' which is not a string (its JDBC type code is not string-like)" ); } } } -// public static void assertNumeric(SqmExpression expression, BinaryArithmeticOperator op) { -// final SqmExpressible nodeType = expression.getNodeType(); -// if ( nodeType != null ) { -// final Class javaType = nodeType.getExpressibleJavaType().getJavaTypeClass(); -// if ( !Number.class.isAssignableFrom( javaType ) -// && !Temporal.class.isAssignableFrom( javaType ) -// && !TemporalAmount.class.isAssignableFrom( javaType ) -// && !java.util.Date.class.isAssignableFrom( javaType ) ) { -// throw new SemanticException( "Operand of " + op.getOperatorSqlText() -// + " is of type '" + nodeType.getTypeName() + "' which is not a numeric type" -// + " (it is not an instance of 'java.lang.Number', 'java.time.Temporal', or 'java.time.TemporalAmount')" ); -// } -// } -// } - public static void assertDuration(SqmExpression expression) { final SqmExpressible nodeType = expression.getNodeType(); if ( nodeType != null ) { - final Class javaType = nodeType.getExpressibleJavaType().getJavaTypeClass(); - if ( !TemporalAmount.class.isAssignableFrom( javaType ) ) { + final DomainType domainType = nodeType.getSqmType(); + if ( !( domainType instanceof JdbcMapping ) || !( (JdbcMapping) domainType ).getJdbcType().isDuration() ) { throw new SemanticException( "Operand of 'by' is of type '" + nodeType.getTypeName() + - "' which is not a duration (it is not an instance of 'java.time.TemporalAmount')" + "' which is not a duration (its JDBC type code is not duration-like)" ); } } @@ -548,11 +533,11 @@ public static void assertDuration(SqmExpression expression) { public static void assertNumeric(SqmExpression expression, UnaryArithmeticOperator op) { final SqmExpressible nodeType = expression.getNodeType(); if ( nodeType != null ) { - final Class javaType = nodeType.getExpressibleJavaType().getJavaTypeClass(); - if ( !Number.class.isAssignableFrom( javaType ) ) { + final DomainType domainType = nodeType.getSqmType(); + if ( !( domainType instanceof JdbcMapping ) || !( (JdbcMapping) domainType ).getJdbcType().isNumber() ) { throw new SemanticException( "Operand of " + op.getOperatorChar() + " is of type '" + nodeType.getTypeName() + - "' which is not a numeric type (it is not an instance of 'java.lang.Number')" + "' which is not a numeric type (its JDBC type code is not numeric)" ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/SqlTypes.java b/hibernate-core/src/main/java/org/hibernate/type/SqlTypes.java index 6eaff7dc81fc..6ca2212e5529 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/SqlTypes.java +++ b/hibernate-core/src/main/java/org/hibernate/type/SqlTypes.java @@ -498,6 +498,14 @@ public class SqlTypes { */ public static final int TIME_UTC = 3007; + /** + * A type code representing a "virtual mapping" of {@linkplain java.time.Duration}. + * + * @see Types#NUMERIC + * @see org.hibernate.type.descriptor.jdbc.DurationJdbcType + */ + public static final int DURATION = 3015; + // Interval types /** @@ -779,6 +787,13 @@ public static boolean isIntervalType(int typeCode) { return typeCode == INTERVAL_SECOND; } + /** + * Does the given typecode represent a {@code duration} type? + */ + public static boolean isDurationType(int typeCode) { + return typeCode == DURATION; + } + /** * Does the given typecode represent a SQL date or timestamp type? * @param typeCode a JDBC type code from {@link Types} diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/DurationJdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/DurationJdbcType.java new file mode 100644 index 000000000000..9342b9b5284e --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/DurationJdbcType.java @@ -0,0 +1,42 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.type.descriptor.jdbc; + +import java.sql.Types; +import java.time.Duration; + +import org.hibernate.type.SqlTypes; +import org.hibernate.type.descriptor.WrapperOptions; + +/** + * Descriptor for {@link java.time.Duration}. + * + * @author Marco Belladelli + */ +public class DurationJdbcType extends NumericJdbcType { + public static final DurationJdbcType INSTANCE = new DurationJdbcType(); + + @Override + public int getDdlTypeCode() { + return Types.NUMERIC; + } + + @Override + public int getDefaultSqlTypeCode() { + return SqlTypes.DURATION; + } + + @Override + public String getFriendlyName() { + return "DURATION"; + } + + @Override + public String toString() { + return "DurationJdbcType"; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java index dd03a423ac82..e2b26b3aba91 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java @@ -287,6 +287,12 @@ default boolean isInterval() { return isIntervalType( getDdlTypeCode() ); } + default boolean isDuration() { + final int ddlTypeCode = getDefaultSqlTypeCode(); + return isDurationType( ddlTypeCode ) + || isIntervalType( ddlTypeCode ); + } + default CastType getCastType() { return getCastType( getDdlTypeCode() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcTypeBaseline.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcTypeBaseline.java index 2a276c66e753..b606122dd174 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcTypeBaseline.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcTypeBaseline.java @@ -18,6 +18,7 @@ import org.hibernate.type.descriptor.jdbc.DateJdbcType; import org.hibernate.type.descriptor.jdbc.DecimalJdbcType; import org.hibernate.type.descriptor.jdbc.DoubleJdbcType; +import org.hibernate.type.descriptor.jdbc.DurationJdbcType; import org.hibernate.type.descriptor.jdbc.FloatJdbcType; import org.hibernate.type.descriptor.jdbc.IntegerJdbcType; import org.hibernate.type.descriptor.jdbc.JdbcType; @@ -66,6 +67,7 @@ public static void prime(BaselineTarget target) { target.addDescriptor( TimestampWithTimeZoneJdbcType.INSTANCE ); target.addDescriptor( TimeJdbcType.INSTANCE ); target.addDescriptor( TimeWithTimeZoneJdbcType.INSTANCE ); + target.addDescriptor( DurationJdbcType.INSTANCE ); target.addDescriptor( BinaryJdbcType.INSTANCE ); target.addDescriptor( VarbinaryJdbcType.INSTANCE );