Search in sources :

Example 1 with JavaType

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

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

the class CollectionIdentifierDescriptorImpl method createDomainResult.

public DomainResult<?> createDomainResult(NavigablePath collectionPath, TableGroup tableGroup, DomainResultCreationState creationState) {
    final SqlAstCreationState astCreationState = creationState.getSqlAstCreationState();
    final SqlAstCreationContext astCreationContext = astCreationState.getCreationContext();
    final SessionFactoryImplementor sessionFactory = astCreationContext.getSessionFactory();
    final SqlExpressionResolver sqlExpressionResolver = astCreationState.getSqlExpressionResolver();
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableGroup.getPrimaryTableReference(), columnName), p -> new ColumnReference(tableGroup.getPrimaryTableReference().getIdentificationVariable(), columnName, false, null, null, type, sessionFactory)), type.getJavaTypeDescriptor(), sessionFactory.getTypeConfiguration());
    // noinspection unchecked
    return new BasicResult<>(sqlSelection.getValuesArrayPosition(), null, (JavaType<Object>) type.getJavaTypeDescriptor(), collectionPath);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicType(org.hibernate.type.BasicType) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JavaType(org.hibernate.type.descriptor.java.JavaType) CollectionIdentifierDescriptor(org.hibernate.metamodel.mapping.CollectionIdentifierDescriptor) Clause(org.hibernate.sql.ast.Clause) BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) MappingType(org.hibernate.metamodel.mapping.MappingType) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) FetchOptions(org.hibernate.sql.results.graph.FetchOptions) BiConsumer(java.util.function.BiConsumer) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResult(org.hibernate.sql.results.graph.DomainResult) Fetch(org.hibernate.sql.results.graph.Fetch) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) FetchStyle(org.hibernate.engine.FetchStyle) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) IndexedConsumer(org.hibernate.mapping.IndexedConsumer) FetchParent(org.hibernate.sql.results.graph.FetchParent) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 3 with JavaType

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

the class SqlTuple method createDomainResult.

@Override
public DomainResult createDomainResult(String resultVariable, DomainResultCreationState creationState) {
    final JavaType javaType = ((SqmExpressible<?>) valueMapping).getExpressibleJavaType();
    final int[] valuesArrayPositions = new int[expressions.size()];
    for (int i = 0; i < expressions.size(); i++) {
        valuesArrayPositions[i] = creationState.getSqlAstCreationState().getSqlExpressionResolver().resolveSqlSelection(expressions.get(i), javaType, creationState.getSqlAstCreationState().getCreationContext().getMappingMetamodel().getTypeConfiguration()).getValuesArrayPosition();
    }
    return new TupleResult(valuesArrayPositions, resultVariable, javaType);
}
Also used : JavaType(org.hibernate.type.descriptor.java.JavaType) SqmExpressible(org.hibernate.query.sqm.SqmExpressible) TupleResult(org.hibernate.sql.results.graph.tuple.TupleResult)

Example 4 with JavaType

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

the class Java8DateTimeTests method dump.

private void dump(PersistentClass entityBinding, TheEntity theEntity) {
    final Iterator propertyBindingIterator = entityBinding.getPropertyClosureIterator();
    while (propertyBindingIterator.hasNext()) {
        final Property propertyBinding = (Property) propertyBindingIterator.next();
        final JavaType javaType = ((AbstractStandardBasicType) propertyBinding.getType()).getJavaTypeDescriptor();
        System.out.println(String.format("%s (%s) -> %s", propertyBinding.getName(), javaType.getJavaTypeClass().getSimpleName(), javaType.toString(propertyBinding.getGetter(TheEntity.class).get(theEntity))));
    }
}
Also used : JavaType(org.hibernate.type.descriptor.java.JavaType) Iterator(java.util.Iterator) Property(org.hibernate.mapping.Property) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType)

Example 5 with JavaType

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

JavaType (org.hibernate.type.descriptor.java.JavaType)13 BasicType (org.hibernate.type.BasicType)6 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)6 SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)5 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)5 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)5 SqlAstCreationState (org.hibernate.sql.ast.spi.SqlAstCreationState)4 BasicJavaType (org.hibernate.type.descriptor.java.BasicJavaType)4 ArrayList (java.util.ArrayList)3 BiConsumer (java.util.function.BiConsumer)3 HibernateException (org.hibernate.HibernateException)3 FetchTiming (org.hibernate.engine.FetchTiming)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 UserType (org.hibernate.usertype.UserType)3 TemporalType (jakarta.persistence.TemporalType)2 Serializable (java.io.Serializable)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2