use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class BasicTypeRegistry method resolveTypeReference.
private BasicType<?> resolveTypeReference(String name) {
final BasicTypeReference<?> typeReference = typeReferencesByName.get(name);
if (typeReference == null) {
return null;
}
if (!name.equals(typeReference.getName())) {
final BasicType<?> basicType = typesByName.get(typeReference.getName());
if (basicType != null) {
return basicType;
}
}
final JavaType<Object> javaType = typeConfiguration.getJavaTypeRegistry().getDescriptor(typeReference.getBindableJavaType());
final JdbcType jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(typeReference.getSqlTypeCode());
final BasicType<?> type;
if (typeReference.getConverter() == null) {
if (typeReference.isForceImmutable()) {
type = new ImmutableNamedBasicTypeImpl<>(javaType, jdbcType, typeReference.getName());
} else {
type = new NamedBasicTypeImpl<>(javaType, jdbcType, typeReference.getName());
}
} else {
if (typeReference.isForceImmutable()) {
// noinspection unchecked
type = new ImmutableConvertedBasicTypeImpl<>(javaType, jdbcType, typeReference.getName(), (BasicValueConverter<Object, ?>) typeReference.getConverter());
} else {
// noinspection unchecked
type = new ConvertedBasicTypeImpl<>(javaType, jdbcType, typeReference.getName(), (BasicValueConverter<Object, ?>) typeReference.getConverter());
}
}
primeRegistryEntry(type);
typesByName.put(typeReference.getName(), type);
typesByName.put(name, type);
return type;
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class EnumType method setParameterValues.
@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);
// handles hbm.xml
if (reader != null) {
enumClass = (Class<T>) reader.getReturnedClass().asSubclass(Enum.class);
final Long columnLength = reader.getColumnLengths()[0];
final boolean isOrdinal;
final jakarta.persistence.EnumType enumType = getEnumType(reader);
if (enumType == null) {
isOrdinal = true;
} else if (jakarta.persistence.EnumType.ORDINAL.equals(enumType)) {
isOrdinal = true;
} else if (jakarta.persistence.EnumType.STRING.equals(enumType)) {
isOrdinal = false;
} else {
throw new AssertionFailure("Unknown EnumType: " + enumType);
}
final EnumJavaType enumJavaType = (EnumJavaType) typeConfiguration.getJavaTypeRegistry().getDescriptor(enumClass);
final LocalJdbcTypeIndicators indicators = new LocalJdbcTypeIndicators(enumType, columnLength, reader);
final BasicJavaType<?> relationalJavaType = resolveRelationalJavaType(indicators, enumJavaType);
final JdbcType jdbcType = relationalJavaType.getRecommendedJdbcType(indicators);
if (isOrdinal) {
this.enumValueConverter = new OrdinalEnumValueConverter(enumJavaType, jdbcType, relationalJavaType);
} else {
this.enumValueConverter = new NamedEnumValueConverter(enumJavaType, jdbcType, relationalJavaType);
}
this.jdbcType = jdbcType;
} else {
final String enumClassName = (String) parameters.get(ENUM);
try {
enumClass = ReflectHelper.classForName(enumClassName, this.getClass()).asSubclass(Enum.class);
} catch (ClassNotFoundException exception) {
throw new HibernateException("Enum class not found: " + enumClassName, exception);
}
this.enumValueConverter = interpretParameters(parameters);
this.jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(enumValueConverter.getJdbcTypeCode());
}
this.jdbcValueExtractor = (ValueExtractor) jdbcType.getExtractor(enumValueConverter.getRelationalJavaType());
this.jdbcValueBinder = (ValueBinder) jdbcType.getBinder(enumValueConverter.getRelationalJavaType());
if (LOG.isDebugEnabled()) {
LOG.debugf("Using %s-based conversion for Enum %s", isOrdinal() ? "ORDINAL" : "NAMED", enumClass.getName());
}
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class SerializableToBlobType 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 EnumResolutionTests method verifyEnumResolution.
@SuppressWarnings("rawtypes")
private void verifyEnumResolution(Property property, int jdbcCode, Class<?> javaType, Consumer<BasicValueConverter> converterChecker, Consumer<BasicType> legacyTypeChecker) {
final BasicValue.Resolution<?> resolution = ((BasicValue) property.getValue()).resolve();
final TypeConfiguration typeConfiguration = ((BasicValue) property.getValue()).getTypeConfiguration();
final JdbcType jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(jdbcCode);
// verify the interpretations used for reading
assertThat(resolution.getJdbcType(), is(jdbcType));
assertThat(resolution.getRelationalJavaType().getJavaTypeClass(), equalTo(javaType));
assertThat(resolution.getDomainJavaType().getJavaTypeClass(), equalTo(Values.class));
final JdbcMapping jdbcMapping = resolution.getJdbcMapping();
assertThat(jdbcMapping.getJdbcType(), equalTo(resolution.getJdbcType()));
assertThat(jdbcMapping.getJavaTypeDescriptor(), equalTo(resolution.getRelationalJavaType()));
converterChecker.accept(resolution.getValueConverter());
// verify the (legacy) interpretations used for writing
legacyTypeChecker.accept(resolution.getLegacyResolvedBasicType());
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class AbstractSelectionQuery method verifyResultType.
protected static <T> void verifyResultType(Class<T> resultClass, SqmExpressible<?> sqmExpressible, SessionFactoryImplementor sessionFactory) {
assert sqmExpressible != null;
final JavaType<?> expressibleJavaType = sqmExpressible.getExpressibleJavaType();
assert expressibleJavaType != null;
final Class<?> javaTypeClass = expressibleJavaType.getJavaTypeClass();
if (!resultClass.isAssignableFrom(javaTypeClass)) {
if (expressibleJavaType instanceof PrimitiveJavaType) {
if (((PrimitiveJavaType) expressibleJavaType).getPrimitiveClass() == resultClass) {
return;
}
throwQueryTypeMismatchException(resultClass, sqmExpressible);
}
// But the expected resultClass could be a subtype of that, so we need to check the JdbcType
if (javaTypeClass == Date.class) {
JdbcType jdbcType = null;
if (sqmExpressible instanceof BasicDomainType<?>) {
jdbcType = ((BasicDomainType<?>) sqmExpressible).getJdbcType();
} else if (sqmExpressible instanceof SqmPathSource<?>) {
final DomainType<?> domainType = ((SqmPathSource<?>) sqmExpressible).getSqmPathType();
if (domainType instanceof BasicDomainType<?>) {
jdbcType = ((BasicDomainType<?>) domainType).getJdbcType();
}
}
if (jdbcType != null) {
switch(jdbcType.getJdbcTypeCode()) {
case Types.DATE:
if (resultClass.isAssignableFrom(java.sql.Date.class)) {
return;
}
break;
case Types.TIME:
if (resultClass.isAssignableFrom(java.sql.Time.class)) {
return;
}
break;
case Types.TIMESTAMP:
if (resultClass.isAssignableFrom(java.sql.Timestamp.class)) {
return;
}
break;
}
}
}
throwQueryTypeMismatchException(resultClass, sqmExpressible);
}
}
Aggregations