Search in sources :

Example 1 with FetchStrategy

use of org.hibernate.engine.FetchStrategy in project hibernate-orm by hibernate.

the class AbstractLoadPlanBuildingAssociationVisitationStrategy method foundCircularAssociation.

@Override
public void foundCircularAssociation(AssociationAttributeDefinition attributeDefinition) {
    final FetchStrategy fetchStrategy = determineFetchStrategy(attributeDefinition);
    if (fetchStrategy.getStyle() != FetchStyle.JOIN) {
        // nothing to do
        return;
    }
    final AssociationKey associationKey = attributeDefinition.getAssociationKey();
    // go ahead and build the bidirectional fetch
    if (attributeDefinition.getAssociationNature() == AssociationAttributeDefinition.AssociationNature.ENTITY) {
        final Joinable currentEntityPersister = (Joinable) currentSource().resolveEntityReference().getEntityPersister();
        final AssociationKey currentEntityReferenceAssociationKey = new AssociationKey(currentEntityPersister.getTableName(), currentEntityPersister.getKeyColumnNames());
        // if associationKey is equal to currentEntityReferenceAssociationKey
        // that means that the current EntityPersister has a single primary key attribute
        // (i.e., derived attribute) which is mapped by attributeDefinition.
        // This is not a bidirectional association.
        // TODO: AFAICT, to avoid an overflow, the associated entity must already be loaded into the session, or
        // it must be loaded when the ID for the dependent entity is resolved. Is there some other way to
        // deal with this???
        final FetchSource registeredFetchSource = registeredFetchSource(associationKey);
        if (registeredFetchSource != null && !associationKey.equals(currentEntityReferenceAssociationKey)) {
            currentSource().buildBidirectionalEntityReference(attributeDefinition, fetchStrategy, registeredFetchSource(associationKey).resolveEntityReference());
        }
    } else {
    // Do nothing for collection
    }
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) FetchSource(org.hibernate.loader.plan.spi.FetchSource) ExpandingFetchSource(org.hibernate.loader.plan.build.spi.ExpandingFetchSource) FetchStrategy(org.hibernate.engine.FetchStrategy) Joinable(org.hibernate.persister.entity.Joinable)

Example 2 with FetchStrategy

use of org.hibernate.engine.FetchStrategy in project hibernate-orm by hibernate.

the class CompositeBasedAssociationAttribute method determineFetchPlan.

@Override
public FetchStrategy determineFetchPlan(LoadQueryInfluencers loadQueryInfluencers, PropertyPath propertyPath) {
    final EntityPersister owningPersister = getSource().locateOwningPersister();
    FetchStyle style = FetchStrategyHelper.determineFetchStyleByProfile(loadQueryInfluencers, owningPersister, propertyPath, attributeNumber());
    if (style == null) {
        style = determineFetchStyleByMetadata(getFetchMode(), getType());
    }
    return new FetchStrategy(determineFetchTiming(style), style);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) FetchStyle(org.hibernate.engine.FetchStyle) FetchStrategy(org.hibernate.engine.FetchStrategy)

Example 3 with FetchStrategy

use of org.hibernate.engine.FetchStrategy in project hibernate-orm by hibernate.

the class AbstractEntityGraphVisitationStrategy method foundCircularAssociation.

@Override
public void foundCircularAssociation(AssociationAttributeDefinition attributeDefinition) {
    final FetchStrategy fetchStrategy = determineFetchStrategy(attributeDefinition);
    if (fetchStrategy.getStyle() != FetchStyle.JOIN) {
        // nothing to do
        return;
    }
    // Bi-directional association & the owning side was already visited.  If the current attribute node refers
    // to it, fetch.
    // ENTITY nature handled by super.
    final GraphNodeImplementor graphNode = graphStack.peekLast();
    if (attributeDefinition.getAssociationNature() == AssociationAttributeDefinition.AssociationNature.COLLECTION && !graphNode.equals(NON_EXIST_SUBGRAPH_NODE) && graphNode.containsAttribute(attributeDefinition.getName())) {
        currentSource().buildCollectionAttributeFetch(attributeDefinition, fetchStrategy);
    }
    super.foundCircularAssociation(attributeDefinition);
}
Also used : FetchStrategy(org.hibernate.engine.FetchStrategy) GraphNodeImplementor(org.hibernate.graph.spi.GraphNodeImplementor)

Example 4 with FetchStrategy

use of org.hibernate.engine.FetchStrategy in project hibernate-orm by hibernate.

the class EntityBasedAssociationAttribute method determineFetchPlan.

@Override
public FetchStrategy determineFetchPlan(LoadQueryInfluencers loadQueryInfluencers, PropertyPath propertyPath) {
    final EntityPersister owningPersister = getSource().getEntityPersister();
    FetchStyle style = FetchStrategyHelper.determineFetchStyleByProfile(loadQueryInfluencers, owningPersister, propertyPath, attributeNumber());
    if (style == null) {
        style = FetchStrategyHelper.determineFetchStyleByMetadata(((OuterJoinLoadable) getSource().getEntityPersister()).getFetchMode(attributeNumber()), getType(), sessionFactory());
    }
    return new FetchStrategy(FetchStrategyHelper.determineFetchTiming(style, getType(), sessionFactory()), style);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) FetchStyle(org.hibernate.engine.FetchStyle) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) FetchStrategy(org.hibernate.engine.FetchStrategy)

Example 5 with FetchStrategy

use of org.hibernate.engine.FetchStrategy 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

FetchStrategy (org.hibernate.engine.FetchStrategy)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 FetchStyle (org.hibernate.engine.FetchStyle)2 ExpandingFetchSource (org.hibernate.loader.plan.build.spi.ExpandingFetchSource)2 AssociationAttributeDefinition (org.hibernate.persister.walking.spi.AssociationAttributeDefinition)2 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)2 Iterator (java.util.Iterator)1 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)1 GraphNodeImplementor (org.hibernate.graph.spi.GraphNodeImplementor)1 PropertyPath (org.hibernate.loader.PropertyPath)1 CollectionAttributeFetch (org.hibernate.loader.plan.spi.CollectionAttributeFetch)1 EntityFetch (org.hibernate.loader.plan.spi.EntityFetch)1 FetchSource (org.hibernate.loader.plan.spi.FetchSource)1 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)1 Joinable (org.hibernate.persister.entity.Joinable)1 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)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 WalkingException (org.hibernate.persister.walking.spi.WalkingException)1