Search in sources :

Example 6 with BasicJavaType

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

the class BasicValueBinder method prepareAnyKey.

private void prepareAnyKey(XProperty modelXProperty) {
    implicitJavaTypeAccess = (typeConfiguration) -> null;
    explicitJavaTypeAccess = (typeConfiguration) -> {
        final AnyKeyJavaType javaTypeAnn = findAnnotation(modelXProperty, AnyKeyJavaType.class);
        if (javaTypeAnn != null) {
            final Class<? extends BasicJavaType<?>> javaType = normalizeJavaType(javaTypeAnn.value());
            if (javaType != null) {
                return getManagedBeanRegistry().getBean(javaType).getBeanInstance();
            }
        }
        final AnyKeyJavaClass javaClassAnn = findAnnotation(modelXProperty, AnyKeyJavaClass.class);
        if (javaClassAnn != null) {
            // noinspection rawtypes
            return (BasicJavaType) typeConfiguration.getJavaTypeRegistry().getDescriptor(javaClassAnn.value());
        }
        return null;
    };
    explicitJdbcTypeAccess = (typeConfiguration) -> {
        final AnyKeyJdbcType jdbcTypeAnn = findAnnotation(modelXProperty, AnyKeyJdbcType.class);
        if (jdbcTypeAnn != null) {
            final Class<? extends JdbcType> jdbcType = normalizeJdbcType(jdbcTypeAnn.value());
            if (jdbcType != null) {
                final ManagedBean<? extends JdbcType> jtdBean = getManagedBeanRegistry().getBean(jdbcType);
                return jtdBean.getBeanInstance();
            }
        }
        final AnyKeyJdbcTypeCode jdbcTypeCodeAnn = findAnnotation(modelXProperty, AnyKeyJdbcTypeCode.class);
        if (jdbcTypeCodeAnn != null) {
            if (jdbcTypeCodeAnn.value() != Integer.MIN_VALUE) {
                return typeConfiguration.getJdbcTypeRegistry().getDescriptor(jdbcTypeCodeAnn.value());
            }
        }
        return null;
    };
}
Also used : BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) AnyKeyJdbcTypeCode(org.hibernate.annotations.AnyKeyJdbcTypeCode) AnyKeyJdbcType(org.hibernate.annotations.AnyKeyJdbcType) AnyKeyJavaType(org.hibernate.annotations.AnyKeyJavaType) AnyKeyJavaClass(org.hibernate.annotations.AnyKeyJavaClass)

Example 7 with BasicJavaType

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

the class BasicValue method buildResolution.

protected Resolution<?> buildResolution() {
    Properties typeParameters = getTypeParameters();
    if (typeParameters != null && Boolean.parseBoolean(typeParameters.getProperty(DynamicParameterizedType.IS_DYNAMIC)) && typeParameters.get(DynamicParameterizedType.PARAMETER_TYPE) == null) {
        createParameterImpl();
    }
    if (explicitTypeName != null) {
        return interpretExplicitlyNamedType(explicitTypeName, enumerationStyle, implicitJavaTypeAccess, explicitJavaTypeAccess, explicitJdbcTypeAccess, explicitMutabilityPlanAccess, getAttributeConverterDescriptor(), typeParameters, this::setTypeParameters, this, typeConfiguration, getBuildingContext());
    }
    if (isVersion()) {
        return VersionResolution.from(implicitJavaTypeAccess, explicitJavaTypeAccess, explicitJdbcTypeAccess, timeZoneStorageType, typeConfiguration, getBuildingContext());
    }
    final ConverterDescriptor attributeConverterDescriptor = getAttributeConverterDescriptor();
    if (attributeConverterDescriptor != null) {
        final ManagedBeanRegistry managedBeanRegistry = getBuildingContext().getBootstrapContext().getServiceRegistry().getService(ManagedBeanRegistry.class);
        final JpaAttributeConverterCreationContext converterCreationContext = new JpaAttributeConverterCreationContext() {

            @Override
            public ManagedBeanRegistry getManagedBeanRegistry() {
                return managedBeanRegistry;
            }

            @Override
            public TypeConfiguration getTypeConfiguration() {
                return typeConfiguration;
            }
        };
        return NamedConverterResolution.from(attributeConverterDescriptor, explicitJavaTypeAccess, explicitJdbcTypeAccess, explicitMutabilityPlanAccess, this, converterCreationContext, getBuildingContext());
    }
    JavaType<?> jtd = null;
    // determine JavaType if we can
    final BasicJavaType explicitJtd;
    if (explicitJavaTypeAccess != null) {
        explicitJtd = explicitJavaTypeAccess.apply(typeConfiguration);
        if (explicitJtd != null) {
            jtd = explicitJtd;
        }
    } else {
        explicitJtd = null;
    }
    if (jtd == null) {
        if (implicitJavaTypeAccess != null) {
            final java.lang.reflect.Type implicitJtd = implicitJavaTypeAccess.apply(typeConfiguration);
            if (implicitJtd != null) {
                jtd = typeConfiguration.getJavaTypeRegistry().getDescriptor(implicitJtd);
            }
        }
    }
    if (jtd == null) {
        final JavaType<?> reflectedJtd = determineReflectedJavaType();
        if (reflectedJtd != null) {
            jtd = reflectedJtd;
        }
    }
    final JdbcType jdbcType;
    if (explicitJdbcTypeAccess != null) {
        jdbcType = explicitJdbcTypeAccess.apply(typeConfiguration);
    } else {
        jdbcType = null;
    }
    if (jtd == null) {
        if (jdbcType != null) {
            jtd = jdbcType.getJdbcRecommendedJavaTypeMapping(null, null, typeConfiguration);
        }
    }
    if (jtd == null) {
        throw new MappingException("Unable to determine JavaType to use : " + this);
    }
    final TypeDefinitionRegistry typeDefinitionRegistry = getBuildingContext().getTypeDefinitionRegistry();
    final TypeDefinition autoAppliedTypeDef = typeDefinitionRegistry.resolveAutoApplied((BasicJavaType<?>) jtd);
    if (autoAppliedTypeDef != null && (!jtd.getJavaTypeClass().isEnum() || enumerationStyle == null)) {
        log.debug("BasicValue resolution matched auto-applied type-definition");
        return autoAppliedTypeDef.resolve(typeParameters, null, getBuildingContext(), this);
    }
    return InferredBasicValueResolver.from(explicitJtd, jdbcType, resolvedJavaType, this::determineReflectedJavaType, this, getTable(), getColumn(), ownerName, propertyName, typeConfiguration);
}
Also used : ConverterDescriptor(org.hibernate.boot.model.convert.spi.ConverterDescriptor) ClassBasedConverterDescriptor(org.hibernate.boot.model.convert.internal.ClassBasedConverterDescriptor) TypeDefinitionRegistry(org.hibernate.boot.model.TypeDefinitionRegistry) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) Properties(java.util.Properties) MappingException(org.hibernate.MappingException) TypeDefinition(org.hibernate.boot.model.TypeDefinition) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) JpaAttributeConverterCreationContext(org.hibernate.boot.model.convert.spi.JpaAttributeConverterCreationContext) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry)

Example 8 with BasicJavaType

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

the class EnumType method interpretParameters.

private EnumValueConverter<T, Object> interpretParameters(Properties parameters) {
    // noinspection rawtypes
    final EnumJavaType enumJavaType = (EnumJavaType) typeConfiguration.getJavaTypeRegistry().getDescriptor(enumClass);
    // this method should only be called for hbm.xml handling
    assert parameters.get(PARAMETER_TYPE) == null;
    final LocalJdbcTypeIndicators localIndicators = new LocalJdbcTypeIndicators(// use ORDINAL as default for hbm.xml mappings
    jakarta.persistence.EnumType.ORDINAL, // etc
    -1L, null);
    final BasicJavaType<?> stringJavaType = (BasicJavaType<?>) typeConfiguration.getJavaTypeRegistry().getDescriptor(String.class);
    final BasicJavaType<?> integerJavaType = (BasicJavaType<?>) typeConfiguration.getJavaTypeRegistry().getDescriptor(Integer.class);
    if (parameters.containsKey(NAMED)) {
        final boolean useNamed = ConfigurationHelper.getBoolean(NAMED, parameters);
        if (useNamed) {
            // noinspection rawtypes
            return new NamedEnumValueConverter(enumJavaType, stringJavaType.getRecommendedJdbcType(localIndicators), stringJavaType);
        } else {
            // noinspection rawtypes
            return new OrdinalEnumValueConverter(enumJavaType, integerJavaType.getRecommendedJdbcType(localIndicators), typeConfiguration.getJavaTypeRegistry().getDescriptor(Integer.class));
        }
    }
    if (parameters.containsKey(TYPE)) {
        final int type = Integer.decode((String) parameters.get(TYPE));
        if (isNumericType(type)) {
            // noinspection rawtypes
            return new OrdinalEnumValueConverter(enumJavaType, integerJavaType.getRecommendedJdbcType(localIndicators), typeConfiguration.getJavaTypeRegistry().getDescriptor(Integer.class));
        } else if (isCharacterType(type)) {
            // noinspection rawtypes
            return new NamedEnumValueConverter(enumJavaType, stringJavaType.getRecommendedJdbcType(localIndicators), stringJavaType);
        } else {
            throw new HibernateException(String.format(Locale.ENGLISH, "Passed JDBC type code [%s] not recognized as numeric nor character", type));
        }
    }
    // the fallback
    return new OrdinalEnumValueConverter(enumJavaType, integerJavaType.getRecommendedJdbcType(localIndicators), typeConfiguration.getJavaTypeRegistry().getDescriptor(Integer.class));
}
Also used : NamedEnumValueConverter(org.hibernate.metamodel.model.convert.internal.NamedEnumValueConverter) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) EnumJavaType(org.hibernate.type.descriptor.java.EnumJavaType) HibernateException(org.hibernate.HibernateException) OrdinalEnumValueConverter(org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter)

Example 9 with BasicJavaType

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

the class BaseSqmToSqlAstConverter method visitEnumLiteral.

@SuppressWarnings({ "raw", "unchecked" })
@Override
public Object visitEnumLiteral(SqmEnumLiteral<?> sqmEnumLiteral) {
    final BasicValuedMapping inferrableType = (BasicValuedMapping) resolveInferredType();
    if (inferrableType instanceof ConvertibleModelPart) {
        final ConvertibleModelPart inferredPart = (ConvertibleModelPart) inferrableType;
        final BasicValueConverter<Enum<?>, ?> valueConverter = inferredPart.getValueConverter();
        final Object jdbcValue = valueConverter.toRelationalValue(sqmEnumLiteral.getEnumValue());
        return new QueryLiteral<>(jdbcValue, inferredPart);
    }
    final EnumJavaType<?> enumJtd = sqmEnumLiteral.getExpressibleJavaType();
    final JdbcType jdbcType = getTypeConfiguration().getJdbcTypeRegistry().getDescriptor(SqlTypes.TINYINT);
    final BasicJavaType<Integer> relationalJtd = (BasicJavaType) getTypeConfiguration().getJavaTypeRegistry().getDescriptor(Integer.class);
    final BasicType<?> jdbcMappingType = getTypeConfiguration().getBasicTypeRegistry().resolve(relationalJtd, jdbcType);
    return new ConvertedQueryLiteral(sqmEnumLiteral.getEnumValue(), new OrdinalEnumValueConverter<>(enumJtd, jdbcType, relationalJtd), jdbcMappingType);
}
Also used : JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ConvertedQueryLiteral(org.hibernate.sql.ast.tree.expression.ConvertedQueryLiteral) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BigInteger(java.math.BigInteger) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) ConvertedQueryLiteral(org.hibernate.sql.ast.tree.expression.ConvertedQueryLiteral) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) ConvertibleModelPart(org.hibernate.metamodel.mapping.ConvertibleModelPart)

Example 10 with BasicJavaType

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

the class YesNoBinder method bind.

@Override
public void bind(YesNo annotation, MetadataBuildingContext buildingContext, PersistentClass persistentClass, Property property) {
    final BasicValue booleanValueMapping = (BasicValue) property.getValue();
    final BasicJavaType<?> javaType = (BasicJavaType<?>) buildingContext.getBootstrapContext().getTypeConfiguration().getJavaTypeRegistry().getDescriptor(Boolean.class);
    final JdbcType jdbcType = buildingContext.getBootstrapContext().getTypeConfiguration().getJdbcTypeRegistry().getDescriptor(Types.CHAR);
    final InstanceBasedConverterDescriptor converter = new InstanceBasedConverterDescriptor(YesNoConverter.INSTANCE, buildingContext.getBootstrapContext().getClassmateContext());
    booleanValueMapping.setExplicitJavaTypeAccess((typeConfiguration) -> javaType);
    booleanValueMapping.setExplicitJdbcTypeAccess((typeConfiguration) -> jdbcType);
    booleanValueMapping.setJpaAttributeConverterDescriptor(converter);
}
Also used : BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) InstanceBasedConverterDescriptor(org.hibernate.boot.model.convert.internal.InstanceBasedConverterDescriptor) BasicValue(org.hibernate.mapping.BasicValue)

Aggregations

BasicJavaType (org.hibernate.type.descriptor.java.BasicJavaType)10 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)5 MapKeyEnumerated (jakarta.persistence.MapKeyEnumerated)3 MapKeyTemporal (jakarta.persistence.MapKeyTemporal)3 Enumerated (jakarta.persistence.Enumerated)2 Temporal (jakarta.persistence.Temporal)2 MappingException (org.hibernate.MappingException)2 XClass (org.hibernate.annotations.common.reflection.XClass)2 BasicValue (org.hibernate.mapping.BasicValue)2 EnumJavaType (org.hibernate.type.descriptor.java.EnumJavaType)2 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)2 ElementCollection (jakarta.persistence.ElementCollection)1 MapKeyClass (jakarta.persistence.MapKeyClass)1 TemporalType (jakarta.persistence.TemporalType)1 Serializable (java.io.Serializable)1 BigInteger (java.math.BigInteger)1 Properties (java.util.Properties)1 HibernateException (org.hibernate.HibernateException)1 TimeZoneStorageStrategy (org.hibernate.TimeZoneStorageStrategy)1 AnyKeyJavaClass (org.hibernate.annotations.AnyKeyJavaClass)1