Search in sources :

Example 1 with Mutability

use of org.hibernate.annotations.Mutability in project hibernate-orm by hibernate.

the class BasicValueBinder method prepareMapKey.

private void prepareMapKey(XProperty mapAttribute, XClass modelPropertyTypeXClass) {
    final XClass mapKeyClass;
    if (modelPropertyTypeXClass == null) {
        mapKeyClass = mapAttribute.getMapKey();
    } else {
        mapKeyClass = modelPropertyTypeXClass;
    }
    final Class<?> implicitJavaType = resolveJavaType(mapKeyClass);
    implicitJavaTypeAccess = (typeConfiguration) -> implicitJavaType;
    final MapKeyEnumerated mapKeyEnumeratedAnn = mapAttribute.getAnnotation(MapKeyEnumerated.class);
    if (mapKeyEnumeratedAnn != null) {
        enumType = mapKeyEnumeratedAnn.value();
    }
    final MapKeyTemporal mapKeyTemporalAnn = mapAttribute.getAnnotation(MapKeyTemporal.class);
    if (mapKeyTemporalAnn != null) {
        temporalPrecision = mapKeyTemporalAnn.value();
    }
    explicitJdbcTypeAccess = typeConfiguration -> {
        final MapKeyJdbcType jdbcTypeAnn = findAnnotation(mapAttribute, MapKeyJdbcType.class);
        if (jdbcTypeAnn != null) {
            final Class<? extends JdbcType> jdbcTypeImpl = normalizeJdbcType(jdbcTypeAnn.value());
            if (jdbcTypeImpl != null) {
                return getManagedBeanRegistry().getBean(jdbcTypeImpl).getBeanInstance();
            }
        }
        final MapKeyJdbcTypeCode jdbcTypeCodeAnn = findAnnotation(mapAttribute, MapKeyJdbcTypeCode.class);
        if (jdbcTypeCodeAnn != null) {
            final int jdbcTypeCode = jdbcTypeCodeAnn.value();
            if (jdbcTypeCode != Integer.MIN_VALUE) {
                return typeConfiguration.getJdbcTypeRegistry().getDescriptor(jdbcTypeCode);
            }
        }
        return null;
    };
    explicitJavaTypeAccess = typeConfiguration -> {
        final MapKeyJavaType javaTypeAnn = findAnnotation(mapAttribute, MapKeyJavaType.class);
        if (javaTypeAnn != null) {
            final Class<? extends BasicJavaType<?>> jdbcTypeImpl = normalizeJavaType(javaTypeAnn.value());
            if (jdbcTypeImpl != null) {
                final ManagedBean<? extends BasicJavaType> jdbcTypeBean = getManagedBeanRegistry().getBean(jdbcTypeImpl);
                return jdbcTypeBean.getBeanInstance();
            }
        }
        final MapKeyClass mapKeyClassAnn = mapAttribute.getAnnotation(MapKeyClass.class);
        if (mapKeyClassAnn != null) {
            return (BasicJavaType<?>) typeConfiguration.getJavaTypeRegistry().getDescriptor(mapKeyClassAnn.value());
        }
        return null;
    };
    explicitMutabilityAccess = typeConfiguration -> {
        final MapKeyMutability mutabilityAnn = findAnnotation(mapAttribute, MapKeyMutability.class);
        if (mutabilityAnn != null) {
            final Class<? extends MutabilityPlan<?>> mutability = normalizeMutability(mutabilityAnn.value());
            if (mutability != null) {
                final ManagedBean<? extends MutabilityPlan<?>> jtdBean = getManagedBeanRegistry().getBean(mutability);
                return jtdBean.getBeanInstance();
            }
        }
        // see if the value's type Class is annotated `@Immutable`
        if (implicitJavaTypeAccess != null) {
            final Class<?> attributeType = ReflectHelper.getClass(implicitJavaTypeAccess.apply(typeConfiguration));
            if (attributeType != null) {
                if (attributeType.isAnnotationPresent(Immutable.class)) {
                    return ImmutableMutabilityPlan.instance();
                }
            }
        }
        // if the value is converted, see if the converter Class is annotated `@Immutable`
        if (converterDescriptor != null) {
            final Mutability converterMutabilityAnn = converterDescriptor.getAttributeConverterClass().getAnnotation(Mutability.class);
            if (converterMutabilityAnn != null) {
                final ManagedBean<? extends MutabilityPlan<?>> jtdBean = getManagedBeanRegistry().getBean(converterMutabilityAnn.value());
                return jtdBean.getBeanInstance();
            }
            if (converterDescriptor.getAttributeConverterClass().isAnnotationPresent(Immutable.class)) {
                return ImmutableMutabilityPlan.instance();
            }
        }
        final Class<? extends UserType> customTypeImpl = Kind.MAP_KEY.mappingAccess.customType(mapAttribute);
        if (customTypeImpl != null) {
            if (customTypeImpl.isAnnotationPresent(Immutable.class)) {
                return ImmutableMutabilityPlan.instance();
            }
        }
        // generally, this will trigger usage of the `JavaType#getMutabilityPlan`
        return null;
    };
}
Also used : MapKeyJdbcTypeCode(org.hibernate.annotations.MapKeyJdbcTypeCode) Mutability(org.hibernate.annotations.Mutability) CollectionIdMutability(org.hibernate.annotations.CollectionIdMutability) MapKeyMutability(org.hibernate.annotations.MapKeyMutability) MapKeyJavaType(org.hibernate.annotations.MapKeyJavaType) XClass(org.hibernate.annotations.common.reflection.XClass) MapKeyEnumerated(jakarta.persistence.MapKeyEnumerated) MapKeyClass(jakarta.persistence.MapKeyClass) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) MapKeyMutability(org.hibernate.annotations.MapKeyMutability) MapKeyTemporal(jakarta.persistence.MapKeyTemporal) MapKeyJdbcType(org.hibernate.annotations.MapKeyJdbcType)

Example 2 with Mutability

use of org.hibernate.annotations.Mutability in project hibernate-orm by hibernate.

the class BasicValueBinder method normalMutabilityDetails.

private void normalMutabilityDetails(XProperty attributeXProperty) {
    explicitMutabilityAccess = typeConfiguration -> {
        final Mutability mutabilityAnn = findAnnotation(attributeXProperty, Mutability.class);
        if (mutabilityAnn != null) {
            final Class<? extends MutabilityPlan<?>> mutability = normalizeMutability(mutabilityAnn.value());
            if (mutability != null) {
                return getManagedBeanRegistry().getBean(mutability).getBeanInstance();
            }
        }
        final Immutable immutableAnn = attributeXProperty.getAnnotation(Immutable.class);
        if (immutableAnn != null) {
            return ImmutableMutabilityPlan.instance();
        }
        // see if the value's type Class is annotated `@Immutable`
        if (implicitJavaTypeAccess != null) {
            final Class<?> attributeType = ReflectHelper.getClass(implicitJavaTypeAccess.apply(typeConfiguration));
            if (attributeType != null) {
                if (attributeType.isAnnotationPresent(Immutable.class)) {
                    return ImmutableMutabilityPlan.instance();
                }
            }
        }
        // if the value is converted, see if the converter Class is annotated `@Immutable`
        if (converterDescriptor != null) {
            final Mutability converterMutabilityAnn = converterDescriptor.getAttributeConverterClass().getAnnotation(Mutability.class);
            if (converterMutabilityAnn != null) {
                final ManagedBean<? extends MutabilityPlan<?>> jtdBean = getManagedBeanRegistry().getBean(converterMutabilityAnn.value());
                return jtdBean.getBeanInstance();
            }
            if (converterDescriptor.getAttributeConverterClass().isAnnotationPresent(Immutable.class)) {
                return ImmutableMutabilityPlan.instance();
            }
        }
        final Class<? extends UserType> customTypeImpl = Kind.ATTRIBUTE.mappingAccess.customType(attributeXProperty);
        if (customTypeImpl != null) {
            if (customTypeImpl.isAnnotationPresent(Immutable.class)) {
                return ImmutableMutabilityPlan.instance();
            }
        }
        // generally, this will trigger usage of the `JavaType#getMutabilityPlan`
        return null;
    };
}
Also used : Immutable(org.hibernate.annotations.Immutable) Mutability(org.hibernate.annotations.Mutability) CollectionIdMutability(org.hibernate.annotations.CollectionIdMutability) MapKeyMutability(org.hibernate.annotations.MapKeyMutability)

Example 3 with Mutability

use of org.hibernate.annotations.Mutability in project hibernate-orm by hibernate.

the class BasicValueBinder method prepareCollectionId.

private void prepareCollectionId(XProperty modelXProperty) {
    final CollectionId collectionIdAnn = modelXProperty.getAnnotation(CollectionId.class);
    if (collectionIdAnn == null) {
        throw new MappingException("idbag mapping missing @CollectionId");
    }
    final ManagedBeanRegistry beanRegistry = getManagedBeanRegistry();
    explicitBasicTypeName = null;
    implicitJavaTypeAccess = (typeConfiguration) -> null;
    explicitJavaTypeAccess = (typeConfiguration) -> {
        final CollectionIdJavaType javaTypeAnn = findAnnotation(modelXProperty, CollectionIdJavaType.class);
        if (javaTypeAnn != null) {
            final Class<? extends BasicJavaType<?>> javaType = normalizeJavaType(javaTypeAnn.value());
            if (javaType != null) {
                final ManagedBean<? extends BasicJavaType<?>> bean = beanRegistry.getBean(javaType);
                return bean.getBeanInstance();
            }
        }
        return null;
    };
    explicitJdbcTypeAccess = (typeConfiguration) -> {
        final CollectionIdJdbcType jdbcTypeAnn = findAnnotation(modelXProperty, CollectionIdJdbcType.class);
        if (jdbcTypeAnn != null) {
            final Class<? extends JdbcType> jdbcType = normalizeJdbcType(jdbcTypeAnn.value());
            if (jdbcType != null) {
                final ManagedBean<? extends JdbcType> managedBean = beanRegistry.getBean(jdbcType);
                return managedBean.getBeanInstance();
            }
        }
        final CollectionIdJdbcTypeCode jdbcTypeCodeAnn = findAnnotation(modelXProperty, CollectionIdJdbcTypeCode.class);
        if (jdbcTypeCodeAnn != null) {
            if (jdbcTypeCodeAnn.value() != Integer.MIN_VALUE) {
                return typeConfiguration.getJdbcTypeRegistry().getDescriptor(jdbcTypeCodeAnn.value());
            }
        }
        return null;
    };
    explicitMutabilityAccess = (typeConfiguration) -> {
        final CollectionIdMutability mutabilityAnn = findAnnotation(modelXProperty, CollectionIdMutability.class);
        if (mutabilityAnn != null) {
            final Class<? extends MutabilityPlan<?>> mutability = normalizeMutability(mutabilityAnn.value());
            if (mutability != null) {
                final ManagedBean<? extends MutabilityPlan<?>> jtdBean = beanRegistry.getBean(mutability);
                return jtdBean.getBeanInstance();
            }
        }
        // see if the value's type Class is annotated `@Immutable`
        if (implicitJavaTypeAccess != null) {
            final Class<?> attributeType = ReflectHelper.getClass(implicitJavaTypeAccess.apply(typeConfiguration));
            if (attributeType != null) {
                if (attributeType.isAnnotationPresent(Immutable.class)) {
                    return ImmutableMutabilityPlan.instance();
                }
            }
        }
        // if there is a converter, check it for mutability-related annotations
        if (converterDescriptor != null) {
            final Mutability converterMutabilityAnn = converterDescriptor.getAttributeConverterClass().getAnnotation(Mutability.class);
            if (converterMutabilityAnn != null) {
                final ManagedBean<? extends MutabilityPlan<?>> jtdBean = beanRegistry.getBean(converterMutabilityAnn.value());
                return jtdBean.getBeanInstance();
            }
            if (converterDescriptor.getAttributeConverterClass().isAnnotationPresent(Immutable.class)) {
                return ImmutableMutabilityPlan.instance();
            }
        }
        final Class<? extends UserType> customTypeImpl = Kind.ATTRIBUTE.mappingAccess.customType(modelXProperty);
        if (customTypeImpl.isAnnotationPresent(Immutable.class)) {
            return ImmutableMutabilityPlan.instance();
        }
        // generally, this will trigger usage of the `JavaType#getMutabilityPlan`
        return null;
    };
    // todo (6.0) - handle generator
    final String generator = collectionIdAnn.generator();
}
Also used : CollectionIdJdbcTypeCode(org.hibernate.annotations.CollectionIdJdbcTypeCode) CollectionIdJdbcType(org.hibernate.annotations.CollectionIdJdbcType) Mutability(org.hibernate.annotations.Mutability) CollectionIdMutability(org.hibernate.annotations.CollectionIdMutability) MapKeyMutability(org.hibernate.annotations.MapKeyMutability) CollectionIdMutability(org.hibernate.annotations.CollectionIdMutability) MappingException(org.hibernate.MappingException) CollectionIdJavaType(org.hibernate.annotations.CollectionIdJavaType) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) CollectionId(org.hibernate.annotations.CollectionId)

Example 4 with Mutability

use of org.hibernate.annotations.Mutability in project hibernate-orm by hibernate.

the class RegistryHelper method determineMutabilityPlan.

public <J> MutabilityPlan<J> determineMutabilityPlan(Type javaType, TypeConfiguration typeConfiguration) {
    final Class<J> javaTypeClass = determineJavaTypeClass(javaType);
    if (javaTypeClass.isAnnotationPresent(Immutable.class)) {
        return ImmutableMutabilityPlan.instance();
    }
    if (javaTypeClass.isAnnotationPresent(Mutability.class)) {
        final Mutability annotation = javaTypeClass.getAnnotation(Mutability.class);
        final Class<? extends MutabilityPlan<?>> planClass = annotation.value();
        final ManagedBeanRegistry managedBeanRegistry = typeConfiguration.getServiceRegistry().getService(ManagedBeanRegistry.class);
        final ManagedBean<? extends MutabilityPlan<?>> planBean = managedBeanRegistry.getBean(planClass);
        return (MutabilityPlan<J>) planBean.getBeanInstance();
    }
    if (javaTypeClass.isEnum()) {
        return ImmutableMutabilityPlan.instance();
    }
    if (javaTypeClass.isPrimitive()) {
        return ImmutableMutabilityPlan.instance();
    }
    if (Serializable.class.isAssignableFrom(javaTypeClass)) {
        return (MutabilityPlan<J>) SerializableJavaType.SerializableMutabilityPlan.INSTANCE;
    }
    return null;
}
Also used : ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) Mutability(org.hibernate.annotations.Mutability) MutabilityPlan(org.hibernate.type.descriptor.java.MutabilityPlan) ImmutableMutabilityPlan(org.hibernate.type.descriptor.java.ImmutableMutabilityPlan)

Aggregations

Mutability (org.hibernate.annotations.Mutability)4 CollectionIdMutability (org.hibernate.annotations.CollectionIdMutability)3 MapKeyMutability (org.hibernate.annotations.MapKeyMutability)3 ManagedBeanRegistry (org.hibernate.resource.beans.spi.ManagedBeanRegistry)2 MapKeyClass (jakarta.persistence.MapKeyClass)1 MapKeyEnumerated (jakarta.persistence.MapKeyEnumerated)1 MapKeyTemporal (jakarta.persistence.MapKeyTemporal)1 MappingException (org.hibernate.MappingException)1 CollectionId (org.hibernate.annotations.CollectionId)1 CollectionIdJavaType (org.hibernate.annotations.CollectionIdJavaType)1 CollectionIdJdbcType (org.hibernate.annotations.CollectionIdJdbcType)1 CollectionIdJdbcTypeCode (org.hibernate.annotations.CollectionIdJdbcTypeCode)1 Immutable (org.hibernate.annotations.Immutable)1 MapKeyJavaType (org.hibernate.annotations.MapKeyJavaType)1 MapKeyJdbcType (org.hibernate.annotations.MapKeyJdbcType)1 MapKeyJdbcTypeCode (org.hibernate.annotations.MapKeyJdbcTypeCode)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 BasicJavaType (org.hibernate.type.descriptor.java.BasicJavaType)1 ImmutableMutabilityPlan (org.hibernate.type.descriptor.java.ImmutableMutabilityPlan)1 MutabilityPlan (org.hibernate.type.descriptor.java.MutabilityPlan)1