use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class InferredBasicValueResolver method stringEnumValueResolution.
private static <E extends Enum<E>> InferredBasicValueResolution<E, String> stringEnumValueResolution(EnumJavaType<E> enumJavaType, BasicJavaType<?> explicitJavaType, JdbcType explicitJdbcType, JdbcTypeIndicators stdIndicators, TypeConfiguration typeConfiguration) {
final JavaType<String> relationalJtd;
if (explicitJavaType != null) {
if (!String.class.isAssignableFrom(explicitJavaType.getJavaTypeClass())) {
throw new MappingException("Explicit JavaType [" + explicitJavaType + "] applied to enumerated value with EnumType#STRING" + " should handle `java.lang.String` as its relational type descriptor");
}
// noinspection unchecked
relationalJtd = (BasicJavaType<String>) explicitJavaType;
} else {
final boolean useCharacter = stdIndicators.getColumnLength() == 1;
relationalJtd = typeConfiguration.getJavaTypeRegistry().getDescriptor(useCharacter ? Character.class : String.class);
}
final JdbcType jdbcType = explicitJdbcType != null ? explicitJdbcType : relationalJtd.getRecommendedJdbcType(stdIndicators);
final NamedEnumValueConverter<E> valueConverter = new NamedEnumValueConverter<>(enumJavaType, jdbcType, relationalJtd);
return new InferredBasicValueResolution<>(typeConfiguration.getBasicTypeRegistry().resolve(relationalJtd, jdbcType), enumJavaType, relationalJtd, jdbcType, valueConverter, new CustomType<>(new org.hibernate.type.EnumType<>(enumJavaType.getJavaTypeClass(), valueConverter, typeConfiguration), typeConfiguration), ImmutableMutabilityPlan.instance());
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class BasicValueBinder method prepareAnyDiscriminator.
private void prepareAnyDiscriminator(XProperty modelXProperty) {
final AnyDiscriminator anyDiscriminatorAnn = findAnnotation(modelXProperty, AnyDiscriminator.class);
implicitJavaTypeAccess = (typeConfiguration) -> {
if (anyDiscriminatorAnn != null) {
switch(anyDiscriminatorAnn.value()) {
case CHAR:
{
return Character.class;
}
case INTEGER:
{
return Integer.class;
}
default:
{
return String.class;
}
}
}
return String.class;
};
normalJdbcTypeDetails(modelXProperty);
normalMutabilityDetails(modelXProperty);
// layer AnyDiscriminator into the JdbcType resolution
final Function<TypeConfiguration, JdbcType> originalJdbcTypeResolution = explicitJdbcTypeAccess;
this.explicitJdbcTypeAccess = (typeConfiguration) -> {
final JdbcType originalResolution = originalJdbcTypeResolution.apply(typeConfiguration);
if (originalResolution != null) {
return originalResolution;
}
final Class<?> hintedJavaType = (Class<?>) implicitJavaTypeAccess.apply(typeConfiguration);
final JavaType<Object> hintedDescriptor = typeConfiguration.getJavaTypeRegistry().getDescriptor(hintedJavaType);
return hintedDescriptor.getRecommendedJdbcType(typeConfiguration.getCurrentBaseSqlTypeIndicators());
};
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class SimpleValue method buildAttributeConverterTypeAdapter.
private <T> AttributeConverterTypeAdapter<T> buildAttributeConverterTypeAdapter(JpaAttributeConverter<T, ?> jpaAttributeConverter) {
JavaType<T> domainJavaType = jpaAttributeConverter.getDomainJavaType();
JavaType<?> relationalJavaType = jpaAttributeConverter.getRelationalJavaType();
// build the SqlTypeDescriptor adapter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Going back to the illustration, this should be a SqlTypeDescriptor that handles the Integer <-> String
// conversions. This is the more complicated piece. First we need to determine the JDBC type code
// corresponding to the AttributeConverter's declared "databaseColumnJavaType" (how we read that value out
// of ResultSets). See JdbcTypeJavaClassMappings for details. Again, given example, this should return
// VARCHAR/CHAR
final JdbcType recommendedJdbcType = relationalJavaType.getRecommendedJdbcType(// todo (6.0) : handle the other JdbcRecommendedSqlTypeMappingContext methods
new JdbcTypeIndicators() {
@Override
public TypeConfiguration getTypeConfiguration() {
return metadata.getTypeConfiguration();
}
@Override
public TimeZoneStorageStrategy getDefaultTimeZoneStorageStrategy() {
return buildingContext.getBuildingOptions().getDefaultTimeZoneStorage();
}
});
int jdbcTypeCode = recommendedJdbcType.getDefaultSqlTypeCode();
if (isLob()) {
if (LobTypeMappings.isMappedToKnownLobCode(jdbcTypeCode)) {
jdbcTypeCode = LobTypeMappings.getLobCodeTypeMapping(jdbcTypeCode);
} else {
if (Serializable.class.isAssignableFrom(domainJavaType.getJavaTypeClass())) {
jdbcTypeCode = Types.BLOB;
} else {
throw new IllegalArgumentException(String.format(Locale.ROOT, "JDBC type-code [%s (%s)] not known to have a corresponding LOB equivalent, and Java type is not Serializable (to use BLOB)", jdbcTypeCode, JdbcTypeNameMapper.getTypeName(jdbcTypeCode)));
}
}
}
if (isNationalized()) {
jdbcTypeCode = NationalizedTypeMappings.toNationalizedTypeCode(jdbcTypeCode);
}
// todo : cache the AttributeConverterTypeAdapter in case that AttributeConverter is applied multiple times.
return new AttributeConverterTypeAdapter<>(ConverterDescriptor.TYPE_NAME_PREFIX + jpaAttributeConverter.getConverterJavaType().getJavaType().getTypeName(), String.format("BasicType adapter for AttributeConverter<%s,%s>", domainJavaType.getJavaType().getTypeName(), relationalJavaType.getJavaType().getTypeName()), jpaAttributeConverter, // calls into the binding/extraction process...
new AttributeConverterJdbcTypeAdapter(jpaAttributeConverter, metadata.getTypeConfiguration().getJdbcTypeRegistry().getDescriptor(jdbcTypeCode), relationalJavaType), relationalJavaType, domainJavaType, null);
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class AttributeConverterTest method testBasicOrmXmlConverterApplication.
@Test
@TestForIssue(jiraKey = "HHH-8462")
public void testBasicOrmXmlConverterApplication() {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
try {
MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Tester.class).addURL(ConfigHelper.findAsResource("org/hibernate/test/converter/orm.xml")).getMetadataBuilder().build();
final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration().getJdbcTypeRegistry();
PersistentClass tester = metadata.getEntityBinding(Tester.class.getName());
Property nameProp = tester.getProperty("name");
BasicValue nameValue = (BasicValue) nameProp.getValue();
Type type = nameValue.getType();
assertNotNull(type);
assertThat(type, instanceOf(AttributeConverterTypeAdapter.class));
final AttributeConverterTypeAdapter typeAdapter = (AttributeConverterTypeAdapter) type;
assertThat(typeAdapter.getDomainJtd().getJavaTypeClass(), equalTo(String.class));
assertThat(typeAdapter.getRelationalJtd().getJavaTypeClass(), equalTo(Clob.class));
final JdbcType jdbcType = typeAdapter.getJdbcType();
assertThat(jdbcType, is(jdbcTypeRegistry.getDescriptor(Types.CLOB)));
} finally {
StandardServiceRegistryBuilder.destroy(ssr);
}
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class Dialect method getCastTypeName.
/**
* Get the name of the database type appropriate for casting operations
* (via the CAST() SQL function) for the given {@link SqlExpressible}
* SQL type.
*
* @return The database type name
*/
public String getCastTypeName(SqlExpressible type, Long length, Integer precision, Integer scale) {
final JdbcMapping jdbcMapping = type.getJdbcMapping();
final JdbcType jdbcType = jdbcMapping.getJdbcType();
final JavaType<?> javaType = jdbcMapping.getJavaTypeDescriptor();
Size size;
if (length == null && precision == null) {
return getUnboundedTypeName(jdbcType, javaType);
} else {
// use the given length/precision/scale
if (precision != null && scale == null) {
// needed for cast(x as BigInteger(p))
scale = javaType.getDefaultSqlScale(Dialect.this, jdbcType);
}
size = new Size().setLength(length).setPrecision(precision).setScale(scale);
}
return getTypeName(jdbcType, size);
}
Aggregations