Search in sources :

Example 1 with JdbcTypeRegistry

use of org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry in project hibernate-orm by hibernate.

the class MapKeyJdbcTypeTests method verifyResolutions.

@Test
public void verifyResolutions(DomainModelScope scope) {
    final MetadataImplementor domainModel = scope.getDomainModel();
    final Dialect dialect = domainModel.getDatabase().getDialect();
    final NationalizationSupport nationalizationSupport = dialect.getNationalizationSupport();
    final JdbcTypeRegistry jdbcTypeRegistry = domainModel.getTypeConfiguration().getJdbcTypeRegistry();
    final PersistentClass entityBinding = domainModel.getEntityBinding(MyEntity.class.getName());
    verifyJdbcTypeCodes(entityBinding.getProperty("baseMap"), jdbcTypeRegistry.getDescriptor(Types.INTEGER).getJdbcTypeCode(), jdbcTypeRegistry.getDescriptor(Types.VARCHAR).getJdbcTypeCode());
    verifyJdbcTypeCodes(entityBinding.getProperty("sqlTypeCodeMap"), jdbcTypeRegistry.getDescriptor(Types.TINYINT).getJdbcTypeCode(), jdbcTypeRegistry.getDescriptor(nationalizationSupport.getVarcharVariantCode()).getJdbcTypeCode());
    verifyJdbcTypeCodes(entityBinding.getProperty("sqlTypeMap"), Types.TINYINT, jdbcTypeRegistry.getDescriptor(nationalizationSupport.getVarcharVariantCode()).getJdbcTypeCode());
}
Also used : SkipForDialect(org.hibernate.testing.orm.junit.SkipForDialect) Dialect(org.hibernate.dialect.Dialect) SybaseDialect(org.hibernate.dialect.SybaseDialect) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) NationalizationSupport(org.hibernate.dialect.NationalizationSupport) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.jupiter.api.Test)

Example 2 with JdbcTypeRegistry

use of org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry in project hibernate-orm by hibernate.

the class ListIndexJdbcTypeTests method verifyResolutions.

@Test
public void verifyResolutions(DomainModelScope scope) {
    final MetadataImplementor domainModel = scope.getDomainModel();
    final PersistentClass entityBinding = domainModel.getEntityBinding(TheEntity.class.getName());
    final JdbcTypeRegistry jdbcTypeRegistry = domainModel.getTypeConfiguration().getJdbcTypeRegistry();
    verifyJdbcTypeCodes(entityBinding.getProperty("listOfStrings"), Types.TINYINT);
    verifyJdbcTypeCodes(entityBinding.getProperty("anotherListOfStrings"), jdbcTypeRegistry.getDescriptor(Types.TINYINT).getJdbcTypeCode());
}
Also used : MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.jupiter.api.Test)

Example 3 with JdbcTypeRegistry

use of org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry in project hibernate-orm by hibernate.

the class AttributeConverterTest method testBasicOrmXmlConverterWithOrmXmlPackage.

@Test
@TestForIssue(jiraKey = "HHH-14881")
public void testBasicOrmXmlConverterWithOrmXmlPackage() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Tester.class).addURL(ConfigHelper.findAsResource("org/hibernate/test/converter/package.xml")).getMetadataBuilder().build();
        final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration().getJdbcTypeRegistry();
        PersistentClass tester = metadata.getEntityBinding(Tester.class.getName());
        Property nameProp = tester.getProperty("name");
        SimpleValue nameValue = (SimpleValue) nameProp.getValue();
        Type type = nameValue.getType();
        assertNotNull(type);
        if (!AttributeConverterTypeAdapter.class.isInstance(type)) {
            fail("AttributeConverter not applied");
        }
        final AttributeConverterTypeAdapter typeAdapter = (AttributeConverterTypeAdapter) type;
        assertThat(typeAdapter.getDomainJtd().getJavaTypeClass(), equalTo(String.class));
        assertThat(typeAdapter.getRelationalJtd().getJavaTypeClass(), equalTo(Clob.class));
        final JdbcType sqlTypeDescriptor = typeAdapter.getJdbcType();
        assertThat(sqlTypeDescriptor, is(jdbcTypeRegistry.getDescriptor(Types.CLOB)));
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) SimpleValue(org.hibernate.mapping.SimpleValue) StringJavaType(org.hibernate.type.descriptor.java.StringJavaType) EnumJavaType(org.hibernate.type.descriptor.java.EnumJavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) Clob(java.sql.Clob) Property(org.hibernate.mapping.Property) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with JdbcTypeRegistry

use of org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry in project hibernate-orm by hibernate.

the class AttributeConverterTest method testEnumConverter.

@Test
@TestForIssue(jiraKey = "HHH-8866")
public void testEnumConverter() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
    try {
        MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(EntityWithConvertibleField.class).getMetadataBuilder().applyAttributeConverter(ConvertibleEnumConverter.class, true).build();
        final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration().getJdbcTypeRegistry();
        // first lets validate that the converter was applied...
        final PersistentClass tester = metadata.getEntityBinding(EntityWithConvertibleField.class.getName());
        final Property nameProp = tester.getProperty("convertibleEnum");
        final BasicValue nameValue = (BasicValue) nameProp.getValue();
        final Type type = nameValue.getType();
        assertNotNull(type);
        assertThat(type, instanceOf(AttributeConverterTypeAdapter.class));
        final AttributeConverterTypeAdapter typeAdapter = (AttributeConverterTypeAdapter) type;
        assertThat(typeAdapter.getDomainJtd(), instanceOf(EnumJavaType.class));
        final int expectedJdbcTypeCode;
        if (metadata.getDatabase().getDialect() instanceof HANAColumnStoreDialect && // Only for SAP HANA Cloud
        metadata.getDatabase().getDialect().getVersion().isSameOrAfter(4)) {
            expectedJdbcTypeCode = Types.NVARCHAR;
        } else {
            expectedJdbcTypeCode = Types.VARCHAR;
        }
        assertThat(typeAdapter.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(expectedJdbcTypeCode)));
        // then lets build the SF and verify its use...
        final SessionFactory sf = metadata.buildSessionFactory();
        try {
            Session s = sf.openSession();
            s.getTransaction().begin();
            EntityWithConvertibleField entity = new EntityWithConvertibleField();
            entity.setId("ID");
            entity.setConvertibleEnum(ConvertibleEnum.VALUE);
            String entityID = entity.getId();
            s.persist(entity);
            s.getTransaction().commit();
            s.close();
            s = sf.openSession();
            s.beginTransaction();
            entity = s.load(EntityWithConvertibleField.class, entityID);
            assertEquals(ConvertibleEnum.VALUE, entity.getConvertibleEnum());
            s.getTransaction().commit();
            s.close();
            s = sf.openSession();
            s.beginTransaction();
            s.createQuery("FROM EntityWithConvertibleField e where e.convertibleEnum = org.hibernate.orm.test.mapping.converted.converter.AttributeConverterTest$ConvertibleEnum.VALUE").list();
            s.getTransaction().commit();
            s.close();
            s = sf.openSession();
            s.beginTransaction();
            s.delete(entity);
            s.getTransaction().commit();
            s.close();
        } finally {
            try {
                sf.close();
            } catch (Exception ignore) {
            }
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : HANAColumnStoreDialect(org.hibernate.dialect.HANAColumnStoreDialect) SessionFactory(org.hibernate.SessionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) BasicValue(org.hibernate.mapping.BasicValue) StringJavaType(org.hibernate.type.descriptor.java.StringJavaType) EnumJavaType(org.hibernate.type.descriptor.java.EnumJavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) EnumJavaType(org.hibernate.type.descriptor.java.EnumJavaType) Property(org.hibernate.mapping.Property) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with JdbcTypeRegistry

use of org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry in project hibernate-orm by hibernate.

the class AttributeConverterTest method testBasicOperation.

@Test
public void testBasicOperation() {
    try (StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().build()) {
        final MetadataBuildingContext buildingContext = new MetadataBuildingContextTestingImpl(serviceRegistry);
        final JdbcTypeRegistry jdbcTypeRegistry = buildingContext.getBootstrapContext().getTypeConfiguration().getJdbcTypeRegistry();
        final BasicValue basicValue = new BasicValue(buildingContext);
        basicValue.setJpaAttributeConverterDescriptor(new InstanceBasedConverterDescriptor(new StringClobConverter(), new ClassmateContext()));
        basicValue.setTypeUsingReflection(IrrelevantEntity.class.getName(), "name");
        final Type type = basicValue.getType();
        assertNotNull(type);
        assertThat(type, instanceOf(AttributeConverterTypeAdapter.class));
        final AttributeConverterTypeAdapter typeAdapter = (AttributeConverterTypeAdapter) type;
        assertThat(typeAdapter.getDomainJtd().getJavaTypeClass(), equalTo(String.class));
        final JdbcType jdbcType = typeAdapter.getJdbcType();
        assertThat(jdbcType, is(jdbcTypeRegistry.getDescriptor(Types.CLOB)));
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) IrrelevantEntity(org.hibernate.IrrelevantEntity) MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) BasicValue(org.hibernate.mapping.BasicValue) MetadataBuildingContextTestingImpl(org.hibernate.testing.boot.MetadataBuildingContextTestingImpl) StringJavaType(org.hibernate.type.descriptor.java.StringJavaType) EnumJavaType(org.hibernate.type.descriptor.java.EnumJavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) InstanceBasedConverterDescriptor(org.hibernate.boot.model.convert.internal.InstanceBasedConverterDescriptor) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) ClassmateContext(org.hibernate.boot.internal.ClassmateContext) Test(org.junit.Test)

Aggregations

JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)62 Test (org.junit.jupiter.api.Test)25 EntityPersister (org.hibernate.persister.entity.EntityPersister)19 BasicAttributeMapping (org.hibernate.metamodel.mapping.internal.BasicAttributeMapping)17 PersistentClass (org.hibernate.mapping.PersistentClass)16 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)14 MappingMetamodelImplementor (org.hibernate.metamodel.spi.MappingMetamodelImplementor)14 Test (org.junit.Test)14 MetadataSources (org.hibernate.boot.MetadataSources)13 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)11 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)11 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)11 Property (org.hibernate.mapping.Property)11 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)10 Dialect (org.hibernate.dialect.Dialect)9 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)9 BasicType (org.hibernate.type.BasicType)8 TestForIssue (org.hibernate.testing.TestForIssue)7 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)7 Type (org.hibernate.type.Type)7