Search in sources :

Example 1 with AttributeContainer

use of org.hibernate.metamodel.model.domain.internal.AttributeContainer in project hibernate-orm by hibernate.

the class MetadataContext method applyIdMetadata.

private <X> void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
    if (mappingType.hasIdentifierProperty()) {
        final Property declaredIdentifierProperty = mappingType.getDeclaredIdentifierProperty();
        if (declaredIdentifierProperty != null) {
            final SingularPersistentAttribute<X, Object> attribute = attributeFactory.buildIdAttribute(jpaMappingType, declaredIdentifierProperty);
            // noinspection unchecked
            ((AttributeContainer) jpaMappingType).getInFlightAccess().applyIdAttribute(attribute);
        }
    } else // a MappedSuperclass can have no identifier if the id is set below in the hierarchy
    if (mappingType.getIdentifierMapper() != null) {
        Set<SingularPersistentAttribute<? super X, ?>> attributes = buildIdClassAttributes(jpaMappingType, mappingType.getIdentifierMapper().getProperties());
        // noinspection unchecked
        ((AttributeContainer<X>) jpaMappingType).getInFlightAccess().applyIdClassAttributes(attributes);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AttributeContainer(org.hibernate.metamodel.model.domain.internal.AttributeContainer) Property(org.hibernate.mapping.Property)

Example 2 with AttributeContainer

use of org.hibernate.metamodel.model.domain.internal.AttributeContainer in project hibernate-orm by hibernate.

the class MetadataContext method wrapUp.

@SuppressWarnings("unchecked")
public void wrapUp() {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Wrapping up metadata context...");
    }
    boolean staticMetamodelScanEnabled = this.jpaStaticMetaModelPopulationSetting != JpaStaticMetaModelPopulationSetting.DISABLED;
    // we need to process types from superclasses to subclasses
    for (Object mapping : orderedMappings) {
        if (PersistentClass.class.isAssignableFrom(mapping.getClass())) {
            final PersistentClass safeMapping = (PersistentClass) mapping;
            if (LOG.isTraceEnabled()) {
                LOG.trace("Starting entity [" + safeMapping.getEntityName() + ']');
            }
            try {
                final EntityDomainType<Object> jpaMapping = (EntityDomainType<Object>) entityTypesByPersistentClass.get(safeMapping);
                applyIdMetadata(safeMapping, jpaMapping);
                applyVersionAttribute(safeMapping, jpaMapping);
                for (Property property : safeMapping.getDeclaredProperties()) {
                    if (property.getValue() == safeMapping.getIdentifierMapper()) {
                        // #buildIdClassAttributes
                        continue;
                    }
                    if (safeMapping.isVersioned() && property == safeMapping.getVersion()) {
                        // skip the version property, it was already handled previously.
                        continue;
                    }
                    final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute(jpaMapping, property);
                    if (attribute != null) {
                        addAttribute(jpaMapping, attribute);
                        if (property.isNaturalIdentifier()) {
                            ((AttributeContainer<Object>) jpaMapping).getInFlightAccess().applyNaturalIdAttribute(attribute);
                        }
                    }
                }
                ((AttributeContainer<?>) jpaMapping).getInFlightAccess().finishUp();
                if (staticMetamodelScanEnabled) {
                    populateStaticMetamodel(jpaMapping);
                }
            } finally {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Completed entity [" + safeMapping.getEntityName() + ']');
                }
            }
        } else if (MappedSuperclass.class.isAssignableFrom(mapping.getClass())) {
            final MappedSuperclass safeMapping = (MappedSuperclass) mapping;
            if (LOG.isTraceEnabled()) {
                LOG.trace("Starting mapped superclass [" + safeMapping.getMappedClass().getName() + ']');
            }
            try {
                final MappedSuperclassDomainType<Object> jpaType = (MappedSuperclassDomainType<Object>) mappedSuperclassByMappedSuperclassMapping.get(safeMapping);
                applyIdMetadata(safeMapping, jpaType);
                applyVersionAttribute(safeMapping, jpaType);
                for (Property property : safeMapping.getDeclaredProperties()) {
                    if (safeMapping.isVersioned() && property == safeMapping.getVersion()) {
                        // skip the version property, it was already handled previously.
                        continue;
                    }
                    final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute(jpaType, property);
                    if (attribute != null) {
                        addAttribute(jpaType, attribute);
                        if (property.isNaturalIdentifier()) {
                            ((AttributeContainer<Object>) jpaType).getInFlightAccess().applyNaturalIdAttribute(attribute);
                        }
                    }
                }
                ((AttributeContainer<?>) jpaType).getInFlightAccess().finishUp();
                if (staticMetamodelScanEnabled) {
                    populateStaticMetamodel(jpaType);
                }
            } finally {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Completed mapped superclass [" + safeMapping.getMappedClass().getName() + ']');
                }
            }
        } else {
            throw new AssertionFailure("Unexpected mapping type: " + mapping.getClass());
        }
    }
    while (!embeddablesToProcess.isEmpty()) {
        final ArrayList<EmbeddableDomainType<?>> processingEmbeddables = new ArrayList<>(embeddablesToProcess.size());
        for (List<EmbeddableDomainType<?>> embeddableDomainTypes : embeddablesToProcess.values()) {
            processingEmbeddables.addAll(embeddableDomainTypes);
        }
        embeddablesToProcess.clear();
        for (EmbeddableDomainType<?> embeddable : processingEmbeddables) {
            final Component component = componentByEmbeddable.get(embeddable);
            for (Property property : component.getProperties()) {
                final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute((ManagedDomainType<Object>) embeddable, property);
                if (attribute != null) {
                    addAttribute(embeddable, attribute);
                }
            }
            ((AttributeContainer<?>) embeddable).getInFlightAccess().finishUp();
            embeddables.put(embeddable.getJavaType(), embeddable);
            if (staticMetamodelScanEnabled) {
                populateStaticMetamodel(embeddable);
            }
        }
    }
}
Also used : EmbeddableDomainType(org.hibernate.metamodel.model.domain.EmbeddableDomainType) AssertionFailure(org.hibernate.AssertionFailure) ArrayList(java.util.ArrayList) AttributeContainer(org.hibernate.metamodel.model.domain.internal.AttributeContainer) MappedSuperclassDomainType(org.hibernate.metamodel.model.domain.MappedSuperclassDomainType) PersistentAttribute(org.hibernate.metamodel.model.domain.PersistentAttribute) SingularPersistentAttribute(org.hibernate.metamodel.model.domain.SingularPersistentAttribute) MappedSuperclass(org.hibernate.mapping.MappedSuperclass) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 3 with AttributeContainer

use of org.hibernate.metamodel.model.domain.internal.AttributeContainer in project hibernate-orm by hibernate.

the class MetadataContext method applyIdMetadata.

// 1) create the part
// 2) register the part (mapping role)
// 3) somehow get the mapping role "into" the part (setter, ?)
private void applyIdMetadata(PersistentClass persistentClass, IdentifiableDomainType<?> identifiableType) {
    if (persistentClass.hasIdentifierProperty()) {
        final Property declaredIdentifierProperty = persistentClass.getDeclaredIdentifierProperty();
        if (declaredIdentifierProperty != null) {
            final SingularPersistentAttribute<?, Object> idAttribute = attributeFactory.buildIdAttribute(identifiableType, declaredIdentifierProperty);
            // noinspection unchecked rawtypes
            ((AttributeContainer) identifiableType).getInFlightAccess().applyIdAttribute(idAttribute);
        }
    } else {
        if (!(persistentClass.getIdentifier() instanceof Component)) {
            throw new MappingException("Expecting Component for id mapping with no id-attribute");
        }
        // Handle the actual id-attributes
        final Component cidValue = (Component) persistentClass.getIdentifier();
        final List<Property> cidProperties;
        final int propertySpan;
        final EmbeddableTypeImpl<?> idClassType;
        final Component identifierMapper = persistentClass.getIdentifierMapper();
        if (identifierMapper != null) {
            cidProperties = identifierMapper.getProperties();
            propertySpan = identifierMapper.getPropertySpan();
            idClassType = applyIdClassMetadata((Component) persistentClass.getIdentifier());
        } else {
            cidProperties = cidValue.getProperties();
            propertySpan = cidValue.getPropertySpan();
            idClassType = null;
        }
        assert cidValue.isEmbedded();
        AbstractIdentifiableType<?> idType = (AbstractIdentifiableType<?>) identifiableTypesByName.get(cidValue.getOwner().getEntityName());
        // noinspection rawtypes
        Set idAttributes = idType.getIdClassAttributesSafely();
        if (idAttributes == null) {
            idAttributes = new HashSet<>(propertySpan);
            for (Property cidSubproperty : cidProperties) {
                final SingularPersistentAttribute<?, Object> cidSubAttr = attributeFactory.buildIdAttribute(idType, cidSubproperty);
                // noinspection unchecked
                idAttributes.add(cidSubAttr);
            }
        }
        AttributeContainer<?> container = (AttributeContainer<?>) identifiableType;
        // noinspection unchecked
        container.getInFlightAccess().applyNonAggregatedIdAttributes(idAttributes, idClassType);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AbstractIdentifiableType(org.hibernate.metamodel.model.domain.AbstractIdentifiableType) AttributeContainer(org.hibernate.metamodel.model.domain.internal.AttributeContainer) MappingException(org.hibernate.MappingException) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property)

Example 4 with AttributeContainer

use of org.hibernate.metamodel.model.domain.internal.AttributeContainer in project hibernate-orm by hibernate.

the class MetadataContext method addAttribute.

private void addAttribute(ManagedDomainType<?> type, PersistentAttribute<Object, ?> attribute) {
    // noinspection unchecked
    AttributeContainer<Object> container = (AttributeContainer<Object>) type;
    final AttributeContainer.InFlightAccess<Object> inFlightAccess = container.getInFlightAccess();
    final boolean virtual = attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED && attribute.getAttributeJavaType() instanceof EntityJavaType<?>;
    if (virtual) {
        final EmbeddableDomainType<?> embeddableDomainType = (EmbeddableDomainType<?>) attribute.getValueGraphType();
        final Component component = componentByEmbeddable.get(embeddableDomainType);
        for (Property property : component.getProperties()) {
            // noinspection unchecked
            ManagedDomainType<Object> managedDomainType = (ManagedDomainType<Object>) embeddableDomainType;
            final PersistentAttribute<Object, ?> subAttribute = attributeFactory.buildAttribute(managedDomainType, property);
            if (subAttribute != null) {
                inFlightAccess.addAttribute(subAttribute);
            }
        }
        if (jpaMetaModelPopulationSetting != JpaMetaModelPopulationSetting.ENABLED) {
            return;
        }
    }
    inFlightAccess.addAttribute(attribute);
}
Also used : EmbeddableDomainType(org.hibernate.metamodel.model.domain.EmbeddableDomainType) ManagedDomainType(org.hibernate.metamodel.model.domain.ManagedDomainType) AttributeContainer(org.hibernate.metamodel.model.domain.internal.AttributeContainer) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property)

Aggregations

Property (org.hibernate.mapping.Property)4 AttributeContainer (org.hibernate.metamodel.model.domain.internal.AttributeContainer)4 Component (org.hibernate.mapping.Component)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 EmbeddableDomainType (org.hibernate.metamodel.model.domain.EmbeddableDomainType)2 ArrayList (java.util.ArrayList)1 AssertionFailure (org.hibernate.AssertionFailure)1 MappingException (org.hibernate.MappingException)1 MappedSuperclass (org.hibernate.mapping.MappedSuperclass)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 AbstractIdentifiableType (org.hibernate.metamodel.model.domain.AbstractIdentifiableType)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 ManagedDomainType (org.hibernate.metamodel.model.domain.ManagedDomainType)1 MappedSuperclassDomainType (org.hibernate.metamodel.model.domain.MappedSuperclassDomainType)1 PersistentAttribute (org.hibernate.metamodel.model.domain.PersistentAttribute)1 SingularPersistentAttribute (org.hibernate.metamodel.model.domain.SingularPersistentAttribute)1