Search in sources :

Example 1 with JavaTypeRegistry

use of org.hibernate.type.descriptor.java.spi.JavaTypeRegistry 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)

Example 2 with JavaTypeRegistry

use of org.hibernate.type.descriptor.java.spi.JavaTypeRegistry in project hibernate-orm by hibernate.

the class MetadataContext method applyIdClassMetadata.

private EmbeddableTypeImpl<?> applyIdClassMetadata(Component idClassComponent) {
    final JavaTypeRegistry registry = getTypeConfiguration().getJavaTypeRegistry();
    final Class<?> componentClass = idClassComponent.getComponentClass();
    final JavaType<?> javaType = registry.resolveManagedTypeDescriptor(componentClass);
    final EmbeddableTypeImpl<?> embeddableType = new EmbeddableTypeImpl<>(javaType, false, getJpaMetamodel());
    registerEmbeddableType(embeddableType, idClassComponent);
    return embeddableType;
}
Also used : JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) EmbeddableTypeImpl(org.hibernate.metamodel.model.domain.internal.EmbeddableTypeImpl)

Example 3 with JavaTypeRegistry

use of org.hibernate.type.descriptor.java.spi.JavaTypeRegistry in project hibernate-orm by hibernate.

the class CoercionTests method testCoercibleDetection.

@Test
public void testCoercibleDetection(SessionFactoryScope scope) {
    final TypeConfiguration typeConfiguration = scope.getSessionFactory().getTypeConfiguration();
    final JavaTypeRegistry jtdRegistry = typeConfiguration.getJavaTypeRegistry();
    final JavaType<Integer> integerType = jtdRegistry.resolveDescriptor(Integer.class);
    final JavaType<Long> longType = jtdRegistry.resolveDescriptor(Long.class);
    final JavaType<Double> doubleType = jtdRegistry.resolveDescriptor(Double.class);
    final JavaType<Float> floatType = jtdRegistry.resolveDescriptor(Float.class);
    scope.inTransaction((session) -> {
        checkIntegerConversions(integerType, session);
        checkLongConversions(longType, session);
        checkDoubleConversions(doubleType, session);
    });
}
Also used : BigInteger(java.math.BigInteger) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) Test(org.junit.jupiter.api.Test)

Example 4 with JavaTypeRegistry

use of org.hibernate.type.descriptor.java.spi.JavaTypeRegistry in project hibernate-orm by hibernate.

the class JavaTypeRegistryTest method testGetJavaTypeDescriptorRegistry.

@Test
public void testGetJavaTypeDescriptorRegistry() {
    final TypeConfiguration typeConfiguration = new TypeConfiguration();
    final JavaTypeRegistry registry = new JavaTypeRegistry(typeConfiguration);
    final JavaType<String> descriptor = registry.getDescriptor(String.class);
    assertThat(descriptor, instanceOf(StringJavaType.class));
}
Also used : JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) StringJavaType(org.hibernate.type.descriptor.java.StringJavaType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) Test(org.junit.Test)

Example 5 with JavaTypeRegistry

use of org.hibernate.type.descriptor.java.spi.JavaTypeRegistry in project hibernate-orm by hibernate.

the class MetadataBuildingProcess method handleTypes.

// todo (7.0) : buildJandexInitializer
// private static JandexInitManager buildJandexInitializer(
// MetadataBuildingOptions options,
// ClassLoaderAccess classLoaderAccess) {
// final boolean autoIndexMembers = ConfigurationHelper.getBoolean(
// org.hibernate.cfg.AvailableSettings.ENABLE_AUTO_INDEX_MEMBER_TYPES,
// options.getServiceRegistry().getService( ConfigurationService.class ).getSettings(),
// false
// );
// 
// return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
// }
private static void handleTypes(BootstrapContext bootstrapContext, MetadataBuildingOptions options) {
    final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
    final TypeConfiguration typeConfiguration = bootstrapContext.getTypeConfiguration();
    final TypeContributions typeContributions = new TypeContributions() {

        @Override
        public void contributeType(BasicType type) {
            getBasicTypeRegistry().register(type);
            conditionallyRegisterJtd(type.getJavaTypeDescriptor());
        }

        private void conditionallyRegisterJtd(JavaType jtd) {
            final JavaTypeRegistry jtdRegistry = getTypeConfiguration().getJavaTypeRegistry();
            jtdRegistry.resolveDescriptor(jtd.getJavaTypeClass(), () -> jtd);
        }

        @Override
        public void contributeType(BasicType type, String... keys) {
            getBasicTypeRegistry().register(type, keys);
            conditionallyRegisterJtd(type.getJavaTypeDescriptor());
        }

        @Override
        public void contributeType(UserType type, String[] keys) {
            contributeType(new CustomType<Object>(type, keys, getTypeConfiguration()));
        }

        @Override
        public void contributeJavaType(JavaType<?> descriptor) {
            typeConfiguration.getJavaTypeRegistry().addDescriptor(descriptor);
        }

        @Override
        public void contributeJdbcType(JdbcType descriptor) {
            typeConfiguration.getJdbcTypeRegistry().addDescriptor(descriptor);
        }

        @Override
        public <T> void contributeType(UserType<T> descriptor) {
            typeConfiguration.getBasicTypeRegistry().register(descriptor, descriptor.returnedClass().getName());
        }

        @Override
        public TypeConfiguration getTypeConfiguration() {
            return typeConfiguration;
        }

        final BasicTypeRegistry getBasicTypeRegistry() {
            return getTypeConfiguration().getBasicTypeRegistry();
        }
    };
    // add Dialect contributed types
    final Dialect dialect = options.getServiceRegistry().getService(JdbcServices.class).getDialect();
    dialect.contributeTypes(typeContributions, options.getServiceRegistry());
    // add TypeContributor contributed types.
    for (TypeContributor contributor : classLoaderService.loadJavaServices(TypeContributor.class)) {
        contributor.contribute(typeContributions, options.getServiceRegistry());
    }
    // add fallback type descriptors
    final JdbcTypeRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeRegistry();
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.UUID, SqlTypes.BINARY);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.JSON, SqlTypes.VARBINARY);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.INET, SqlTypes.VARBINARY);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.INTERVAL_SECOND, SqlTypes.NUMERIC);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.GEOMETRY, SqlTypes.VARBINARY);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.POINT, SqlTypes.VARBINARY);
    // add explicit application registered types
    typeConfiguration.addBasicTypeRegistrationContributions(options.getBasicTypeRegistrations());
    // For NORMALIZE, we replace the standard types that use TIMESTAMP_WITH_TIMEZONE to use TIMESTAMP
    if (options.getDefaultTimeZoneStorage() == TimeZoneStorageStrategy.NORMALIZE) {
        final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();
        final JdbcType timestampDescriptor = jdbcTypeRegistry.getDescriptor(Types.TIMESTAMP);
        final BasicTypeRegistry basicTypeRegistry = typeConfiguration.getBasicTypeRegistry();
        final BasicType<?> offsetDateTimeType = new NamedBasicTypeImpl<>(javaTypeRegistry.getDescriptor(OffsetDateTime.class), timestampDescriptor, "OffsetDateTime");
        final BasicType<?> zonedDateTimeType = new NamedBasicTypeImpl<>(javaTypeRegistry.getDescriptor(ZonedDateTime.class), timestampDescriptor, "ZonedDateTime");
        basicTypeRegistry.register(offsetDateTimeType, "org.hibernate.type.OffsetDateTimeType", OffsetDateTime.class.getSimpleName(), OffsetDateTime.class.getName());
        basicTypeRegistry.register(zonedDateTimeType, "org.hibernate.type.ZonedDateTimeType", ZonedDateTime.class.getSimpleName(), ZonedDateTime.class.getName());
    }
}
Also used : NamedBasicTypeImpl(org.hibernate.type.internal.NamedBasicTypeImpl) BasicType(org.hibernate.type.BasicType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) TypeContributor(org.hibernate.boot.model.TypeContributor) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) JavaType(org.hibernate.type.descriptor.java.JavaType) TypeContributions(org.hibernate.boot.model.TypeContributions) OffsetDateTime(java.time.OffsetDateTime) ZonedDateTime(java.time.ZonedDateTime) Dialect(org.hibernate.dialect.Dialect) BasicTypeRegistry(org.hibernate.type.BasicTypeRegistry) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) UserType(org.hibernate.usertype.UserType) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Aggregations

JavaTypeRegistry (org.hibernate.type.descriptor.java.spi.JavaTypeRegistry)12 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 List (java.util.List)2 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)2 Dialect (org.hibernate.dialect.Dialect)2 BasicValue (org.hibernate.mapping.BasicValue)2 CollectionPart (org.hibernate.metamodel.mapping.CollectionPart)2 SelectableMapping (org.hibernate.metamodel.mapping.SelectableMapping)2 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)2 BigInteger (java.math.BigInteger)1 OffsetDateTime (java.time.OffsetDateTime)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 SortedMap (java.util.SortedMap)1 SortedSet (java.util.SortedSet)1 MappingException (org.hibernate.MappingException)1 MapKeyJavaType (org.hibernate.annotations.MapKeyJavaType)1 MapKeyJdbcType (org.hibernate.annotations.MapKeyJdbcType)1