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();
}
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));
}
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;
}
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());
}
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);
}
Aggregations