use of jakarta.persistence.TemporalType in project hibernate-orm by hibernate.
the class VersionResolution method from.
// todo (6.0) : support explicit JTD?
// todo (6.0) : support explicit STD?
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <E> VersionResolution<E> from(Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess, Function<TypeConfiguration, BasicJavaType> explicitJtdAccess, Function<TypeConfiguration, JdbcType> explicitStdAccess, TimeZoneStorageType timeZoneStorageType, TypeConfiguration typeConfiguration, @SuppressWarnings("unused") MetadataBuildingContext context) {
// todo (6.0) : add support for Dialect-specific interpretation?
final java.lang.reflect.Type implicitJavaType = implicitJavaTypeAccess.apply(typeConfiguration);
final JavaType registered = typeConfiguration.getJavaTypeRegistry().resolveDescriptor(implicitJavaType);
final BasicJavaType jtd = (BasicJavaType) registered;
final JdbcType recommendedJdbcType = jtd.getRecommendedJdbcType(new JdbcTypeIndicators() {
@Override
public TypeConfiguration getTypeConfiguration() {
return typeConfiguration;
}
@Override
public TemporalType getTemporalPrecision() {
// if it is a temporal version, it needs to be a TIMESTAMP
return TemporalType.TIMESTAMP;
}
@Override
public TimeZoneStorageStrategy getDefaultTimeZoneStorageStrategy() {
if (timeZoneStorageType != null) {
switch(timeZoneStorageType) {
case NATIVE:
return TimeZoneStorageStrategy.NATIVE;
case NORMALIZE:
return TimeZoneStorageStrategy.NORMALIZE;
}
}
return context.getBuildingOptions().getDefaultTimeZoneStorage();
}
});
final BasicType<?> basicType = typeConfiguration.getBasicTypeRegistry().resolve(jtd, recommendedJdbcType);
final BasicType legacyType = typeConfiguration.getBasicTypeRegistry().getRegisteredType(jtd.getJavaType());
assert legacyType.getJdbcType().equals(recommendedJdbcType);
return new VersionResolution<>(jtd, recommendedJdbcType, basicType, legacyType);
}
use of jakarta.persistence.TemporalType in project hibernate-orm by hibernate.
the class BindingTypeHelper method resolveBindType.
public JdbcMapping resolveBindType(Object value, JdbcMapping baseType, TypeConfiguration typeConfiguration) {
if (value == null || !(baseType.getJavaTypeDescriptor() instanceof TemporalJavaType<?>)) {
return baseType;
}
final Class<?> javaType = value.getClass();
final TemporalType temporalType = ((TemporalJavaType<?>) baseType.getJavaTypeDescriptor()).getPrecision();
switch(temporalType) {
case TIMESTAMP:
{
return (JdbcMapping) resolveTimestampTemporalTypeVariant(javaType, (BindableType<?>) baseType, typeConfiguration);
}
case DATE:
{
return (JdbcMapping) resolveDateTemporalTypeVariant(javaType, (BindableType<?>) baseType, typeConfiguration);
}
case TIME:
{
return (JdbcMapping) resolveTimeTemporalTypeVariant(javaType, (BindableType<?>) baseType, typeConfiguration);
}
default:
{
throw new IllegalArgumentException("Unexpected TemporalType [" + temporalType + "]; expecting TIMESTAMP, DATE or TIME");
}
}
}
use of jakarta.persistence.TemporalType in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method transformDatetimeArithmetic.
private Object transformDatetimeArithmetic(SqmBinaryArithmetic<?> expression) {
BinaryArithmeticOperator operator = expression.getOperator();
// timestamps are ill-formed
if (operator != SUBTRACT) {
throw new SemanticException("illegal operator for temporal type: " + operator);
}
// a difference between two dates or two
// timestamps is a leaf duration, so we
// must apply the scale, and the 'by unit'
// ts1 - ts2
Expression left = cleanly(() -> toSqlExpression(expression.getLeftHandOperand().accept(this)));
Expression right = cleanly(() -> toSqlExpression(expression.getRightHandOperand().accept(this)));
TypeConfiguration typeConfiguration = getCreationContext().getMappingMetamodel().getTypeConfiguration();
TemporalType leftTimestamp = typeConfiguration.getSqlTemporalType(expression.getLeftHandOperand().getNodeType());
TemporalType rightTimestamp = typeConfiguration.getSqlTemporalType(expression.getRightHandOperand().getNodeType());
// when we're dealing with Dates, we use
// DAY as the smallest unit, otherwise we
// use a platform-specific granularity
TemporalUnit baseUnit = (rightTimestamp == TemporalType.TIMESTAMP || leftTimestamp == TemporalType.TIMESTAMP) ? NATIVE : DAY;
if (adjustedTimestamp != null) {
if (appliedByUnit != null) {
throw new IllegalStateException();
}
// we're using the resulting duration to
// adjust a date or timestamp on the left
// baseUnit is the finest resolution for the
// temporal type, so we must use it for both
// the diff, and then the subsequent add
DurationUnit unit = new DurationUnit(baseUnit, basicType(Integer.class));
Expression magnitude = applyScale(timestampdiff().expression(null, unit, right, left));
return timestampadd().expression(// TODO should be adjustedTimestamp.getType()
(ReturnableType<?>) adjustedTimestampType, unit, magnitude, adjustedTimestamp);
} else if (appliedByUnit != null) {
// we're immediately converting the resulting
// duration to a scalar in the given unit
DurationUnit unit = (DurationUnit) appliedByUnit.getUnit().accept(this);
return applyScale(timestampdiff().expression(null, unit, right, left));
} else {
// a plain "bare" Duration
DurationUnit unit = new DurationUnit(baseUnit, basicType(Integer.class));
BasicValuedMapping durationType = (BasicValuedMapping) expression.getNodeType();
Expression scaledMagnitude = applyScale(timestampdiff().expression((ReturnableType<?>) expression.getNodeType(), unit, right, left));
return new Duration(scaledMagnitude, baseUnit, durationType);
}
}
use of jakarta.persistence.TemporalType in project hibernate-orm by hibernate.
the class ZonedDateTimeJavaType method getRecommendedJdbcType.
@Override
public JdbcType getRecommendedJdbcType(JdbcTypeIndicators stdIndicators) {
final TemporalType temporalPrecision = stdIndicators.getTemporalPrecision();
final JdbcTypeRegistry jdbcTypeRegistry = stdIndicators.getTypeConfiguration().getJdbcTypeRegistry();
if (temporalPrecision == null || temporalPrecision == TemporalType.TIMESTAMP) {
return stdIndicators.getDefaultTimeZoneStorageStrategy() == TimeZoneStorageStrategy.NORMALIZE ? jdbcTypeRegistry.getDescriptor(Types.TIMESTAMP) : jdbcTypeRegistry.getDescriptor(Types.TIMESTAMP_WITH_TIMEZONE);
}
switch(temporalPrecision) {
case TIME:
{
return jdbcTypeRegistry.getDescriptor(Types.TIME);
}
case DATE:
{
return jdbcTypeRegistry.getDescriptor(Types.DATE);
}
default:
{
throw new IllegalArgumentException("Unexpected jakarta.persistence.TemporalType : " + temporalPrecision);
}
}
}
Aggregations