Search in sources :

Example 6 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class AbstractStandardBasicType method getCastType.

@Override
public CastType getCastType() {
    final JdbcType jdbcType = getJdbcType();
    final int jdbcTypeCode = jdbcType.getJdbcTypeCode();
    switch(jdbcTypeCode) {
        case Types.BIT:
        case Types.SMALLINT:
        case Types.TINYINT:
        case Types.INTEGER:
            if (getJavaType() == Boolean.class) {
                return CastType.INTEGER_BOOLEAN;
            }
            break;
        case Types.CHAR:
        case Types.NCHAR:
            if (getJavaType() == Boolean.class) {
                return (Boolean) getJavaTypeDescriptor().wrap('Y', null) ? CastType.YN_BOOLEAN : CastType.TF_BOOLEAN;
            }
            break;
        case Types.TIMESTAMP_WITH_TIMEZONE:
            if (getJavaType() == ZonedDateTime.class) {
                return CastType.ZONE_TIMESTAMP;
            }
            break;
    }
    return jdbcType.getCastType();
}
Also used : JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType)

Example 7 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class DurationMappingTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityWithDuration.class);
    final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
    final BasicAttributeMapping duration = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("duration");
    final JdbcMapping jdbcMapping = duration.getJdbcMapping();
    assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Duration.class));
    final JdbcType intervalType = jdbcTypeRegistry.getDescriptor(SqlTypes.INTERVAL_SECOND);
    final JdbcType realType;
    if (intervalType instanceof AdjustableJdbcType) {
        realType = ((AdjustableJdbcType) intervalType).resolveIndicatedType(() -> mappingMetamodel.getTypeConfiguration(), jdbcMapping.getJavaTypeDescriptor());
    } else {
        realType = intervalType;
    }
    assertThat(jdbcMapping.getJdbcType(), is(realType));
    scope.inTransaction((session) -> {
        session.persist(new EntityWithDuration(1, Duration.ofHours(3)));
    });
    scope.inTransaction((session) -> session.find(EntityWithDuration.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) AdjustableJdbcType(org.hibernate.type.descriptor.jdbc.AdjustableJdbcType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AdjustableJdbcType(org.hibernate.type.descriptor.jdbc.AdjustableJdbcType) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Duration(java.time.Duration) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 8 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class MappingMetamodelImpl method resolveQueryParameterType.

@Override
public <T> BindableType<T> resolveQueryParameterType(Class<T> javaClass) {
    final BasicType<T> basicType = getTypeConfiguration().getBasicTypeForJavaType(javaClass);
    // For enums, we simply don't know the exact mapping if there is no basic type registered
    if (basicType != null || javaClass.isEnum()) {
        return basicType;
    }
    final ManagedDomainType<T> managedType = jpaMetamodel.findManagedType(javaClass);
    if (managedType != null) {
        return managedType;
    }
    final JavaType<T> javaType = getTypeConfiguration().getJavaTypeRegistry().findDescriptor(javaClass);
    if (javaType != null) {
        final JdbcType recommendedJdbcType = javaType.getRecommendedJdbcType(getTypeConfiguration().getCurrentBaseSqlTypeIndicators());
        if (recommendedJdbcType != null) {
            return getTypeConfiguration().getBasicTypeRegistry().resolve(javaType, recommendedJdbcType);
        }
    }
    return null;
}
Also used : JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType)

Example 9 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class NamedConverterResolution method fromInternal.

private static <T> NamedConverterResolution<T> fromInternal(Function<TypeConfiguration, BasicJavaType> explicitJtdAccess, Function<TypeConfiguration, JdbcType> explicitStdAccess, Function<TypeConfiguration, MutabilityPlan> explicitMutabilityPlanAccess, JpaAttributeConverter<T, ?> converter, JdbcTypeIndicators sqlTypeIndicators, MetadataBuildingContext context) {
    final TypeConfiguration typeConfiguration = context.getBootstrapContext().getTypeConfiguration();
    final JavaType<T> explicitJtd = explicitJtdAccess != null ? explicitJtdAccess.apply(typeConfiguration) : null;
    final JavaType<T> domainJtd = explicitJtd != null ? explicitJtd : converter.getDomainJavaType();
    final JdbcType explicitJdbcType = explicitStdAccess != null ? explicitStdAccess.apply(typeConfiguration) : null;
    final JavaType<?> relationalJtd = converter.getRelationalJavaType();
    final JdbcType jdbcType = explicitJdbcType != null ? explicitJdbcType : relationalJtd.getRecommendedJdbcType(sqlTypeIndicators);
    final MutabilityPlan<T> explicitMutabilityPlan = explicitMutabilityPlanAccess != null ? explicitMutabilityPlanAccess.apply(typeConfiguration) : null;
    final MutabilityPlan<T> mutabilityPlan;
    if (explicitMutabilityPlan != null) {
        mutabilityPlan = explicitMutabilityPlan;
    } else if (!domainJtd.getMutabilityPlan().isMutable()) {
        mutabilityPlan = ImmutableMutabilityPlan.instance();
    } else {
        mutabilityPlan = new AttributeConverterMutabilityPlanImpl<>(converter, true);
    }
    return new NamedConverterResolution<T>(domainJtd, relationalJtd, jdbcType, converter, mutabilityPlan, context.getBootstrapContext().getTypeConfiguration());
}
Also used : JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AttributeConverterMutabilityPlanImpl(org.hibernate.type.descriptor.converter.AttributeConverterMutabilityPlanImpl) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration)

Example 10 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class InferredBasicValueResolver method from.

public static <T> BasicValue.Resolution<T> from(BasicJavaType<T> explicitJavaType, JdbcType explicitJdbcType, Type resolvedJavaType, Supplier<JavaType<T>> reflectedJtdResolver, JdbcTypeIndicators stdIndicators, Table table, Selectable selectable, String ownerName, String propertyName, TypeConfiguration typeConfiguration) {
    final JavaType<T> reflectedJtd = reflectedJtdResolver.get();
    // NOTE : the distinction that is made below wrt `explicitJavaType` and `reflectedJtd` is
    // needed temporarily to trigger "legacy resolution" versus "ORM6 resolution.  Yes, it
    // makes the code a little more complicated but the benefit is well worth it - saving memory
    final BasicType<T> jdbcMapping;
    final BasicType<T> legacyType;
    if (explicitJavaType != null) {
        if (explicitJdbcType != null) {
            // we also have an explicit JdbcType
            jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(explicitJavaType, explicitJdbcType);
            legacyType = jdbcMapping;
        } else {
            if (explicitJavaType instanceof TemporalJavaType) {
                return fromTemporal((TemporalJavaType<T>) explicitJavaType, null, null, resolvedJavaType, stdIndicators, typeConfiguration);
            }
            // we need to infer the JdbcType and use that to build the value-mapping
            final JdbcType inferredJdbcType = explicitJavaType.getRecommendedJdbcType(stdIndicators);
            if (inferredJdbcType instanceof ObjectJdbcType) {
                if (explicitJavaType instanceof EnumJavaType) {
                    return fromEnum((EnumJavaType) reflectedJtd, explicitJavaType, null, stdIndicators, typeConfiguration);
                } else if (explicitJavaType instanceof TemporalJavaType) {
                    return fromTemporal((TemporalJavaType<T>) reflectedJtd, explicitJavaType, null, resolvedJavaType, stdIndicators, typeConfiguration);
                } else if (explicitJavaType instanceof SerializableJavaType || explicitJavaType.getJavaType() instanceof Serializable) {
                    legacyType = new SerializableType(explicitJavaType);
                    jdbcMapping = legacyType;
                } else {
                    jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(explicitJavaType, inferredJdbcType);
                    legacyType = jdbcMapping;
                }
            } else {
                jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(explicitJavaType, inferredJdbcType);
                legacyType = jdbcMapping;
            }
        }
    } else if (reflectedJtd != null) {
        // we were able to determine the "reflected java-type"
        if (explicitJdbcType != null) {
            // we also have an explicit JdbcType
            jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(reflectedJtd, explicitJdbcType);
            legacyType = jdbcMapping;
        } else {
            if (reflectedJtd instanceof EnumJavaType) {
                return fromEnum((EnumJavaType) reflectedJtd, null, null, stdIndicators, typeConfiguration);
            } else if (reflectedJtd instanceof TemporalJavaType) {
                return fromTemporal((TemporalJavaType<T>) reflectedJtd, null, null, resolvedJavaType, stdIndicators, typeConfiguration);
            } else {
                // see if there is a registered BasicType for this JavaType and, if so, use it.
                // this mimics the legacy handling
                final BasicType<T> registeredType = typeConfiguration.getBasicTypeRegistry().getRegisteredType(reflectedJtd.getJavaType());
                if (registeredType != null) {
                    // so here is the legacy resolution
                    legacyType = resolveSqlTypeIndicators(stdIndicators, registeredType, reflectedJtd);
                    jdbcMapping = legacyType;
                } else {
                    // there was not a "legacy" BasicType registration,  so use `JavaType#getRecommendedJdbcType`, if
                    // one, to create a mapping
                    final JdbcType recommendedJdbcType = reflectedJtd.getRecommendedJdbcType(stdIndicators);
                    if (recommendedJdbcType != null) {
                        jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(reflectedJtd, recommendedJdbcType);
                        legacyType = jdbcMapping;
                    } else if (reflectedJtd instanceof SerializableJavaType || Serializable.class.isAssignableFrom(reflectedJtd.getJavaTypeClass())) {
                        legacyType = new SerializableType(reflectedJtd);
                        jdbcMapping = legacyType;
                    } else {
                        // let this fall through to the exception creation below
                        legacyType = null;
                        jdbcMapping = null;
                    }
                }
            }
        }
    } else {
        if (explicitJdbcType != null) {
            // we have an explicit STD, but no JTD - infer JTD
            // - NOTE : yes its an odd case, but its easy to implement here, so...
            Integer length = null;
            Integer scale = null;
            if (selectable instanceof Column) {
                final Column column = (Column) selectable;
                if (column.getPrecision() != null && column.getPrecision() > 0) {
                    length = column.getPrecision();
                    scale = column.getScale();
                } else if (column.getLength() != null) {
                    if (column.getLength() > (long) Integer.MAX_VALUE) {
                        length = Integer.MAX_VALUE;
                    } else {
                        length = column.getLength().intValue();
                    }
                }
            }
            final BasicJavaType<T> recommendedJtd = explicitJdbcType.getJdbcRecommendedJavaTypeMapping(length, scale, typeConfiguration);
            jdbcMapping = resolveSqlTypeIndicators(stdIndicators, typeConfiguration.getBasicTypeRegistry().resolve(recommendedJtd, explicitJdbcType), recommendedJtd);
            legacyType = jdbcMapping;
        } else {
            throw new MappingException("Could not determine JavaType nor JdbcType to use" + " for BasicValue: owner = " + ownerName + "; property = " + propertyName + "; table = " + table.getName() + "; column = " + selectable.getText());
        }
    }
    if (jdbcMapping == null) {
        throw new MappingException("Could not determine JavaType nor JdbcType to use" + "" + " for " + ((BasicValue) stdIndicators).getResolvedJavaType() + "; table = " + table.getName() + "; column = " + selectable.getText());
    }
    return new InferredBasicValueResolution<>(jdbcMapping, jdbcMapping.getJavaTypeDescriptor(), jdbcMapping.getJavaTypeDescriptor(), jdbcMapping.getJdbcType(), null, legacyType, null);
}
Also used : Serializable(java.io.Serializable) SerializableJavaType(org.hibernate.type.descriptor.java.SerializableJavaType) SerializableType(org.hibernate.type.SerializableType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) MappingException(org.hibernate.MappingException) BasicValue(org.hibernate.mapping.BasicValue) TemporalJavaType(org.hibernate.type.descriptor.java.TemporalJavaType) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) EnumJavaType(org.hibernate.type.descriptor.java.EnumJavaType) Column(org.hibernate.mapping.Column)

Aggregations

JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)36 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)11 BasicJavaType (org.hibernate.type.descriptor.java.BasicJavaType)7 BasicValue (org.hibernate.mapping.BasicValue)6 ObjectJdbcType (org.hibernate.type.descriptor.jdbc.ObjectJdbcType)6 MappingException (org.hibernate.MappingException)5 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)5 AttributeConverterTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter)5 EnumJavaType (org.hibernate.type.descriptor.java.EnumJavaType)5 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)5 MapKeyJdbcType (org.hibernate.annotations.MapKeyJdbcType)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)4 Type (org.hibernate.type.Type)4 JavaType (org.hibernate.type.descriptor.java.JavaType)4 StringJavaType (org.hibernate.type.descriptor.java.StringJavaType)4 Test (org.junit.Test)4 MetadataSources (org.hibernate.boot.MetadataSources)3 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)3 PersistentClass (org.hibernate.mapping.PersistentClass)3