Search in sources :

Example 1 with AttributeClassification

use of org.hibernate.metamodel.AttributeClassification in project hibernate-orm by hibernate.

the class AttributeFactory method determineAttributeMetadata.

private static <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata(AttributeContext<X> attributeContext, MemberResolver memberResolver, MetadataContext context) {
    final Property propertyMapping = attributeContext.getPropertyMapping();
    final String propertyName = propertyMapping.getName();
    LOG.tracef("Starting attribute metadata determination [%s]", propertyName);
    final Member member = memberResolver.resolveMember(attributeContext, context);
    LOG.tracef("    Determined member [%s]", member);
    final Value value = propertyMapping.getValue();
    final org.hibernate.type.Type type = value.getType();
    LOG.tracef("    Determined type [name=%s, class=%s]", type.getName(), type.getClass().getName());
    if (type.isAnyType()) {
        return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.ANY, context);
    } else if (type.isAssociationType()) {
        // collection or entity
        if (type.isEntityType()) {
            // entity
            return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, determineSingularAssociationClassification(member), context);
        }
        // collection
        if (value instanceof Collection) {
            final Collection collValue = (Collection) value;
            final Value elementValue = collValue.getElement();
            final org.hibernate.type.Type elementType = elementValue.getType();
            final boolean isManyToMany = isManyToMany(member);
            // First, determine the type of the elements and use that to help determine the
            // collection type
            final AttributeClassification elementClassification;
            final AttributeClassification attributeClassification;
            if (elementType.isAnyType()) {
                attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
                elementClassification = AttributeClassification.ANY;
            } else if (elementValue instanceof Component) {
                elementClassification = AttributeClassification.EMBEDDED;
                attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
            } else if (elementType.isAssociationType()) {
                elementClassification = isManyToMany ? AttributeClassification.MANY_TO_MANY : AttributeClassification.ONE_TO_MANY;
                attributeClassification = elementClassification;
            } else {
                elementClassification = AttributeClassification.BASIC;
                attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
            }
            final AttributeClassification indexClassification;
            // Finally, we determine the type of the map key (if needed)
            if (value instanceof Map) {
                final Value keyValue = ((Map) value).getIndex();
                final org.hibernate.type.Type keyType = keyValue.getType();
                if (keyType.isAnyType()) {
                    indexClassification = AttributeClassification.ANY;
                } else if (keyValue instanceof Component) {
                    indexClassification = AttributeClassification.EMBEDDED;
                } else if (keyType.isAssociationType()) {
                    indexClassification = AttributeClassification.MANY_TO_ONE;
                } else {
                    indexClassification = AttributeClassification.BASIC;
                }
            } else if (value instanceof List) {
                indexClassification = AttributeClassification.BASIC;
            } else {
                indexClassification = null;
            }
            return new PluralAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, attributeClassification, elementClassification, indexClassification, context);
        } else if (value instanceof OneToMany) {
            // element value within a o.h.mapping.Collection (see logic branch above)
            throw new IllegalArgumentException("HUH???");
        // final boolean isManyToMany = isManyToMany( member );
        // //one to many with FK => entity
        // return new PluralAttributeMetadataImpl(
        // attributeContext.getPropertyMapping(),
        // attributeContext.getOwnerType(),
        // member,
        // isManyToMany
        // ? Attribute.PersistentAttributeType.MANY_TO_MANY
        // : Attribute.PersistentAttributeType.ONE_TO_MANY
        // value,
        // AttributeContext.TypeStatus.ENTITY,
        // Attribute.PersistentAttributeType.ONE_TO_MANY,
        // null, null, null
        // );
        }
    } else if (propertyMapping.isComposite()) {
        // component
        return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.EMBEDDED, context);
    } else {
        // basic type
        return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.BASIC, context);
    }
    throw new UnsupportedMappingException("oops, we are missing something: " + propertyMapping);
}
Also used : AttributeClassification(org.hibernate.metamodel.AttributeClassification) OneToMany(org.hibernate.mapping.OneToMany) UnsupportedMappingException(org.hibernate.metamodel.UnsupportedMappingException) ManagedDomainType(org.hibernate.metamodel.model.domain.ManagedDomainType) AnyType(org.hibernate.type.AnyType) IdentifiableDomainType(org.hibernate.metamodel.model.domain.IdentifiableDomainType) SimpleDomainType(org.hibernate.metamodel.model.domain.SimpleDomainType) JavaType(org.hibernate.type.descriptor.java.JavaType) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) EntityType(org.hibernate.type.EntityType) Type(jakarta.persistence.metamodel.Type) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) AbstractIdentifiableType(org.hibernate.metamodel.model.domain.AbstractIdentifiableType) EmbeddableDomainType(org.hibernate.metamodel.model.domain.EmbeddableDomainType) ParameterizedType(java.lang.reflect.ParameterizedType) DomainType(org.hibernate.metamodel.model.domain.DomainType) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) List(org.hibernate.mapping.List) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) Member(java.lang.reflect.Member) MapMember(org.hibernate.metamodel.model.domain.internal.MapMember) Map(org.hibernate.mapping.Map)

Aggregations

Type (jakarta.persistence.metamodel.Type)1 Member (java.lang.reflect.Member)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Collection (org.hibernate.mapping.Collection)1 Component (org.hibernate.mapping.Component)1 List (org.hibernate.mapping.List)1 Map (org.hibernate.mapping.Map)1 OneToMany (org.hibernate.mapping.OneToMany)1 Property (org.hibernate.mapping.Property)1 Value (org.hibernate.mapping.Value)1 AttributeClassification (org.hibernate.metamodel.AttributeClassification)1 UnsupportedMappingException (org.hibernate.metamodel.UnsupportedMappingException)1 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)1 AbstractIdentifiableType (org.hibernate.metamodel.model.domain.AbstractIdentifiableType)1 DomainType (org.hibernate.metamodel.model.domain.DomainType)1 EmbeddableDomainType (org.hibernate.metamodel.model.domain.EmbeddableDomainType)1 IdentifiableDomainType (org.hibernate.metamodel.model.domain.IdentifiableDomainType)1 ManagedDomainType (org.hibernate.metamodel.model.domain.ManagedDomainType)1 SimpleDomainType (org.hibernate.metamodel.model.domain.SimpleDomainType)1 MapMember (org.hibernate.metamodel.model.domain.internal.MapMember)1