Search in sources :

Example 1 with EmbeddableTypeImpl

use of org.hibernate.metamodel.model.domain.internal.EmbeddableTypeImpl 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 2 with EmbeddableTypeImpl

use of org.hibernate.metamodel.model.domain.internal.EmbeddableTypeImpl in project igloo-parent by igloo-project.

the class AbstractTestCase method testMetaModel.

/**
 * Méthode utilisée à des fins de tests.
 */
protected void testMetaModel(Attribute<?, ?> attribute, List<Class<?>> classesAutorisees, List<Attribute<?, ?>> ignoredAttributes) throws NoSuchFieldException, SecurityException {
    for (Attribute<?, ?> ignoredAttribute : ignoredAttributes) {
        if (ignoredAttribute.getJavaMember().equals(attribute.getJavaMember())) {
            // champ ignoré
            return;
        }
    }
    Enumerated enumerated = attribute.getJavaMember().getDeclaringClass().getDeclaredField(attribute.getName()).getAnnotation(Enumerated.class);
    MapKeyEnumerated mapKeyEnumerated = attribute.getJavaMember().getDeclaringClass().getDeclaredField(attribute.getName()).getAnnotation(MapKeyEnumerated.class);
    MapKey mapKey = attribute.getJavaMember().getDeclaringClass().getDeclaredField(attribute.getName()).getAnnotation(MapKey.class);
    // cas des embeddable et des collectionOfElements d'embeddable
    if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.ELEMENT_COLLECTION) && EmbeddableTypeImpl.class.isInstance(((PluralAttribute<?, ?, ?>) attribute).getElementType())) {
        PluralAttribute<?, ?, ?> pluralAttribute = (PluralAttribute<?, ?, ?>) attribute;
        if (classesAutorisees.contains(pluralAttribute.getElementType().getJavaType())) {
            // type autorisé de manière explicite
            return;
        }
        for (Attribute<?, ?> embeddedAttribute : ((EmbeddableTypeImpl<?>) pluralAttribute.getElementType()).getAttributes()) {
            testMetaModel(embeddedAttribute, classesAutorisees, ignoredAttributes);
        }
        return;
    } else if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.EMBEDDED)) {
        SingularAttribute<?, ?> singularAttribute = (SingularAttribute<?, ?>) attribute;
        if (classesAutorisees.contains(singularAttribute.getJavaType())) {
            // type autorisé de manière explicite
            return;
        }
        if (EmbeddableTypeImpl.class.isInstance(singularAttribute.getType())) {
            for (Attribute<?, ?> embeddedAttribute : ((EmbeddableTypeImpl<?>) singularAttribute.getType()).getAttributes()) {
                testMetaModel(embeddedAttribute, classesAutorisees, ignoredAttributes);
            }
            return;
        }
    }
    if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.BASIC) && !classesAutorisees.contains(attribute.getJavaType()) && (enumerated == null || EnumType.ORDINAL.equals(enumerated.value()))) {
        throw new IllegalStateException("Champ \"" + attribute.getName() + "\", de type " + attribute.getJavaType().getSimpleName() + " refusé");
    } else if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.ELEMENT_COLLECTION) && PluralAttribute.class.isInstance(attribute) && !classesAutorisees.contains(((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType()) && (enumerated == null || EnumType.ORDINAL.equals(enumerated.value()))) {
        PluralAttribute<?, ?, ?> pluralAttribute = (PluralAttribute<?, ?, ?>) attribute;
        throw new IllegalStateException("Collection \"" + attribute.getName() + "\" de " + pluralAttribute.getElementType().getJavaType().getSimpleName() + " refusée");
    } else if (attribute instanceof MapAttribute) {
        MapAttribute<?, ?, ?> mapAttribute = (MapAttribute<?, ?, ?>) attribute;
        if (Enum.class.isAssignableFrom(mapAttribute.getKeyJavaType()) && (mapKeyEnumerated == null || EnumType.ORDINAL.equals(mapKeyEnumerated.value())) && mapKey == null) /* if @MapKey present, then field format is defined elsewhere and check is useless */
        {
            throw new IllegalStateException("Map \"" + attribute.getName() + "\" de clés ordinales " + ((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType().getSimpleName() + " refusée");
        }
        if (Enum.class.isAssignableFrom(mapAttribute.getElementType().getJavaType()) && (enumerated == null || EnumType.ORDINAL.equals(enumerated.value()))) {
            throw new IllegalStateException("Map \"" + attribute.getName() + "\" de valeurs ordinales " + ((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType().getSimpleName() + " refusée");
        }
    }
}
Also used : EmbeddableTypeImpl(org.hibernate.metamodel.model.domain.internal.EmbeddableTypeImpl) PluralAttribute(javax.persistence.metamodel.PluralAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) MapKeyEnumerated(javax.persistence.MapKeyEnumerated) Enumerated(javax.persistence.Enumerated) MapKeyEnumerated(javax.persistence.MapKeyEnumerated) SingularAttribute(javax.persistence.metamodel.SingularAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) MapKey(javax.persistence.MapKey)

Aggregations

EmbeddableTypeImpl (org.hibernate.metamodel.model.domain.internal.EmbeddableTypeImpl)2 Enumerated (javax.persistence.Enumerated)1 MapKey (javax.persistence.MapKey)1 MapKeyEnumerated (javax.persistence.MapKeyEnumerated)1 Attribute (javax.persistence.metamodel.Attribute)1 MapAttribute (javax.persistence.metamodel.MapAttribute)1 PluralAttribute (javax.persistence.metamodel.PluralAttribute)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1 JavaTypeRegistry (org.hibernate.type.descriptor.java.spi.JavaTypeRegistry)1