use of org.hibernate.TimeZoneStorageStrategy 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.TimeZoneStorageStrategy 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 org.hibernate.TimeZoneStorageStrategy in project hibernate-orm by hibernate.
the class MetadataBuilderImpl method resolveTimeZoneStorageStrategy.
private static TimeZoneStorageStrategy resolveTimeZoneStorageStrategy(StandardServiceRegistry serviceRegistry, ConfigurationService configService) {
final TimeZoneStorageType configuredTimeZoneStorageType = configService.getSetting(AvailableSettings.TIMEZONE_DEFAULT_STORAGE, TimeZoneStorageType.class, null);
final TimeZoneStorageStrategy resolvedTimezoneStorage;
// For now, we default to NORMALIZE as that is the Hibernate 5.x behavior
if (configuredTimeZoneStorageType == null) {
resolvedTimezoneStorage = TimeZoneStorageStrategy.NORMALIZE;
} else {
final TimeZoneSupport timeZoneSupport = serviceRegistry.getService(JdbcServices.class).getDialect().getTimeZoneSupport();
switch(configuredTimeZoneStorageType) {
case NATIVE:
if (timeZoneSupport != TimeZoneSupport.NATIVE) {
throw new HibernateException("The configured time zone storage type NATIVE is not supported with the configured dialect");
}
resolvedTimezoneStorage = TimeZoneStorageStrategy.NATIVE;
break;
case NORMALIZE:
resolvedTimezoneStorage = TimeZoneStorageStrategy.NORMALIZE;
break;
default:
throw new HibernateException("Unsupported time zone storage type: " + configuredTimeZoneStorageType);
}
}
return resolvedTimezoneStorage;
}
Aggregations