Search in sources :

Example 1 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType 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 2 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType 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)

Example 3 with JdbcType

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

the class AttributeConverterTest method testBasicConverterApplication.

@Test
public void testBasicConverterApplication() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Tester.class).getMetadataBuilder().applyAttributeConverter(StringClobConverter.class, true).build();
        final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration().getJdbcTypeRegistry();
        final PersistentClass tester = metadata.getEntityBinding(Tester.class.getName());
        final Property nameProp = tester.getProperty("name");
        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().getJavaTypeClass(), Matchers.equalTo(String.class));
        final JdbcType jdbcType = typeAdapter.getJdbcType();
        assertThat(jdbcType, 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) 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) 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)

Example 4 with JdbcType

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

the class AnnotationBinder method handleJdbcTypeRegistration.

private static void handleJdbcTypeRegistration(MetadataBuildingContext context, ManagedBeanRegistry managedBeanRegistry, JdbcTypeRegistration annotation) {
    final Class<? extends JdbcType> jdbcTypeClass = annotation.value();
    final JdbcType jdbcType = managedBeanRegistry.getBean(jdbcTypeClass).getBeanInstance();
    final int typeCode = annotation.registrationCode() == Integer.MIN_VALUE ? jdbcType.getJdbcTypeCode() : annotation.registrationCode();
    context.getMetadataCollector().addJdbcTypeRegistration(typeCode, jdbcType);
}
Also used : JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) MapKeyJdbcType(org.hibernate.annotations.MapKeyJdbcType) UniqueConstraint(jakarta.persistence.UniqueConstraint) Constraint(org.hibernate.mapping.Constraint)

Example 5 with JdbcType

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

the class AnnotationBinder method resolveJavaType.

private static JdbcMapping resolveJavaType(Class<JavaType<?>> type, MetadataBuildingContext context) {
    final TypeConfiguration typeConfiguration = context.getBootstrapContext().getTypeConfiguration();
    final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();
    final JavaType<?> registeredJtd = javaTypeRegistry.findDescriptor(type);
    final JavaType<?> jtd;
    if (registeredJtd != null) {
        jtd = registeredJtd;
    } else {
        final StandardServiceRegistry serviceRegistry = context.getBootstrapContext().getServiceRegistry();
        final ManagedBeanRegistry beanRegistry = serviceRegistry.getService(ManagedBeanRegistry.class);
        final ManagedBean<JavaType<?>> bean = beanRegistry.getBean(type);
        jtd = bean.getBeanInstance();
    }
    final JdbcType jdbcType = jtd.getRecommendedJdbcType(typeConfiguration.getCurrentBaseSqlTypeIndicators());
    return typeConfiguration.getBasicTypeRegistry().resolve(jtd, jdbcType);
}
Also used : JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) MapKeyJavaType(org.hibernate.annotations.MapKeyJavaType) JavaType(org.hibernate.type.descriptor.java.JavaType) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) MapKeyJdbcType(org.hibernate.annotations.MapKeyJdbcType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Aggregations

JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)36 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)11 BasicJavaType (org.hibernate.type.descriptor.java.BasicJavaType)7 BasicValue (org.hibernate.mapping.BasicValue)6 ObjectJdbcType (org.hibernate.type.descriptor.jdbc.ObjectJdbcType)6 MappingException (org.hibernate.MappingException)5 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)5 AttributeConverterTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter)5 EnumJavaType (org.hibernate.type.descriptor.java.EnumJavaType)5 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)5 MapKeyJdbcType (org.hibernate.annotations.MapKeyJdbcType)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)4 Type (org.hibernate.type.Type)4 JavaType (org.hibernate.type.descriptor.java.JavaType)4 StringJavaType (org.hibernate.type.descriptor.java.StringJavaType)4 Test (org.junit.Test)4 MetadataSources (org.hibernate.boot.MetadataSources)3 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)3 PersistentClass (org.hibernate.mapping.PersistentClass)3