Search in sources :

Example 6 with JavaType

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

the class TypeDefinition method createResolution.

private static BasicValue.Resolution<?> createResolution(String name, Class<?> typeImplementorClass, Properties parameters, Map<?, ?> usageSiteProperties, JdbcTypeIndicators indicators, MetadataBuildingContext context) {
    final BootstrapContext bootstrapContext = context.getBootstrapContext();
    final TypeConfiguration typeConfiguration = bootstrapContext.getTypeConfiguration();
    final BeanInstanceProducer instanceProducer = bootstrapContext.getBeanInstanceProducer();
    final boolean isKnownType = Type.class.isAssignableFrom(typeImplementorClass) || UserType.class.isAssignableFrom(typeImplementorClass);
    // support for AttributeConverter would be nice too
    if (isKnownType) {
        final Object typeInstance = instantiateType(bootstrapContext.getServiceRegistry(), name, typeImplementorClass, instanceProducer);
        if (typeInstance instanceof TypeConfigurationAware) {
            ((TypeConfigurationAware) typeInstance).setTypeConfiguration(typeConfiguration);
        }
        final Properties combinedTypeParameters;
        if (CollectionHelper.isNotEmpty(usageSiteProperties)) {
            combinedTypeParameters = new Properties(parameters);
            combinedTypeParameters.putAll(usageSiteProperties);
        } else {
            combinedTypeParameters = parameters;
        }
        injectParameters(typeInstance, combinedTypeParameters);
        if (typeInstance instanceof UserType) {
            final UserType<Object> userType = (UserType<Object>) typeInstance;
            final CustomType<Object> customType = new CustomType<>(userType, typeConfiguration);
            return new UserTypeResolution(customType, null, combinedTypeParameters);
        }
        if (typeInstance instanceof BasicType) {
            final BasicType resolvedBasicType = (BasicType) typeInstance;
            return new BasicValue.Resolution<Object>() {

                @Override
                public JdbcMapping getJdbcMapping() {
                    return resolvedBasicType;
                }

                @Override
                public BasicType getLegacyResolvedBasicType() {
                    return resolvedBasicType;
                }

                @Override
                public Properties getCombinedTypeParameters() {
                    return combinedTypeParameters;
                }

                @Override
                public JavaType<Object> getDomainJavaType() {
                    return resolvedBasicType.getMappedJavaType();
                }

                @Override
                public JavaType<?> getRelationalJavaType() {
                    return resolvedBasicType.getMappedJavaType();
                }

                @Override
                public JdbcType getJdbcType() {
                    return resolvedBasicType.getJdbcType();
                }

                @Override
                public BasicValueConverter getValueConverter() {
                    return null;
                }

                @Override
                public MutabilityPlan<Object> getMutabilityPlan() {
                    // a TypeDefinition does not explicitly provide a MutabilityPlan (yet?)
                    return resolvedBasicType.isMutable() ? getDomainJavaType().getMutabilityPlan() : ImmutableMutabilityPlan.instance();
                }
            };
        }
    }
    if (Serializable.class.isAssignableFrom(typeImplementorClass)) {
        final JavaType<Serializable> jtd = typeConfiguration.getJavaTypeRegistry().resolveDescriptor(typeImplementorClass);
        final JdbcType jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(Types.VARBINARY);
        final BasicType<Serializable> resolved = typeConfiguration.getBasicTypeRegistry().resolve(jtd, jdbcType);
        final SerializableType legacyType = new SerializableType(typeImplementorClass);
        return new BasicValue.Resolution<Object>() {

            @Override
            public JdbcMapping getJdbcMapping() {
                return resolved;
            }

            @Override
            public BasicType getLegacyResolvedBasicType() {
                return legacyType;
            }

            @Override
            public JavaType<Object> getDomainJavaType() {
                return (JavaType) resolved.getMappedJavaType();
            }

            @Override
            public JavaType<?> getRelationalJavaType() {
                return resolved.getMappedJavaType();
            }

            @Override
            public JdbcType getJdbcType() {
                return resolved.getJdbcType();
            }

            @Override
            public BasicValueConverter getValueConverter() {
                return null;
            }

            @Override
            public MutabilityPlan<Object> getMutabilityPlan() {
                // a TypeDefinition does not explicitly provide a MutabilityPlan (yet?)
                return resolved.isMutable() ? getDomainJavaType().getMutabilityPlan() : ImmutableMutabilityPlan.instance();
            }
        };
    }
    throw new IllegalArgumentException("Named type [" + typeImplementorClass + "] did not implement BasicType nor UserType");
}
Also used : BeanInstanceProducer(org.hibernate.resource.beans.spi.BeanInstanceProducer) CustomType(org.hibernate.type.CustomType) TypeConfigurationAware(org.hibernate.type.spi.TypeConfigurationAware) Serializable(java.io.Serializable) BasicType(org.hibernate.type.BasicType) SerializableType(org.hibernate.type.SerializableType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) BootstrapContext(org.hibernate.boot.spi.BootstrapContext) UserTypeResolution(org.hibernate.boot.model.process.internal.UserTypeResolution) Properties(java.util.Properties) BasicType(org.hibernate.type.BasicType) CustomType(org.hibernate.type.CustomType) JavaType(org.hibernate.type.descriptor.java.JavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) SerializableType(org.hibernate.type.SerializableType) UserType(org.hibernate.usertype.UserType) Type(org.hibernate.type.Type) JavaType(org.hibernate.type.descriptor.java.JavaType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) UserType(org.hibernate.usertype.UserType) UserTypeResolution(org.hibernate.boot.model.process.internal.UserTypeResolution)

Example 7 with JavaType

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

the class VersionResolution method from.

// todo (6.0) : support explicit JTD?
// todo (6.0) : support explicit STD?
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <E> VersionResolution<E> from(Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess, Function<TypeConfiguration, BasicJavaType> explicitJtdAccess, Function<TypeConfiguration, JdbcType> explicitStdAccess, TimeZoneStorageType timeZoneStorageType, TypeConfiguration typeConfiguration, @SuppressWarnings("unused") MetadataBuildingContext context) {
    // todo (6.0) : add support for Dialect-specific interpretation?
    final java.lang.reflect.Type implicitJavaType = implicitJavaTypeAccess.apply(typeConfiguration);
    final JavaType registered = typeConfiguration.getJavaTypeRegistry().resolveDescriptor(implicitJavaType);
    final BasicJavaType jtd = (BasicJavaType) registered;
    final JdbcType recommendedJdbcType = jtd.getRecommendedJdbcType(new JdbcTypeIndicators() {

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

        @Override
        public TemporalType getTemporalPrecision() {
            // if it is a temporal version, it needs to be a TIMESTAMP
            return TemporalType.TIMESTAMP;
        }

        @Override
        public TimeZoneStorageStrategy getDefaultTimeZoneStorageStrategy() {
            if (timeZoneStorageType != null) {
                switch(timeZoneStorageType) {
                    case NATIVE:
                        return TimeZoneStorageStrategy.NATIVE;
                    case NORMALIZE:
                        return TimeZoneStorageStrategy.NORMALIZE;
                }
            }
            return context.getBuildingOptions().getDefaultTimeZoneStorage();
        }
    });
    final BasicType<?> basicType = typeConfiguration.getBasicTypeRegistry().resolve(jtd, recommendedJdbcType);
    final BasicType legacyType = typeConfiguration.getBasicTypeRegistry().getRegisteredType(jtd.getJavaType());
    assert legacyType.getJdbcType().equals(recommendedJdbcType);
    return new VersionResolution<>(jtd, recommendedJdbcType, basicType, legacyType);
}
Also used : JdbcTypeIndicators(org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators) TimeZoneStorageStrategy(org.hibernate.TimeZoneStorageStrategy) BasicType(org.hibernate.type.BasicType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) JavaType(org.hibernate.type.descriptor.java.JavaType) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) TemporalType(jakarta.persistence.TemporalType)

Example 8 with JavaType

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

the class AnnotationBinder method resolveFilterParamType.

private static JdbcMapping resolveFilterParamType(Class<?> type, MetadataBuildingContext context) {
    if (UserType.class.isAssignableFrom(type)) {
        // noinspection unchecked
        return resolveUserType((Class<UserType<?>>) type, context);
    }
    if (AttributeConverter.class.isAssignableFrom(type)) {
        // noinspection unchecked
        return resolveAttributeConverter((Class<AttributeConverter<?, ?>>) type, context);
    }
    if (JavaType.class.isAssignableFrom(type)) {
        // noinspection unchecked
        return resolveJavaType((Class<JavaType<?>>) type, context);
    }
    final TypeConfiguration typeConfiguration = context.getBootstrapContext().getTypeConfiguration();
    final JavaType<Object> jtd = typeConfiguration.getJavaTypeRegistry().findDescriptor(type);
    if (jtd != null) {
        final JdbcType jdbcType = jtd.getRecommendedJdbcType(typeConfiguration.getCurrentBaseSqlTypeIndicators());
        return typeConfiguration.getBasicTypeRegistry().resolve(jtd, jdbcType);
    }
    return null;
}
Also used : AttributeConverter(jakarta.persistence.AttributeConverter) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) MapKeyJavaType(org.hibernate.annotations.MapKeyJavaType) JavaType(org.hibernate.type.descriptor.java.JavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) MapKeyJdbcType(org.hibernate.annotations.MapKeyJdbcType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) CompositeUserType(org.hibernate.usertype.CompositeUserType) UserType(org.hibernate.usertype.UserType)

Example 9 with JavaType

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

the class Predicate method createDomainResult.

@Override
default DomainResult<Boolean> createDomainResult(String resultVariable, DomainResultCreationState creationState) {
    final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final JavaType javaType = getExpressionType().getJdbcMappings().get(0).getJavaTypeDescriptor();
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(this, javaType, sqlAstCreationState.getCreationContext().getMappingMetamodel().getTypeConfiguration());
    // noinspection unchecked
    return new BasicResult(sqlSelection.getValuesArrayPosition(), resultVariable, javaType);
}
Also used : JavaType(org.hibernate.type.descriptor.java.JavaType) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 10 with JavaType

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

the class QuerySpec method createDomainResult.

@Override
public DomainResult createDomainResult(String resultVariable, DomainResultCreationState creationState) {
    TypeConfiguration typeConfiguration = creationState.getSqlAstCreationState().getCreationContext().getMappingMetamodel().getTypeConfiguration();
    final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlAstCreationState().getSqlExpressionResolver();
    if (selectClause.getSqlSelections().size() == 1) {
        SqlSelection first = selectClause.getSqlSelections().get(0);
        JavaType descriptor = first.getExpressionType().getJdbcMappings().get(0).getJavaTypeDescriptor();
        final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(this, descriptor, typeConfiguration);
        return new BasicResult<>(sqlSelection.getValuesArrayPosition(), resultVariable, descriptor);
    } else {
        throw new UnsupportedOperationException("Domain result for non-scalar subquery shouldn't be created!");
    }
}
Also used : JavaType(org.hibernate.type.descriptor.java.JavaType) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

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