Search in sources :

Example 1 with AssociationAttributeDefinition

use of org.hibernate.persister.walking.spi.AssociationAttributeDefinition in project hibernate-orm by hibernate.

the class AbstractLoadPlanBuildingAssociationVisitationStrategy method handleAssociationAttribute.

protected boolean handleAssociationAttribute(AssociationAttributeDefinition attributeDefinition) {
    // todo : this seems to not be correct for one-to-one
    final FetchStrategy fetchStrategy = determineFetchStrategy(attributeDefinition);
    final ExpandingFetchSource currentSource = currentSource();
    currentSource.validateFetchPlan(fetchStrategy, attributeDefinition);
    final AssociationAttributeDefinition.AssociationNature nature = attributeDefinition.getAssociationNature();
    if (nature == AssociationAttributeDefinition.AssociationNature.ANY) {
        // for ANY mappings we need to build a Fetch:
        //		1) fetch type is SELECT
        //		2) (because the fetch cannot be a JOIN...) do not push it to the stack
        // regardless of the fetch style, build the fetch
        currentSource.buildAnyAttributeFetch(attributeDefinition, fetchStrategy);
        return false;
    } else if (nature == AssociationAttributeDefinition.AssociationNature.ENTITY) {
        // regardless of the fetch style, build the fetch
        EntityFetch fetch = currentSource.buildEntityAttributeFetch(attributeDefinition, fetchStrategy);
        if (FetchStrategyHelper.isJoinFetched(fetchStrategy)) {
            // only push to the stack if join fetched
            pushToStack((ExpandingFetchSource) fetch);
            return true;
        } else {
            return false;
        }
    } else {
        // Collection
        // regardless of the fetch style, build the fetch
        CollectionAttributeFetch fetch = currentSource.buildCollectionAttributeFetch(attributeDefinition, fetchStrategy);
        if (FetchStrategyHelper.isJoinFetched(fetchStrategy)) {
            // only push to the stack if join fetched
            pushToCollectionStack(fetch);
            return true;
        } else {
            return false;
        }
    }
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) CollectionAttributeFetch(org.hibernate.loader.plan.spi.CollectionAttributeFetch) FetchStrategy(org.hibernate.engine.FetchStrategy) ExpandingFetchSource(org.hibernate.loader.plan.build.spi.ExpandingFetchSource) AssociationAttributeDefinition(org.hibernate.persister.walking.spi.AssociationAttributeDefinition)

Example 2 with AssociationAttributeDefinition

use of org.hibernate.persister.walking.spi.AssociationAttributeDefinition in project hibernate-orm by hibernate.

the class AbstractLoadPlanBuildingAssociationVisitationStrategy method startingAttribute.

@Override
public boolean startingAttribute(AttributeDefinition attributeDefinition) {
    log.tracef("%s Starting attribute %s", StringHelper.repeat(">>", fetchSourceStack.size()), attributeDefinition);
    final Type attributeType = attributeDefinition.getType();
    final boolean isComponentType = attributeType.isComponentType();
    final boolean isAssociationType = attributeType.isAssociationType();
    final boolean isBasicType = !(isComponentType || isAssociationType);
    currentPropertyPath = currentPropertyPath.append(attributeDefinition.getName());
    if (isBasicType) {
        return true;
    } else if (isAssociationType) {
        // also handles any type attributes...
        return handleAssociationAttribute((AssociationAttributeDefinition) attributeDefinition);
    } else {
        return handleCompositeAttribute(attributeDefinition);
    }
}
Also used : Type(org.hibernate.type.Type) AssociationAttributeDefinition(org.hibernate.persister.walking.spi.AssociationAttributeDefinition)

Example 3 with AssociationAttributeDefinition

use of org.hibernate.persister.walking.spi.AssociationAttributeDefinition in project hibernate-orm by hibernate.

the class AbstractLoadPlanBuildingAssociationVisitationStrategy method finishingAttribute.

@Override
public void finishingAttribute(AttributeDefinition attributeDefinition) {
    final Type attributeType = attributeDefinition.getType();
    if (attributeType.isAssociationType()) {
        final AssociationAttributeDefinition associationAttributeDefinition = (AssociationAttributeDefinition) attributeDefinition;
        if (attributeType.isAnyType()) {
        // Nothing to do because AnyFetch does not implement ExpandingFetchSource (i.e., it cannot be pushed/popped).
        } else if (attributeType.isEntityType()) {
            final ExpandingFetchSource source = currentSource();
            // associationAttributeDefinition.
            if (AttributeFetch.class.isInstance(source) && associationAttributeDefinition.equals(AttributeFetch.class.cast(source).getFetchedAttributeDefinition())) {
                final ExpandingFetchSource popped = popFromStack();
                checkPoppedEntity(popped, associationAttributeDefinition.toEntityDefinition());
            }
        } else if (attributeType.isCollectionType()) {
            final CollectionReference currentCollection = currentCollection();
            // associationAttributeDefinition.
            if (AttributeFetch.class.isInstance(currentCollection) && associationAttributeDefinition.equals(AttributeFetch.class.cast(currentCollection).getFetchedAttributeDefinition())) {
                final CollectionReference popped = popFromCollectionStack();
                checkedPoppedCollection(popped, associationAttributeDefinition.toCollectionDefinition());
            }
        }
    } else if (attributeType.isComponentType()) {
        // CompositeFetch is always pushed, during #startingAttribute(),
        // so pop the current fetch owner, and make sure what we just popped represents this composition
        final ExpandingFetchSource popped = popFromStack();
        if (!CompositeAttributeFetch.class.isInstance(popped)) {
            throw new WalkingException(String.format("Mismatched FetchSource from stack on pop; expected: CompositeAttributeFetch; actual: [%s]", popped));
        }
        final CompositeAttributeFetch poppedAsCompositeAttributeFetch = (CompositeAttributeFetch) popped;
        if (!attributeDefinition.equals(poppedAsCompositeAttributeFetch.getFetchedAttributeDefinition())) {
            throw new WalkingException(String.format("Mismatched CompositeAttributeFetch from stack on pop; expected fetch for attribute: [%s]; actual: [%s]", attributeDefinition, poppedAsCompositeAttributeFetch.getFetchedAttributeDefinition()));
        }
    }
    log.tracef("%s Finishing up attribute : %s", StringHelper.repeat("<<", fetchSourceStack.size()), attributeDefinition);
    currentPropertyPath = currentPropertyPath.getParent();
}
Also used : Type(org.hibernate.type.Type) ExpandingFetchSource(org.hibernate.loader.plan.build.spi.ExpandingFetchSource) CompositeAttributeFetch(org.hibernate.loader.plan.spi.CompositeAttributeFetch) AttributeFetch(org.hibernate.loader.plan.spi.AttributeFetch) CollectionAttributeFetch(org.hibernate.loader.plan.spi.CollectionAttributeFetch) WalkingException(org.hibernate.persister.walking.spi.WalkingException) AssociationAttributeDefinition(org.hibernate.persister.walking.spi.AssociationAttributeDefinition) CollectionReference(org.hibernate.loader.plan.spi.CollectionReference) CompositeAttributeFetch(org.hibernate.loader.plan.spi.CompositeAttributeFetch)

Example 4 with AssociationAttributeDefinition

use of org.hibernate.persister.walking.spi.AssociationAttributeDefinition in project hibernate-orm by hibernate.

the class CompositionSingularSubAttributesHelper method getSingularSubAttributes.

private static Iterable<AttributeDefinition> getSingularSubAttributes(final AttributeSource source, final OuterJoinLoadable ownerEntityPersister, final CompositeType compositeType, final String lhsTableName, final String[] lhsColumns) {
    return new Iterable<AttributeDefinition>() {

        @Override
        public Iterator<AttributeDefinition> iterator() {
            return new Iterator<AttributeDefinition>() {

                private final int numberOfAttributes = compositeType.getSubtypes().length;

                private int currentSubAttributeNumber;

                private int currentColumnPosition;

                @Override
                public boolean hasNext() {
                    return currentSubAttributeNumber < numberOfAttributes;
                }

                @Override
                public AttributeDefinition next() {
                    final int subAttributeNumber = currentSubAttributeNumber;
                    currentSubAttributeNumber++;
                    final String name = compositeType.getPropertyNames()[subAttributeNumber];
                    final Type type = compositeType.getSubtypes()[subAttributeNumber];
                    final int columnPosition = currentColumnPosition;
                    final int columnSpan = type.getColumnSpan(ownerEntityPersister.getFactory());
                    final String[] subAttributeLhsColumns = ArrayHelper.slice(lhsColumns, columnPosition, columnSpan);
                    final boolean[] propertyNullability = compositeType.getPropertyNullability();
                    final boolean nullable = propertyNullability == null || propertyNullability[subAttributeNumber];
                    currentColumnPosition += columnSpan;
                    if (type.isAssociationType()) {
                        final AssociationType aType = (AssociationType) type;
                        return new AssociationAttributeDefinition() {

                            @Override
                            public AssociationKey getAssociationKey() {
                                return new AssociationKey(lhsTableName, subAttributeLhsColumns);
                            }

                            @Override
                            public AssociationNature getAssociationNature() {
                                if (type.isAnyType()) {
                                    return AssociationNature.ANY;
                                } else {
                                    // cannot be a collection
                                    return AssociationNature.ENTITY;
                                }
                            }

                            @Override
                            public EntityDefinition toEntityDefinition() {
                                if (getAssociationNature() != AssociationNature.ENTITY) {
                                    throw new WalkingException("Cannot build EntityDefinition from non-entity-typed attribute");
                                }
                                return (EntityPersister) aType.getAssociatedJoinable(ownerEntityPersister.getFactory());
                            }

                            @Override
                            public AnyMappingDefinition toAnyDefinition() {
                                if (getAssociationNature() != AssociationNature.ANY) {
                                    throw new WalkingException("Cannot build AnyMappingDefinition from non-any-typed attribute");
                                }
                                // todo : not sure how lazy is propogated into the component for a subattribute of type any
                                return new StandardAnyTypeDefinition((AnyType) aType, false);
                            }

                            @Override
                            public CollectionDefinition toCollectionDefinition() {
                                throw new WalkingException("A collection cannot be mapped to a composite ID sub-attribute.");
                            }

                            @Override
                            public FetchStrategy determineFetchPlan(LoadQueryInfluencers loadQueryInfluencers, PropertyPath propertyPath) {
                                return new FetchStrategy(FetchTiming.IMMEDIATE, FetchStyle.JOIN);
                            }

                            @Override
                            public CascadeStyle determineCascadeStyle() {
                                return CascadeStyles.NONE;
                            }

                            @Override
                            public HydratedCompoundValueHandler getHydratedCompoundValueExtractor() {
                                return null;
                            }

                            @Override
                            public String getName() {
                                return name;
                            }

                            @Override
                            public AssociationType getType() {
                                return aType;
                            }

                            @Override
                            public boolean isNullable() {
                                return nullable;
                            }

                            @Override
                            public AttributeSource getSource() {
                                return source;
                            }
                        };
                    } else if (type.isComponentType()) {
                        return new CompositionDefinition() {

                            @Override
                            public String getName() {
                                return name;
                            }

                            @Override
                            public CompositeType getType() {
                                return (CompositeType) type;
                            }

                            @Override
                            public boolean isNullable() {
                                return nullable;
                            }

                            @Override
                            public AttributeSource getSource() {
                                return source;
                            }

                            @Override
                            public Iterable<AttributeDefinition> getAttributes() {
                                return CompositionSingularSubAttributesHelper.getSingularSubAttributes(this, ownerEntityPersister, (CompositeType) type, lhsTableName, subAttributeLhsColumns);
                            }
                        };
                    } else {
                        return new AttributeDefinition() {

                            @Override
                            public String getName() {
                                return name;
                            }

                            @Override
                            public Type getType() {
                                return type;
                            }

                            @Override
                            public boolean isNullable() {
                                return nullable;
                            }

                            @Override
                            public AttributeSource getSource() {
                                return source;
                            }
                        };
                    }
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Remove operation not supported here");
                }
            };
        }
    };
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) AttributeSource(org.hibernate.persister.walking.spi.AttributeSource) FetchStrategy(org.hibernate.engine.FetchStrategy) AssociationAttributeDefinition(org.hibernate.persister.walking.spi.AssociationAttributeDefinition) AttributeDefinition(org.hibernate.persister.walking.spi.AttributeDefinition) WalkingException(org.hibernate.persister.walking.spi.WalkingException) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) AnyType(org.hibernate.type.AnyType) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) Iterator(java.util.Iterator) PropertyPath(org.hibernate.loader.PropertyPath) AssociationAttributeDefinition(org.hibernate.persister.walking.spi.AssociationAttributeDefinition) CompositionDefinition(org.hibernate.persister.walking.spi.CompositionDefinition) CompositeType(org.hibernate.type.CompositeType)

Aggregations

AssociationAttributeDefinition (org.hibernate.persister.walking.spi.AssociationAttributeDefinition)4 Type (org.hibernate.type.Type)3 FetchStrategy (org.hibernate.engine.FetchStrategy)2 ExpandingFetchSource (org.hibernate.loader.plan.build.spi.ExpandingFetchSource)2 CollectionAttributeFetch (org.hibernate.loader.plan.spi.CollectionAttributeFetch)2 WalkingException (org.hibernate.persister.walking.spi.WalkingException)2 Iterator (java.util.Iterator)1 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)1 PropertyPath (org.hibernate.loader.PropertyPath)1 AttributeFetch (org.hibernate.loader.plan.spi.AttributeFetch)1 CollectionReference (org.hibernate.loader.plan.spi.CollectionReference)1 CompositeAttributeFetch (org.hibernate.loader.plan.spi.CompositeAttributeFetch)1 EntityFetch (org.hibernate.loader.plan.spi.EntityFetch)1 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)1 AttributeDefinition (org.hibernate.persister.walking.spi.AttributeDefinition)1 AttributeSource (org.hibernate.persister.walking.spi.AttributeSource)1 CompositionDefinition (org.hibernate.persister.walking.spi.CompositionDefinition)1 AnyType (org.hibernate.type.AnyType)1