diff --git a/hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java b/hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java index dcf0378b2b..b4e3d37a63 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/AbstractStandardBasicType.java @@ -395,6 +395,7 @@ public abstract class AbstractStandardBasicType } break; case Types.CHAR: + case Types.NCHAR: if ( getJavaType() == Boolean.class ) { return (Boolean) getJavaTypeDescriptor().wrap( 'Y', null ) ? CastType.YN_BOOLEAN diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embeddables/nested/NestedEmbeddableMetadataTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embeddables/nested/NestedEmbeddableMetadataTest.java index d4b6abbb3f..5a947581a5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embeddables/nested/NestedEmbeddableMetadataTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/embeddables/nested/NestedEmbeddableMetadataTest.java @@ -21,6 +21,7 @@ import org.hibernate.mapping.Property; import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.Value; import org.hibernate.type.CustomType; +import org.hibernate.type.spi.TypeConfiguration; import org.junit.jupiter.api.Test; @@ -43,6 +44,7 @@ public class NestedEmbeddableMetadataTest { final Metadata metadata = new MetadataSources( serviceRegistry ) .addAnnotatedClass( Customer.class ) .buildMetadata(); + final TypeConfiguration typeConfiguration = metadata.getDatabase().getTypeConfiguration(); PersistentClass classMetadata = metadata.getEntityBinding( Customer.class.getName() ); Property investmentsProperty = classMetadata.getProperty( "investments" ); @@ -57,7 +59,10 @@ public class NestedEmbeddableMetadataTest { CustomType currencyType = (CustomType) currencyMetadata.getType(); int[] currencySqlTypes = currencyType.getSqlTypeCodes( metadata ); assertEquals( 1, currencySqlTypes.length ); - assertJdbcTypeCode( Types.VARCHAR, currencySqlTypes[0] ); + assertJdbcTypeCode( + typeConfiguration.getJdbcTypeDescriptorRegistry().getDescriptor( Types.VARCHAR ).getJdbcTypeCode(), + currencySqlTypes[0] + ); } finally { StandardServiceRegistryBuilder.destroy( serviceRegistry ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/EnumeratedSmokeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/EnumeratedSmokeTest.java index 38f93c19b4..e3dcef3c0f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/EnumeratedSmokeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/enumerated/EnumeratedSmokeTest.java @@ -19,6 +19,8 @@ import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Property; import org.hibernate.type.CustomType; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; +import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseUnitTestCase; @@ -60,22 +62,31 @@ public class EnumeratedSmokeTest extends BaseUnitTestCase { .buildMetadata(); mappings.validate(); + final JdbcTypeRegistry jdbcTypeRegistry = mappings.getTypeConfiguration().getJdbcTypeDescriptorRegistry(); final PersistentClass entityBinding = mappings.getEntityBinding( EntityWithEnumeratedAttributes.class.getName() ); - validateEnumMapping( entityBinding.getProperty( "notAnnotated" ), EnumType.ORDINAL ); - validateEnumMapping( entityBinding.getProperty( "noEnumType" ), EnumType.ORDINAL ); - validateEnumMapping( entityBinding.getProperty( "ordinalEnumType" ), EnumType.ORDINAL ); - validateEnumMapping( entityBinding.getProperty( "stringEnumType" ), EnumType.STRING ); + validateEnumMapping( jdbcTypeRegistry, entityBinding.getProperty( "notAnnotated" ), EnumType.ORDINAL ); + validateEnumMapping( jdbcTypeRegistry, entityBinding.getProperty( "noEnumType" ), EnumType.ORDINAL ); + validateEnumMapping( jdbcTypeRegistry, entityBinding.getProperty( "ordinalEnumType" ), EnumType.ORDINAL ); + validateEnumMapping( jdbcTypeRegistry, entityBinding.getProperty( "stringEnumType" ), EnumType.STRING ); } - private void validateEnumMapping(Property property, EnumType expectedJpaEnumType) { + private void validateEnumMapping(JdbcTypeRegistry jdbcRegistry, Property property, EnumType expectedJpaEnumType) { assertThat( property.getType(), instanceOf( CustomType.class ) ); final CustomType customType = (CustomType) property.getType(); assertThat( customType.getUserType(), instanceOf( org.hibernate.type.EnumType.class ) ); final org.hibernate.type.EnumType hibernateMappingEnumType = (org.hibernate.type.EnumType) customType.getUserType(); assertThat( hibernateMappingEnumType.isOrdinal(), is(expectedJpaEnumType==EnumType.ORDINAL) ); assertThat( hibernateMappingEnumType.sqlTypes().length, is(1) ); - assertThat( hibernateMappingEnumType.sqlTypes()[0], is(expectedJpaEnumType==EnumType.ORDINAL? Types.TINYINT:Types.VARCHAR) ); + final int expectedJdbcTypeCode = jdbcRegistry.getDescriptor( + expectedJpaEnumType == EnumType.ORDINAL ? + Types.TINYINT : + Types.VARCHAR + ).getJdbcTypeCode(); + assertThat( + hibernateMappingEnumType.sqlTypes()[0], + is( expectedJdbcTypeCode ) + ); } @Entity diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/BaseInsertOrderingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/BaseInsertOrderingTest.java index b87b9afb5b..90a0390fe6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/BaseInsertOrderingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/BaseInsertOrderingTest.java @@ -2,11 +2,15 @@ package org.hibernate.orm.test.insertordering; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.sql.Types; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; +import org.hibernate.type.descriptor.java.StringJavaTypeDescriptor; +import org.hibernate.type.descriptor.jdbc.JdbcType; + import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider; import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest; @@ -59,6 +63,18 @@ abstract class BaseInsertOrderingTest extends BaseSessionFactoryFunctionalTest { connectionProvider.stop(); } + protected String literal(String value) { + final JdbcType jdbcType = sessionFactory().getTypeConfiguration().getJdbcTypeDescriptorRegistry().getDescriptor( + Types.VARCHAR + ); + return jdbcType.getJdbcLiteralFormatter( StringJavaTypeDescriptor.INSTANCE ) + .toJdbcLiteral( + value, + sessionFactory().getJdbcServices().getDialect(), + sessionFactory().getWrapperOptions() + ); + } + void verifyContainsBatches(Batch... expectedBatches) { for ( Batch expectedBatch : expectedBatches ) { PreparedStatement preparedStatement = connectionProvider.getPreparedStatement( expectedBatch.sql ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/InsertOrderingSelfReferenceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/InsertOrderingSelfReferenceTest.java index 895a3cb12c..44fbced254 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/InsertOrderingSelfReferenceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/InsertOrderingSelfReferenceTest.java @@ -1,5 +1,6 @@ package org.hibernate.orm.test.insertordering; +import java.sql.Types; import java.util.ArrayList; import java.util.List; import jakarta.persistence.CascadeType; @@ -21,6 +22,8 @@ import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.SortNatural; import org.hibernate.annotations.Where; +import org.hibernate.type.descriptor.java.StringJavaTypeDescriptor; +import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.testing.TestForIssue; import org.junit.jupiter.api.Test; @@ -70,8 +73,8 @@ public class InsertOrderingSelfReferenceTest extends BaseInsertOrderingTest { verifyContainsBatches( new Batch( "insert into Placeholder (name, id) values (?, ?)", 2 ), - new Batch( "insert into Parameter (name, parent_id, TYPE, id) values (?, ?, 'INPUT', ?)" ), - new Batch( "insert into Parameter (name, parent_id, TYPE, id) values (?, ?, 'OUTPUT', ?)", 3 ) + new Batch( "insert into Parameter (name, parent_id, TYPE, id) values (?, ?, " + literal( "INPUT" ) + ", ?)" ), + new Batch( "insert into Parameter (name, parent_id, TYPE, id) values (?, ?, " + literal( "OUTPUT" ) + ", ?)", 3 ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/InsertOrderingWithSecondaryTable.java b/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/InsertOrderingWithSecondaryTable.java index 9242da24c5..8c210c661d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/InsertOrderingWithSecondaryTable.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/insertordering/InsertOrderingWithSecondaryTable.java @@ -73,8 +73,8 @@ public class InsertOrderingWithSecondaryTable extends BaseInsertOrderingTest { verifyContainsBatches( new Batch( "insert into TOP_LEVEL (name, id) values (?, ?)" ), - new Batch( "insert into SHAPE (name, SHAPE_TYPE, SHAPE_ID) values (?, 'POLYGON', ?)" ), - new Batch( "insert into SHAPE (name, SHAPE_TYPE, SHAPE_ID) values (?, 'CIRCLE', ?)" ), + new Batch( "insert into SHAPE (name, SHAPE_TYPE, SHAPE_ID) values (?, " + literal( "POLYGON" ) + ", ?)" ), + new Batch( "insert into SHAPE (name, SHAPE_TYPE, SHAPE_ID) values (?, " + literal( "CIRCLE" ) + ", ?)" ), new Batch( "insert into SHAPE_CIRCLE (centre, SHAPE_ID) values (?, ?)" ), new Batch( "insert into GEOGRAPHIC_AREA (name, SHAPE_ID, TOP_LEVEL_ID, id) values (?, ?, ?, ?)", 2 ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/SimpleAttributeBinderTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/SimpleAttributeBinderTests.java index 9abe908dfc..06719f0101 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/SimpleAttributeBinderTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/attributebinder/SimpleAttributeBinderTests.java @@ -10,6 +10,8 @@ import java.sql.Types; import org.hibernate.mapping.BasicValue; import org.hibernate.mapping.Property; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; +import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.DomainModelScope; @@ -31,10 +33,14 @@ public class SimpleAttributeBinderTests { assertThat( activeMapping.getJpaAttributeConverterDescriptor() ).isNotNull(); final BasicValue.Resolution resolution = activeMapping.resolve(); + final JdbcTypeRegistry jdbcTypeRegistry = activeMapping.getBuildingContext() + .getBuildingOptions() + .getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); assertThat( resolution.getDomainJavaDescriptor().getJavaType() ).isEqualTo( Boolean.class ); assertThat( resolution.getRelationalJavaDescriptor().getJavaType() ).isEqualTo( Character.class ); - assertThat( resolution.getJdbcTypeDescriptor().getJdbcTypeCode() ).isEqualTo( Types.CHAR ); + assertThat( resolution.getJdbcTypeDescriptor() ).isEqualTo( jdbcTypeRegistry.getDescriptor( Types.CHAR ) ); assertThat( resolution.getValueConverter() ).isNotNull(); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/AttributeConverterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/AttributeConverterTest.java index 3222d51592..351f2ab9c1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/AttributeConverterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/AttributeConverterTest.java @@ -19,6 +19,7 @@ import org.hibernate.boot.internal.ClassmateContext; import org.hibernate.boot.model.convert.internal.InstanceBasedConverterDescriptor; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataBuildingContext; import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Configuration; @@ -37,6 +38,8 @@ import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; import org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor; import org.hibernate.type.descriptor.java.StringJavaTypeDescriptor; import org.hibernate.type.descriptor.jdbc.JdbcType; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; +import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.boot.MetadataBuildingContextTestingImpl; @@ -110,7 +113,11 @@ public class AttributeConverterTest extends BaseUnitTestCase { @Test public void testBasicOperation() { try ( StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().build()) { - final BasicValue basicValue = new BasicValue( new MetadataBuildingContextTestingImpl( serviceRegistry ) ); + final MetadataBuildingContext buildingContext = new MetadataBuildingContextTestingImpl( serviceRegistry ); + final JdbcTypeRegistry jdbcTypeRegistry = buildingContext.getBootstrapContext() + .getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); + final BasicValue basicValue = new BasicValue( buildingContext ); basicValue.setJpaAttributeConverterDescriptor( new InstanceBasedConverterDescriptor( new StringClobConverter(), @@ -128,7 +135,7 @@ public class AttributeConverterTest extends BaseUnitTestCase { assertThat( typeAdapter.getDomainJtd().getJavaTypeClass(), equalTo( String.class ) ); final JdbcType jdbcType = typeAdapter.getJdbcTypeDescriptor(); - assertThat( jdbcType.getJdbcTypeCode(), is( Types.CLOB ) ); + assertThat( jdbcType, is( jdbcTypeRegistry.getDescriptor( Types.CLOB ) ) ); } } @@ -168,6 +175,8 @@ public class AttributeConverterTest extends BaseUnitTestCase { .getMetadataBuilder() .applyAttributeConverter( StringClobConverter.class, true ) .build(); + final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); final PersistentClass tester = metadata.getEntityBinding( Tester.class.getName() ); final Property nameProp = tester.getProperty( "name" ); @@ -180,7 +189,7 @@ public class AttributeConverterTest extends BaseUnitTestCase { final AttributeConverterTypeAdapter typeAdapter = (AttributeConverterTypeAdapter) type; assertThat( typeAdapter.getDomainJtd().getJavaTypeClass(), Matchers.equalTo( String.class ) ); final JdbcType jdbcType = typeAdapter.getJdbcTypeDescriptor(); - assertThat( jdbcType.getJdbcTypeCode(), is( Types.CLOB ) ); + assertThat( jdbcType, is( jdbcTypeRegistry.getDescriptor( Types.CLOB ) ) ); } finally { StandardServiceRegistryBuilder.destroy( ssr ); @@ -198,6 +207,8 @@ public class AttributeConverterTest extends BaseUnitTestCase { .addURL( ConfigHelper.findAsResource( "org/hibernate/test/converter/orm.xml" ) ) .getMetadataBuilder() .build(); + final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); PersistentClass tester = metadata.getEntityBinding( Tester.class.getName() ); Property nameProp = tester.getProperty( "name" ); @@ -212,7 +223,7 @@ public class AttributeConverterTest extends BaseUnitTestCase { assertThat( typeAdapter.getRelationalJtd().getJavaTypeClass(), equalTo( Clob.class ) ); final JdbcType jdbcType = typeAdapter.getJdbcTypeDescriptor(); - assertThat( jdbcType.getJdbcTypeCode(), is( Types.CLOB ) ); + assertThat( jdbcType, is( jdbcTypeRegistry.getDescriptor( Types.CLOB ) ) ); } finally { StandardServiceRegistryBuilder.destroy( ssr ); @@ -230,6 +241,8 @@ public class AttributeConverterTest extends BaseUnitTestCase { .addURL( ConfigHelper.findAsResource( "org/hibernate/test/converter/package.xml" ) ) .getMetadataBuilder() .build(); + final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); PersistentClass tester = metadata.getEntityBinding( Tester.class.getName() ); Property nameProp = tester.getProperty( "name" ); @@ -246,7 +259,7 @@ public class AttributeConverterTest extends BaseUnitTestCase { assertThat( typeAdapter.getRelationalJtd().getJavaTypeClass(), equalTo( Clob.class ) ); final JdbcType sqlTypeDescriptor = typeAdapter.getJdbcTypeDescriptor(); - assertThat( sqlTypeDescriptor.getJdbcTypeCode(), is( Types.CLOB ) ); + assertThat( sqlTypeDescriptor, is( jdbcTypeRegistry.getDescriptor( Types.CLOB ) ) ); } finally { StandardServiceRegistryBuilder.destroy( ssr ); @@ -264,6 +277,8 @@ public class AttributeConverterTest extends BaseUnitTestCase { .getMetadataBuilder() .applyAttributeConverter( StringClobConverter.class, true ) .build(); + final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); PersistentClass tester = metadata.getEntityBinding( Tester2.class.getName() ); Property nameProp = tester.getProperty( "name" ); @@ -275,7 +290,7 @@ public class AttributeConverterTest extends BaseUnitTestCase { } AbstractStandardBasicType basicType = assertTyping( AbstractStandardBasicType.class, type ); assertSame( StringJavaTypeDescriptor.INSTANCE, basicType.getJavaTypeDescriptor() ); - assertEquals( Types.VARCHAR, basicType.getJdbcTypeDescriptor().getJdbcTypeCode() ); + assertEquals( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ), basicType.getJdbcTypeDescriptor() ); } finally { StandardServiceRegistryBuilder.destroy( ssr ); @@ -333,6 +348,8 @@ public class AttributeConverterTest extends BaseUnitTestCase { .getMetadataBuilder() .applyAttributeConverter( IntegerToVarcharConverter.class, true ) .build(); + final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); final PersistentClass tester = metadata.getEntityBinding( Tester5.class.getName() ); final Property codeProp = tester.getProperty( "code" ); @@ -345,7 +362,7 @@ public class AttributeConverterTest extends BaseUnitTestCase { assertThat( typeAdapter.getDomainJtd().getJavaTypeClass(), equalTo( Integer.class ) ); assertThat( typeAdapter.getRelationalJtd().getJavaTypeClass(), equalTo( String.class ) ); - assertThat( typeAdapter.getJdbcTypeDescriptor().getJdbcTypeCode(), is( Types.VARCHAR ) ); + assertThat( typeAdapter.getJdbcTypeDescriptor(), is( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ) ) ); } finally { StandardServiceRegistryBuilder.destroy( ssr ); @@ -437,6 +454,8 @@ public class AttributeConverterTest extends BaseUnitTestCase { .getMetadataBuilder() .applyAttributeConverter( ConvertibleEnumConverter.class, true ) .build(); + final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); // first lets validate that the converter was applied... final PersistentClass tester = metadata.getEntityBinding( EntityWithConvertibleField.class.getName() ); @@ -459,7 +478,7 @@ public class AttributeConverterTest extends BaseUnitTestCase { else { expectedJdbcTypeCode = Types.VARCHAR; } - assertThat( typeAdapter.getJdbcTypeDescriptor().getJdbcTypeCode(), is( expectedJdbcTypeCode ) ); + assertThat( typeAdapter.getJdbcTypeDescriptor(), is( jdbcTypeRegistry.getDescriptor( expectedJdbcTypeCode ) ) ); // then lets build the SF and verify its use... final SessionFactory sf = metadata.buildSessionFactory(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleEmbeddableOverriddenConverterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleEmbeddableOverriddenConverterTest.java index 05dc8ab1e2..ef08f5195c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleEmbeddableOverriddenConverterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleEmbeddableOverriddenConverterTest.java @@ -6,6 +6,8 @@ */ package org.hibernate.orm.test.mapping.converted.converter; +import java.sql.Types; + import jakarta.persistence.Convert; import jakarta.persistence.Embeddable; import jakarta.persistence.Embedded; @@ -18,6 +20,7 @@ import org.hibernate.type.CompositeType; import org.hibernate.type.Type; import org.hibernate.type.descriptor.java.StringJavaTypeDescriptor; import org.hibernate.type.descriptor.jdbc.VarcharJdbcType; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; @@ -46,10 +49,12 @@ public class SimpleEmbeddableOverriddenConverterTest extends BaseNonConfigCoreFu @Test public void testSimpleConvertOverrides() { final EntityPersister ep = sessionFactory().getEntityPersister( Person.class.getName() ); + final JdbcTypeRegistry jdbcTypeRegistry = sessionFactory().getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); CompositeType homeAddressType = assertTyping( CompositeType.class, ep.getPropertyType( "homeAddress" ) ); BasicType homeAddressCityType = (BasicType) findCompositeAttributeType( homeAddressType, "city" ); assertTyping( StringJavaTypeDescriptor.class, homeAddressCityType.getJavaTypeDescriptor() ); - assertTyping( VarcharJdbcType.class, homeAddressCityType.getJdbcTypeDescriptor() ); + assertTyping( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ).getClass(), homeAddressCityType.getJdbcTypeDescriptor() ); } public Type findCompositeAttributeType(CompositeType compositeType, String attributeName) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleOverriddenConverterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleOverriddenConverterTest.java index e7637b0701..ca0f912c27 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleOverriddenConverterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleOverriddenConverterTest.java @@ -6,6 +6,8 @@ */ package org.hibernate.orm.test.mapping.converted.converter; +import java.sql.Types; + import jakarta.persistence.Convert; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -15,6 +17,7 @@ import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.BasicType; import org.hibernate.type.descriptor.java.StringJavaTypeDescriptor; import org.hibernate.type.descriptor.jdbc.VarcharJdbcType; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; @@ -43,10 +46,12 @@ public class SimpleOverriddenConverterTest extends BaseNonConfigCoreFunctionalTe @Test public void testSimpleConvertOverrides() { final EntityPersister ep = sessionFactory().getEntityPersister( Sub.class.getName() ); + final JdbcTypeRegistry jdbcTypeRegistry = sessionFactory().getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); BasicType type = (BasicType) ep.getPropertyType( "it" ); assertTyping( StringJavaTypeDescriptor.class, type.getJavaTypeDescriptor() ); - assertTyping( VarcharJdbcType.class, type.getJdbcTypeDescriptor() ); + assertTyping( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ).getClass(), type.getJdbcTypeDescriptor() ); } @MappedSuperclass diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleXmlOverriddenTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleXmlOverriddenTest.java index b2a81bc655..0a3ec874ee 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleXmlOverriddenTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/SimpleXmlOverriddenTest.java @@ -6,6 +6,8 @@ */ package org.hibernate.orm.test.mapping.converted.converter; +import java.sql.Types; + import jakarta.persistence.Convert; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -20,6 +22,7 @@ import org.hibernate.type.Type; import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; import org.hibernate.type.descriptor.java.StringJavaTypeDescriptor; import org.hibernate.type.descriptor.jdbc.VarcharJdbcType; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.testing.junit4.BaseUnitTestCase; import org.junit.After; @@ -74,11 +77,13 @@ public class SimpleXmlOverriddenTest extends BaseUnitTestCase { .addAnnotatedClass( TheEntity.class ) .addResource( "org/hibernate/test/converter/simple-override.xml" ) .buildMetadata(); + final JdbcTypeRegistry jdbcTypeRegistry = metadata.getDatabase().getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); PersistentClass pc = metadata.getEntityBinding( TheEntity.class.getName() ); BasicType type = (BasicType) pc.getProperty( "it" ).getType(); assertTyping( StringJavaTypeDescriptor.class, type.getJavaTypeDescriptor() ); - assertTyping( VarcharJdbcType.class, type.getJdbcTypeDescriptor() ); + assertTyping( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ).getClass(), type.getJdbcTypeDescriptor() ); } /** @@ -91,11 +96,13 @@ public class SimpleXmlOverriddenTest extends BaseUnitTestCase { .addAnnotatedClass( TheEntity2.class ) .addResource( "org/hibernate/test/converter/simple-override2.xml" ) .buildMetadata(); + final JdbcTypeRegistry jdbcTypeRegistry = metadata.getDatabase().getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); PersistentClass pc = metadata.getEntityBinding( TheEntity2.class.getName() ); BasicType type = (BasicType) pc.getProperty( "it" ).getType(); assertTyping( StringJavaTypeDescriptor.class, type.getJavaTypeDescriptor() ); - assertTyping( VarcharJdbcType.class, type.getJdbcTypeDescriptor() ); + assertTyping( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ).getClass(), type.getJdbcTypeDescriptor() ); } @Entity(name="TheEntity") diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/custom/CustomTypeConverterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/custom/CustomTypeConverterTest.java index 702dee3504..8539f43a81 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/custom/CustomTypeConverterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/custom/CustomTypeConverterTest.java @@ -6,19 +6,28 @@ */ package org.hibernate.orm.test.mapping.converted.converter.custom; +import java.util.List; + import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.BootstrapServiceRegistry; +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.InFlightMetadataCollector; import org.hibernate.boot.spi.MetadataBuilderImplementor; +import org.hibernate.boot.spi.MetadataContributor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.tool.schema.Action; import org.hibernate.type.spi.TypeConfiguration; +import org.hibernate.testing.boot.ExtraJavaServicesClassLoaderService; import org.hibernate.testing.junit4.BaseUnitTestCase; import org.junit.Test; +import org.jboss.jandex.IndexView; + import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.MatcherAssert.assertThat; @@ -28,8 +37,16 @@ import static org.hamcrest.MatcherAssert.assertThat; public class CustomTypeConverterTest extends BaseUnitTestCase { @Test public void testConverterAppliedScopedRegistration() { - - try ( final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() + final List> services = List.of( + new ExtraJavaServicesClassLoaderService.JavaServiceDescriptor<>( + MetadataContributor.class, + PayloadWrapperMetadataContributor.class + ) + ); + final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().enableAutoClose() + .applyClassLoaderService( new ExtraJavaServicesClassLoaderService( services ) ) + .build(); + try ( final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder( bsr ) .applySetting( AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP ) .build() ) { final MetadataSources metadataSources = new MetadataSources( ssr ) @@ -39,10 +56,6 @@ public class CustomTypeConverterTest extends BaseUnitTestCase { // now the new scoped way final TypeConfiguration bootTypeConfiguration = metadataBuilder.getBootstrapContext().getTypeConfiguration(); - bootTypeConfiguration.getJavaTypeDescriptorRegistry() - .addDescriptor( PayloadWrapperJavaType.INSTANCE ); - bootTypeConfiguration.getJdbcTypeDescriptorRegistry() - .addDescriptor( PayloadWrapperJdbcType.INSTANCE ); performAssertions( metadataBuilder, bootTypeConfiguration ); } @@ -71,4 +84,15 @@ public class CustomTypeConverterTest extends BaseUnitTestCase { entityPersister.getPropertyType( "customType" ); } } + + public static class PayloadWrapperMetadataContributor implements MetadataContributor { + @Override + public void contribute(InFlightMetadataCollector metadataCollector, IndexView jandexIndex) { + final TypeConfiguration typeConfiguration = metadataCollector.getTypeConfiguration(); + typeConfiguration.getJavaTypeDescriptorRegistry() + .addDescriptor( PayloadWrapperJavaType.INSTANCE ); + typeConfiguration.getJdbcTypeDescriptorRegistry() + .addDescriptor( PayloadWrapperJdbcType.INSTANCE ); + } + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/formula/FormulaFromHbmTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/formula/FormulaFromHbmTests.java index c2b0ef2886..13369ae7f4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/formula/FormulaFromHbmTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/formula/FormulaFromHbmTests.java @@ -14,6 +14,7 @@ import org.hibernate.mapping.BasicValue; import org.hibernate.mapping.Formula; import org.hibernate.mapping.Property; import org.hibernate.mapping.Selectable; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.DomainModelScope; @@ -37,11 +38,14 @@ public class FormulaFromHbmTests { scope.withHierarchy( EntityOfFormulas.class, (rootClass) -> { + final JdbcTypeRegistry jdbcTypeRegistry = scope.getDomainModel() + .getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); final Property stringFormula = rootClass.getProperty( "stringFormula" ); { final int[] sqlTypes = stringFormula.getType().getSqlTypeCodes( scope.getDomainModel() ); assertThat( sqlTypes.length, is( 1 ) ); - assertThat( sqlTypes[ 0 ], is( Types.VARCHAR ) ); + assertThat( sqlTypes[ 0 ], is( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ).getJdbcTypeCode() ) ); final Selectable selectable = ( (BasicValue) stringFormula.getValue() ).getColumn(); assertThat( selectable, instanceOf( Formula.class ) ); @@ -51,7 +55,7 @@ public class FormulaFromHbmTests { { final int[] sqlTypes = integerFormula.getType().getSqlTypeCodes( scope.getDomainModel() ); assertThat( sqlTypes.length, is( 1 ) ); - assertThat( sqlTypes[ 0 ], is( Types.INTEGER ) ); + assertThat( sqlTypes[ 0 ], is( jdbcTypeRegistry.getDescriptor( Types.INTEGER ).getJdbcTypeCode() ) ); final Selectable selectable = ( (BasicValue) integerFormula.getValue() ).getColumn(); assertThat( selectable, instanceOf( Formula.class ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/ZoneMappingTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/ZoneMappingTests.java index efb0af9baa..91f0bde48e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/ZoneMappingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/ZoneMappingTests.java @@ -16,6 +16,7 @@ import java.util.Set; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping; import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; @@ -43,16 +44,17 @@ public class ZoneMappingTests { public void basicAssertions(SessionFactoryScope scope) { final SessionFactoryImplementor sessionFactory = scope.getSessionFactory(); final EntityPersister entityDescriptor = sessionFactory.getMetamodel().entityPersister( ZoneMappingTestEntity.class ); + final JdbcTypeRegistry jdbcTypeRegistry = sessionFactory.getTypeConfiguration().getJdbcTypeDescriptorRegistry(); { final BasicAttributeMapping zoneIdAttribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping( "zoneId" ); - assertThat( zoneIdAttribute.getJdbcMapping().getJdbcTypeDescriptor().getJdbcTypeCode() ).isEqualTo( Types.VARCHAR ); + assertThat( zoneIdAttribute.getJdbcMapping().getJdbcTypeDescriptor() ).isEqualTo( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ) ); assertThat( zoneIdAttribute.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass() ).isEqualTo( ZoneId.class ); } { final BasicAttributeMapping zoneOffsetAttribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping( "zoneOffset" ); - assertThat( zoneOffsetAttribute.getJdbcMapping().getJdbcTypeDescriptor().getJdbcTypeCode() ).isEqualTo( Types.VARCHAR ); + assertThat( zoneOffsetAttribute.getJdbcMapping().getJdbcTypeDescriptor() ).isEqualTo( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ) ); assertThat( zoneOffsetAttribute.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass() ).isEqualTo( ZoneOffset.class ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/QueryTimeOutTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/QueryTimeOutTest.java index 66c9c6927a..803d42b756 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/QueryTimeOutTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/QueryTimeOutTest.java @@ -7,6 +7,7 @@ package org.hibernate.orm.test.query; import java.sql.SQLException; +import java.sql.Types; import java.util.Map; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -18,6 +19,8 @@ import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.jpa.QueryHints; import org.hibernate.query.NativeQuery; import org.hibernate.query.Query; +import org.hibernate.type.descriptor.java.StringJavaTypeDescriptor; +import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; @@ -44,6 +47,8 @@ public class QueryTimeOutTest extends BaseNonConfigCoreFunctionalTestCase { ); private static final String QUERY = "update AnEntity set name='abc'"; + private String expectedSqlQuery; + @Override protected Class[] getAnnotatedClasses() { return new Class[] { AnEntity.class }; @@ -58,6 +63,15 @@ public class QueryTimeOutTest extends BaseNonConfigCoreFunctionalTestCase { @Before public void before() { CONNECTION_PROVIDER.clear(); + final JdbcType jdbcType = sessionFactory().getTypeConfiguration().getJdbcTypeDescriptorRegistry().getDescriptor( + Types.VARCHAR + ); + expectedSqlQuery = "update AnEntity set name=" + jdbcType.getJdbcLiteralFormatter( StringJavaTypeDescriptor.INSTANCE ) + .toJdbcLiteral( + "abc", + sessionFactory().getJdbcServices().getDialect(), + sessionFactory().getWrapperOptions() + ); } @Test @@ -79,7 +93,7 @@ public class QueryTimeOutTest extends BaseNonConfigCoreFunctionalTestCase { } else { verify( - CONNECTION_PROVIDER.getPreparedStatement( QUERY ), + CONNECTION_PROVIDER.getPreparedStatement( expectedSqlQuery ), times( 1 ) ).setQueryTimeout( 123 ); } @@ -110,7 +124,7 @@ public class QueryTimeOutTest extends BaseNonConfigCoreFunctionalTestCase { } else { verify( - CONNECTION_PROVIDER.getPreparedStatement( QUERY ), + CONNECTION_PROVIDER.getPreparedStatement( expectedSqlQuery ), times( 1 ) ).setQueryTimeout( 123 ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/NativeQueryResultBuilderTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/NativeQueryResultBuilderTests.java index 10a7030ddb..7162cb927a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/NativeQueryResultBuilderTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/NativeQueryResultBuilderTests.java @@ -23,6 +23,7 @@ import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping; import org.hibernate.metamodel.model.convert.spi.BasicValueConverter; import org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter; import org.hibernate.query.sql.spi.NativeQueryImplementor; +import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.testing.orm.domain.StandardDomainModel; import org.hibernate.testing.orm.domain.gambit.EntityOfBasics; @@ -283,7 +284,9 @@ public class NativeQueryResultBuilderTests { final EntityMappingType entityDescriptor = scope.getSessionFactory() .getRuntimeMetamodels() .getEntityMappingType( EntityOfBasics.class ); - + final JdbcTypeRegistry jdbcTypeRegistry = scope.getSessionFactory() + .getTypeConfiguration() + .getJdbcTypeDescriptorRegistry(); final ModelPart part = entityDescriptor.findSubPart( "convertedGender", null ); assertThat( part, instanceOf( BasicAttributeMapping.class ) ); final BasicAttributeMapping attrMapping = (BasicAttributeMapping) part; @@ -295,7 +298,7 @@ public class NativeQueryResultBuilderTests { assertThat( valueConverter.getDomainJavaDescriptor(), is( attrMapping.getJavaTypeDescriptor() ) ); assertThat( valueConverter.getRelationalJavaDescriptor().getJavaTypeClass(), equalTo( Character.class ) ); - assertThat( attrMapping.getJdbcMapping().getJdbcTypeDescriptor().getJdbcTypeCode(), is( Types.CHAR ) ); + assertThat( attrMapping.getJdbcMapping().getJdbcTypeDescriptor(), is( jdbcTypeRegistry.getDescriptor( Types.CHAR ) ) ); } @BeforeEach diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/HANASchemaMigrationTargetScriptCreationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/HANASchemaMigrationTargetScriptCreationTest.java index 213f25cd10..740ed3a243 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/HANASchemaMigrationTargetScriptCreationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/HANASchemaMigrationTargetScriptCreationTest.java @@ -22,6 +22,7 @@ import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.dialect.AbstractHANADialect; +import org.hibernate.dialect.Dialect; import org.hibernate.service.ServiceRegistry; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.tool.schema.TargetType; @@ -68,8 +69,14 @@ public class HANASchemaMigrationTargetScriptCreationTest extends BaseCoreFunctio this.output.deleteOnExit(); configuration.setProperty( Environment.HBM2DDL_SCRIPTS_ACTION, "create" ); configuration.setProperty( Environment.HBM2DDL_SCRIPTS_CREATE_TARGET, this.output.getAbsolutePath() ); - this.varcharType = ( (AbstractHANADialect) getDialect() ).isUseUnicodeStringTypes() ? "nvarchar" : "varchar"; - this.clobType = ( (AbstractHANADialect) getDialect() ).isUseUnicodeStringTypes() ? "nclob" : "clob"; + } + + @Override + protected void afterSessionFactoryBuilt() { + super.afterSessionFactoryBuilt(); + final Dialect dialect = sessionFactory().getJdbcServices().getDialect(); + this.varcharType = ( (AbstractHANADialect) dialect ).isUseUnicodeStringTypes() ? "nvarchar" : "varchar"; + this.clobType = ( (AbstractHANADialect) dialect ).isUseUnicodeStringTypes() ? "nclob" : "clob"; } @After