Search in sources :

Example 1 with VirtualModelPart

use of org.hibernate.metamodel.mapping.VirtualModelPart in project hibernate-orm by hibernate.

the class AbstractEntityPersister method findSubPart.

@Override
public ModelPart findSubPart(String name, EntityMappingType treatTargetType) {
    LOG.tracef("#findSubPart(`%s`)", name);
    if (EntityDiscriminatorMapping.matchesRoleName(name)) {
        return discriminatorMapping;
    }
    final AttributeMapping declaredAttribute = declaredAttributeMappings.get(name);
    if (declaredAttribute != null) {
        return declaredAttribute;
    }
    if (superMappingType != null) {
        final ModelPart superDefinedAttribute = superMappingType.findSubPart(name, superMappingType);
        if (superDefinedAttribute != null) {
            // Prefer the identifier mapping of the concrete class
            if (superDefinedAttribute instanceof EntityIdentifierMapping) {
                final ModelPart identifierModelPart = getIdentifierModelPart(name, treatTargetType);
                if (identifierModelPart != null) {
                    return identifierModelPart;
                }
            }
            return superDefinedAttribute;
        }
    }
    if (treatTargetType != null) {
        if (!treatTargetType.isTypeOrSuperType(this)) {
            return null;
        }
        if (subclassMappingTypes != null && !subclassMappingTypes.isEmpty()) {
            for (EntityMappingType subMappingType : subclassMappingTypes.values()) {
                if (!treatTargetType.isTypeOrSuperType(subMappingType)) {
                    continue;
                }
                final ModelPart subDefinedAttribute = subMappingType.findSubTypesSubPart(name, treatTargetType);
                if (subDefinedAttribute != null) {
                    return subDefinedAttribute;
                }
            }
        }
    } else {
        if (subclassMappingTypes != null && !subclassMappingTypes.isEmpty()) {
            ModelPart attribute = null;
            for (EntityMappingType subMappingType : subclassMappingTypes.values()) {
                final ModelPart subDefinedAttribute = subMappingType.findSubTypesSubPart(name, treatTargetType);
                if (subDefinedAttribute != null) {
                    if (attribute != null && !MappingModelHelper.isCompatibleModelPart(attribute, subDefinedAttribute)) {
                        throw new IllegalArgumentException(new SemanticException(String.format(Locale.ROOT, "Could not resolve attribute '%s' of '%s' due to the attribute being declared in multiple sub types: ['%s', '%s']", name, getJavaType().getJavaType().getTypeName(), ((AttributeMapping) attribute).getDeclaringType().getJavaType().getJavaType().getTypeName(), ((AttributeMapping) subDefinedAttribute).getDeclaringType().getJavaType().getJavaType().getTypeName())));
                    }
                    attribute = subDefinedAttribute;
                }
            }
            if (attribute != null) {
                return attribute;
            }
        }
    }
    final ModelPart identifierModelPart = getIdentifierModelPart(name, treatTargetType);
    if (identifierModelPart != null) {
        return identifierModelPart;
    }
    for (AttributeMapping attribute : declaredAttributeMappings.values()) {
        if (attribute instanceof EmbeddableValuedModelPart && attribute instanceof VirtualModelPart) {
            final ModelPart subPart = ((EmbeddableValuedModelPart) attribute).findSubPart(name, null);
            if (subPart != null) {
                return subPart;
            }
        }
    }
    return null;
}
Also used : ModelPart(org.hibernate.metamodel.mapping.ModelPart) DiscriminatedAssociationModelPart(org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) DiscriminatedAssociationAttributeMapping(org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) InFlightEntityMappingType(org.hibernate.metamodel.mapping.internal.InFlightEntityMappingType) SemanticException(org.hibernate.query.SemanticException) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart)

Aggregations

AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)1 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)1 DiscriminatedAssociationModelPart (org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart)1 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)1 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)1 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)1 ModelPart (org.hibernate.metamodel.mapping.ModelPart)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)1 VirtualModelPart (org.hibernate.metamodel.mapping.VirtualModelPart)1 DiscriminatedAssociationAttributeMapping (org.hibernate.metamodel.mapping.internal.DiscriminatedAssociationAttributeMapping)1 EmbeddedAttributeMapping (org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping)1 InFlightEntityMappingType (org.hibernate.metamodel.mapping.internal.InFlightEntityMappingType)1 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)1 SemanticException (org.hibernate.query.SemanticException)1