Search in sources :

Example 1 with AbstractIdentifiableType

use of org.hibernate.metamodel.model.domain.AbstractIdentifiableType 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)

Aggregations

HashSet (java.util.HashSet)1 Set (java.util.Set)1 MappingException (org.hibernate.MappingException)1 Component (org.hibernate.mapping.Component)1 Property (org.hibernate.mapping.Property)1 AbstractIdentifiableType (org.hibernate.metamodel.model.domain.AbstractIdentifiableType)1 AttributeContainer (org.hibernate.metamodel.model.domain.internal.AttributeContainer)1