HHH-15288 Switch to SMALLINT as recommended type for enums

This commit is contained in:
Christian Beikov 2022-05-19 15:16:13 +02:00
parent 497c09cddb
commit b7dca12e70
18 changed files with 370 additions and 24 deletions

View File

@ -25,6 +25,7 @@ import org.hibernate.type.AdjustableBasicType;
import org.hibernate.type.BasicType;
import org.hibernate.type.CustomType;
import org.hibernate.type.SerializableType;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.java.BasicPluralJavaType;
import org.hibernate.type.descriptor.java.BasicJavaType;
import org.hibernate.type.descriptor.java.EnumJavaType;
@ -155,6 +156,16 @@ public class InferredBasicValueResolver {
legacyType = jdbcMapping;
}
else if ( explicitJdbcType != null ) {
// we also have an explicit JdbcType
jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(
reflectedJtd,
explicitJdbcType
);
legacyType = jdbcMapping;
}
else {
// see if there is a registered BasicType for this JavaType and, if so, use it.
// this mimics the legacy handling
@ -417,7 +428,7 @@ public class InferredBasicValueResolver {
final JdbcType jdbcType = explicitJdbcType != null
? explicitJdbcType
: typeConfiguration.getJdbcTypeRegistry().getDescriptor( stdIndicators.getPreferredSqlTypeCodeForEnum() );
: typeConfiguration.getJdbcTypeRegistry().getDescriptor( SqlTypes.SMALLINT );
final OrdinalEnumValueConverter<E> valueConverter = new OrdinalEnumValueConverter<>(
enumJavaType,

View File

@ -77,6 +77,31 @@ public class VersionResolution<E> implements BasicValue.Resolution<E> {
}
return context.getBuildingOptions().getDefaultTimeZoneStorage();
}
@Override
public int getPreferredSqlTypeCodeForBoolean() {
return context.getPreferredSqlTypeCodeForBoolean();
}
@Override
public int getPreferredSqlTypeCodeForDuration() {
return context.getPreferredSqlTypeCodeForDuration();
}
@Override
public int getPreferredSqlTypeCodeForUuid() {
return context.getPreferredSqlTypeCodeForUuid();
}
@Override
public int getPreferredSqlTypeCodeForInstant() {
return context.getPreferredSqlTypeCodeForInstant();
}
@Override
public int getPreferredSqlTypeCodeForArray() {
return context.getPreferredSqlTypeCodeForArray();
}
}
);

View File

@ -12,7 +12,6 @@ import java.util.function.Supplier;
import org.hibernate.CustomEntityDirtinessStrategy;
import org.hibernate.EntityNameResolver;
import org.hibernate.Incubating;
import org.hibernate.Interceptor;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.TimeZoneStorageStrategy;

View File

@ -158,6 +158,7 @@ import org.hibernate.type.descriptor.java.BasicJavaType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
import org.hibernate.type.spi.TypeConfiguration;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;
@ -1702,7 +1703,39 @@ public final class AnnotationBinder {
final JavaType<Object> jtd = typeConfiguration.getJavaTypeRegistry().findDescriptor( type );
if ( jtd != null ) {
final JdbcType jdbcType = jtd.getRecommendedJdbcType( typeConfiguration.getCurrentBaseSqlTypeIndicators() );
final JdbcType jdbcType = jtd.getRecommendedJdbcType(
new JdbcTypeIndicators() {
@Override
public TypeConfiguration getTypeConfiguration() {
return typeConfiguration;
}
@Override
public int getPreferredSqlTypeCodeForBoolean() {
return context.getPreferredSqlTypeCodeForBoolean();
}
@Override
public int getPreferredSqlTypeCodeForDuration() {
return context.getPreferredSqlTypeCodeForDuration();
}
@Override
public int getPreferredSqlTypeCodeForUuid() {
return context.getPreferredSqlTypeCodeForUuid();
}
@Override
public int getPreferredSqlTypeCodeForInstant() {
return context.getPreferredSqlTypeCodeForInstant();
}
@Override
public int getPreferredSqlTypeCodeForArray() {
return context.getPreferredSqlTypeCodeForArray();
}
}
);
return typeConfiguration.getBasicTypeRegistry().resolve( jtd, jdbcType );
}

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.internal.util.config;
import java.sql.Types;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

View File

@ -589,8 +589,12 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
}
else if ( basicTypeByName instanceof ConvertedBasicType ) {
final ConvertedBasicType<?> convertedType = (ConvertedBasicType<?>) basicTypeByName;
if ( convertedType.getValueConverter() != null ) {
return new ConvertedBasicTypeResolution<>( convertedType, stdIndicators );
}
valueConverter = null;
domainJtd = basicTypeByName.getJavaTypeDescriptor();
}
else {
valueConverter = null;
domainJtd = basicTypeByName.getJavaTypeDescriptor();

View File

@ -5952,7 +5952,8 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
}
final EnumJavaType<?> enumJtd = sqmEnumLiteral.getExpressibleJavaType();
final JdbcType jdbcType = getTypeConfiguration().getJdbcTypeRegistry().getDescriptor( SqlTypes.TINYINT );
final JdbcType jdbcType = getTypeConfiguration().getJdbcTypeRegistry()
.getDescriptor( SqlTypes.SMALLINT );
final BasicJavaType<Number> relationalJtd = (BasicJavaType) getTypeConfiguration()
.getJavaTypeRegistry()
.getDescriptor( Integer.class );

View File

@ -433,13 +433,13 @@ public class EnumType<T extends Enum<T>>
@Override
public String toString(T value) {
verifyConfigured();
return enumValueConverter.getDomainJavaType().unwrap( value, String.class, null );
return enumValueConverter.getRelationalJavaType().toString( enumValueConverter.toRelationalValue( value ) );
}
@Override
public T fromStringValue(CharSequence sequence) {
verifyConfigured();
return enumValueConverter.getDomainJavaType().wrap( sequence, null );
return enumValueConverter.toDomainValue( enumValueConverter.getRelationalJavaType().fromString( sequence ) );
}
@Override

View File

@ -10,6 +10,7 @@ import java.sql.Types;
import jakarta.persistence.EnumType;
import org.hibernate.dialect.Dialect;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
@ -40,7 +41,7 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
: registry.getDescriptor( Types.VARCHAR );
}
else {
return registry.getDescriptor( Types.TINYINT );
return registry.getDescriptor( SqlTypes.SMALLINT );
}
}

View File

@ -70,7 +70,7 @@ public interface JdbcTypeIndicators {
* {@link JdbcTypeRegistry}.
*/
default int getPreferredSqlTypeCodeForBoolean() {
return getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForBoolean();
return getTypeConfiguration().getCurrentBaseSqlTypeIndicators().getPreferredSqlTypeCodeForBoolean();
}
/**
@ -80,7 +80,7 @@ public interface JdbcTypeIndicators {
* {@link JdbcTypeRegistry}.
*/
default int getPreferredSqlTypeCodeForDuration() {
return getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForDuration();
return getTypeConfiguration().getCurrentBaseSqlTypeIndicators().getPreferredSqlTypeCodeForDuration();
}
/**
@ -90,7 +90,7 @@ public interface JdbcTypeIndicators {
* {@link JdbcTypeRegistry}.
*/
default int getPreferredSqlTypeCodeForUuid() {
return getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForUuid();
return getTypeConfiguration().getCurrentBaseSqlTypeIndicators().getPreferredSqlTypeCodeForUuid();
}
/**
@ -100,7 +100,7 @@ public interface JdbcTypeIndicators {
* {@link JdbcTypeRegistry}.
*/
default int getPreferredSqlTypeCodeForInstant() {
return getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForInstant();
return getTypeConfiguration().getCurrentBaseSqlTypeIndicators().getPreferredSqlTypeCodeForInstant();
}
/**
@ -108,9 +108,10 @@ public interface JdbcTypeIndicators {
* <p/>
* Specifically names the key into the
* {@link JdbcTypeRegistry}.
* @since 6.1
*/
default int getPreferredSqlTypeCodeForArray() {
return getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForArray();
return getTypeConfiguration().getCurrentBaseSqlTypeIndicators().getPreferredSqlTypeCodeForArray();
}
/**
@ -135,7 +136,7 @@ public interface JdbcTypeIndicators {
}
default TimeZoneStorageStrategy getDefaultTimeZoneStorageStrategy() {
return getTypeConfiguration().getSessionFactory().getFastSessionServices().getDefaultTimeZoneStorageStrategy();
return getTypeConfiguration().getCurrentBaseSqlTypeIndicators().getDefaultTimeZoneStorageStrategy();
}
/**

View File

@ -20,6 +20,7 @@ import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.java.MutabilityPlanExposer;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
import org.hibernate.usertype.EnhancedUserType;
import org.hibernate.usertype.UserType;
/**
@ -115,9 +116,20 @@ public class UserTypeJavaTypeWrapper<J> implements BasicJavaType<J> {
@Override
public J fromString(CharSequence string) {
if ( userType instanceof EnhancedUserType<?> ) {
return ( (EnhancedUserType<J>) userType ).fromStringValue( string );
}
throw new UnsupportedOperationException( "No support for parsing UserType values from String: " + userType );
}
@Override
public String toString(J value) {
if ( userType.returnedClass().isInstance( value ) && userType instanceof EnhancedUserType<?> ) {
return ( (EnhancedUserType<J>) userType ).toString( value );
}
return value == null ? "null" : value.toString();
}
@Override
public <X> X unwrap(J value, Class<X> type, WrapperOptions options) {
assert value == null || userType.returnedClass().isInstance( value );

View File

@ -34,6 +34,7 @@ import org.hibernate.HibernateException;
import org.hibernate.Incubating;
import org.hibernate.SessionFactory;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.TimeZoneStorageStrategy;
import org.hibernate.annotations.common.reflection.java.generics.ParameterizedTypeImpl;
import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
@ -352,6 +353,48 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable {
public TypeConfiguration getTypeConfiguration() {
return typeConfiguration;
}
@Override
public TimeZoneStorageStrategy getDefaultTimeZoneStorageStrategy() {
return sessionFactory == null
? getMetadataBuildingContext().getBuildingOptions().getDefaultTimeZoneStorage()
: getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getDefaultTimeZoneStorageStrategy();
}
@Override
public int getPreferredSqlTypeCodeForBoolean() {
return sessionFactory == null
? getMetadataBuildingContext().getPreferredSqlTypeCodeForBoolean()
: getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForBoolean();
}
@Override
public int getPreferredSqlTypeCodeForDuration() {
return sessionFactory == null
? getMetadataBuildingContext().getPreferredSqlTypeCodeForDuration()
: getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForDuration();
}
@Override
public int getPreferredSqlTypeCodeForUuid() {
return sessionFactory == null
? getMetadataBuildingContext().getPreferredSqlTypeCodeForUuid()
: getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForUuid();
}
@Override
public int getPreferredSqlTypeCodeForInstant() {
return sessionFactory == null
? getMetadataBuildingContext().getPreferredSqlTypeCodeForInstant()
: getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForInstant();
}
@Override
public int getPreferredSqlTypeCodeForArray() {
return sessionFactory == null
? getMetadataBuildingContext().getPreferredSqlTypeCodeForArray()
: getTypeConfiguration().getSessionFactory().getSessionFactoryOptions().getPreferredSqlTypeCodeForArray();
}
};
public Scope(TypeConfiguration typeConfiguration) {

View File

@ -78,7 +78,7 @@ public class EnumeratedSmokeTest extends BaseUnitTestCase {
assertThat( hibernateMappingEnumType.isOrdinal(), is(expectedJpaEnumType==EnumType.ORDINAL) );
final int expectedJdbcTypeCode = jdbcRegistry.getDescriptor(
expectedJpaEnumType == EnumType.ORDINAL ?
Types.TINYINT :
Types.SMALLINT :
Types.VARCHAR
).getJdbcTypeCode();
assertThat(

View File

@ -15,6 +15,7 @@ import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
@ -59,7 +60,7 @@ public class EnumResolutionTests {
verifyEnumResolution(
entityBinding.getProperty( "rawEnum" ),
Types.TINYINT,
Types.SMALLINT,
Integer.class,
OrdinalEnumValueConverter.class,
true
@ -74,7 +75,7 @@ public class EnumResolutionTests {
verifyEnumResolution(
entityBinding.getProperty( "unspecifiedMappingEnum" ),
Types.TINYINT,
Types.SMALLINT,
Integer.class,
OrdinalEnumValueConverter.class,
true
@ -89,7 +90,7 @@ public class EnumResolutionTests {
verifyEnumResolution(
entityBinding.getProperty( "ordinalEnum" ),
Types.TINYINT,
Types.SMALLINT,
Integer.class,
OrdinalEnumValueConverter.class,
true
@ -134,6 +135,21 @@ public class EnumResolutionTests {
);
}
@Test
public void testExplicitEnumResolution(DomainModelScope scope) {
final PersistentClass entityBinding = scope
.getDomainModel()
.getEntityBinding( EntityWithEnums.class.getName() );
verifyEnumResolution(
entityBinding.getProperty( "explicitEnum" ),
Types.TINYINT,
Integer.class,
OrdinalEnumValueConverter.class,
true
);
}
@SuppressWarnings("rawtypes")
private void verifyEnumResolution(
Property property,
@ -204,6 +220,10 @@ public class EnumResolutionTests {
@Enumerated( STRING )
private Values namedEnum;
@Enumerated( ORDINAL )
@JdbcTypeCode( Types.TINYINT )
private Values explicitEnum;
}
enum Values { FIRST, SECOND }

View File

@ -96,7 +96,7 @@ public class SmokeTests {
assertThat(
genderAttrMapping.getJdbcMapping().getJdbcType(),
is( jdbcTypeRegistry.getDescriptor( Types.TINYINT ) )
is( jdbcTypeRegistry.getDescriptor( Types.SMALLINT ) )
);
}

View File

@ -0,0 +1,197 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.schemavalidation;
import java.sql.Types;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Map;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.JdbcMetadaAccessStrategy;
import org.hibernate.tool.schema.SourceType;
import org.hibernate.tool.schema.TargetType;
import org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl;
import org.hibernate.tool.schema.spi.ContributableMatcher;
import org.hibernate.tool.schema.spi.ExceptionHandler;
import org.hibernate.tool.schema.spi.ExecutionOptions;
import org.hibernate.tool.schema.spi.SchemaFilter;
import org.hibernate.tool.schema.spi.SchemaManagementTool;
import org.hibernate.tool.schema.spi.ScriptSourceInput;
import org.hibernate.tool.schema.spi.ScriptTargetOutput;
import org.hibernate.tool.schema.spi.SourceDescriptor;
import org.hibernate.tool.schema.spi.TargetDescriptor;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
/**
* Test that an existing tinyint column works even if we switch to smallint type code for enums.
*/
@TestForIssue(jiraKey = "HHH-15288")
@RunWith(Parameterized.class)
public class EnumValidationTest implements ExecutionOptions {
@Parameterized.Parameters
public static Collection<String> parameters() {
return Arrays.asList(
JdbcMetadaAccessStrategy.GROUPED.toString(),
JdbcMetadaAccessStrategy.INDIVIDUALLY.toString()
);
}
@Parameterized.Parameter
public String jdbcMetadataExtractorStrategy;
private StandardServiceRegistry ssr;
private MetadataImplementor metadata;
private MetadataImplementor oldMetadata;
@Before
public void beforeTest() {
ssr = new StandardServiceRegistryBuilder()
.applySetting(
AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY,
jdbcMetadataExtractorStrategy
)
.build();
oldMetadata = (MetadataImplementor) new MetadataSources( ssr )
.addAnnotatedClass( TestEntityOld.class )
.buildMetadata();
oldMetadata.validate();
metadata = (MetadataImplementor) new MetadataSources( ssr )
.addAnnotatedClass( TestEntity.class )
.buildMetadata();
metadata.validate();
try {
dropSchema();
// create the schema
createSchema();
}
catch (Exception e) {
tearDown();
throw e;
}
}
@After
public void tearDown() {
dropSchema();
if ( ssr != null ) {
StandardServiceRegistryBuilder.destroy( ssr );
}
}
@Test
public void testValidation() {
doValidation();
}
private void doValidation() {
ssr.getService( SchemaManagementTool.class ).getSchemaValidator( null )
.doValidation( metadata, this, ContributableMatcher.ALL );
}
private void createSchema() {
ssr.getService( SchemaManagementTool.class ).getSchemaCreator( null ).doCreation(
oldMetadata,
this,
ContributableMatcher.ALL,
new SourceDescriptor() {
@Override
public SourceType getSourceType() {
return SourceType.METADATA;
}
@Override
public ScriptSourceInput getScriptSourceInput() {
return null;
}
},
new TargetDescriptor() {
@Override
public EnumSet<TargetType> getTargetTypes() {
return EnumSet.of( TargetType.DATABASE );
}
@Override
public ScriptTargetOutput getScriptTargetOutput() {
return null;
}
}
);
}
private void dropSchema() {
new SchemaExport()
.drop( EnumSet.of( TargetType.DATABASE ), oldMetadata );
}
@Entity(name = "TestEntity")
public static class TestEntityOld {
@Id
public Integer id;
@Enumerated(EnumType.ORDINAL)
@Column(name = "enumVal")
@JdbcTypeCode(Types.TINYINT)
TestEnum enumVal;
}
@Entity(name = "TestEntity")
public static class TestEntity {
@Id
public Integer id;
@Enumerated(EnumType.ORDINAL)
@Column(name = "enumVal")
TestEnum enumVal;
}
public enum TestEnum {
VALUE1,
VALUE2
}
@Override
public Map getConfigurationValues() {
return ssr.getService( ConfigurationService.class ).getSettings();
}
@Override
public boolean shouldManageNamespaces() {
return false;
}
@Override
public ExceptionHandler getExceptionHandler() {
return ExceptionHandlerLoggedImpl.INSTANCE;
}
@Override
public SchemaFilter getSchemaFilter() {
return SchemaFilter.ALL;
}
}

View File

@ -187,7 +187,7 @@ public class SmokeTests {
assertThat( basicType.getJavaTypeDescriptor().getJavaTypeClass(), AssignableMatcher.assignableTo( Integer.class ) );
assertThat(
basicType.getJdbcType(),
is( jdbcTypeRegistry.getDescriptor( Types.TINYINT ) )
is( jdbcTypeRegistry.getDescriptor( Types.SMALLINT ) )
);

View File

@ -38,14 +38,12 @@ This mapping was not quite correct as Java effectively allows up to 32K enum ent
In practice, this isn't a big issue though for two reasons. A lot of databases do not support a 1 byte integer DDL type,
so Hibernate falls back to the 2+ byte integer type as DDL type. Apart from that, enums in ORM models usually do not exceed the 255 value limit.
Even though a migration is not necessary, schema validation errors could occur as of 6.1 due to the switch to a bigger data type.
Note that the migration is not required as schema validation is able to handle the use of `SMALLINT` when the DDL type is `TINYINT`.
The migration usually requires running only a simple alter command `alter table tbl alter column enum_col smallint`
or `alter table tbl modify column enum_col smallint`, depending on your database dialect.
To retain backwards compatibility, configure the setting `hibernate.type.preferred_enum_jdbc_type` to `TINYINT`.
The following dialects currently have DDL types registered for `TINYINT` and might require migration:
The following dialects currently have DDL types registered for `TINYINT` and might produce a different schema now:
* Cachè
* Ingres