Search in sources :

Example 1 with AssociationKey

use of org.hibernate.persister.walking.spi.AssociationKey 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 AssociationKey

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

the class AbstractLoadPlanBuildingAssociationVisitationStrategy method startingCollection.

@Override
public void startingCollection(CollectionDefinition collectionDefinition) {
    // see if the EntityDefinition is a root...
    final boolean isRoot = fetchSourceStack.isEmpty();
    if (!isRoot) {
        // if not, this call should represent a fetch which should have been handled in #startingAttribute
        return;
    }
    log.tracef("%s Starting root collection : %s", StringHelper.repeat(">>", fetchSourceStack.size()), collectionDefinition.getCollectionPersister().getRole());
    // if we get here, it is a root
    if (!supportsRootCollectionReturns()) {
        throw new HibernateException("This strategy does not support root collection returns");
    }
    final CollectionReturn collectionReturn = new CollectionReturnImpl(collectionDefinition, querySpaces);
    pushToCollectionStack(collectionReturn);
    addRootReturn(collectionReturn);
    associationKeyRegistered(new AssociationKey(((Joinable) collectionDefinition.getCollectionPersister()).getTableName(), ((Joinable) collectionDefinition.getCollectionPersister()).getKeyColumnNames()));
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) HibernateException(org.hibernate.HibernateException) CollectionReturn(org.hibernate.loader.plan.spi.CollectionReturn) Joinable(org.hibernate.persister.entity.Joinable) CollectionReturnImpl(org.hibernate.loader.plan.build.internal.returns.CollectionReturnImpl)

Example 3 with AssociationKey

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

the class AbstractLoadPlanBuildingAssociationVisitationStrategy method startingEntity.

// Entities  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public void startingEntity(EntityDefinition entityDefinition) {
    // see if the EntityDefinition is a root...
    final boolean isRoot = fetchSourceStack.isEmpty();
    if (!isRoot) {
        // if not, this call should represent a fetch which should have been handled in #startingAttribute
        return;
    }
    // if we get here, it is a root
    log.tracef("%s Starting root entity : %s", StringHelper.repeat(">>", fetchSourceStack.size()), entityDefinition.getEntityPersister().getEntityName());
    if (!supportsRootEntityReturns()) {
        throw new HibernateException("This strategy does not support root entity returns");
    }
    final EntityReturnImpl entityReturn = new EntityReturnImpl(entityDefinition, querySpaces);
    addRootReturn(entityReturn);
    pushToStack(entityReturn);
    // also add an AssociationKey for the root so we can later on recognize circular references back to the root.
    final Joinable entityPersister = (Joinable) entityDefinition.getEntityPersister();
    associationKeyRegistered(new AssociationKey(entityPersister.getTableName(), entityPersister.getKeyColumnNames()));
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) HibernateException(org.hibernate.HibernateException) Joinable(org.hibernate.persister.entity.Joinable) EntityReturnImpl(org.hibernate.loader.plan.build.internal.returns.EntityReturnImpl)

Example 4 with AssociationKey

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

the class AbstractCompositionAttribute method getAttributes.

@Override
public Iterable<AttributeDefinition> getAttributes() {
    return new Iterable<AttributeDefinition>() {

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

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

                private int currentSubAttributeNumber;

                private int currentColumnPosition = columnStartPosition;

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

                @Override
                public AttributeDefinition next() {
                    final int subAttributeNumber = currentSubAttributeNumber;
                    currentSubAttributeNumber++;
                    final String name = getType().getPropertyNames()[subAttributeNumber];
                    final Type type = getType().getSubtypes()[subAttributeNumber];
                    int columnPosition = currentColumnPosition;
                    currentColumnPosition += type.getColumnSpan(sessionFactory());
                    if (type.isAssociationType()) {
                        // we build the association-key here because of the "goofiness" with 'currentColumnPosition'
                        final AssociationKey associationKey;
                        final AssociationType aType = (AssociationType) type;
                        final Joinable joinable = aType.getAssociatedJoinable(sessionFactory());
                        if (aType.isAnyType()) {
                            associationKey = new AssociationKey(JoinHelper.getLHSTableName(aType, attributeNumber(), (OuterJoinLoadable) locateOwningPersister()), JoinHelper.getLHSColumnNames(aType, attributeNumber(), columnPosition, (OuterJoinLoadable) locateOwningPersister(), sessionFactory()));
                        } else if (aType.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT) {
                            final String lhsTableName;
                            final String[] lhsColumnNames;
                            if (joinable.isCollection()) {
                                final QueryableCollection collectionPersister = (QueryableCollection) joinable;
                                lhsTableName = collectionPersister.getTableName();
                                lhsColumnNames = collectionPersister.getElementColumnNames();
                            } else {
                                final OuterJoinLoadable entityPersister = (OuterJoinLoadable) locateOwningPersister();
                                lhsTableName = getLHSTableName(aType, attributeNumber(), entityPersister);
                                lhsColumnNames = getLHSColumnNames(aType, attributeNumber(), columnPosition, entityPersister, sessionFactory());
                            }
                            associationKey = new AssociationKey(lhsTableName, lhsColumnNames);
                        } else {
                            associationKey = new AssociationKey(joinable.getTableName(), getRHSColumnNames(aType, sessionFactory()));
                        }
                        final CompositeType cType = getType();
                        final boolean nullable = cType.getPropertyNullability() == null || cType.getPropertyNullability()[subAttributeNumber];
                        return new CompositeBasedAssociationAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (AssociationType) type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation(), subAttributeNumber, associationKey);
                    } else if (type.isComponentType()) {
                        return new CompositionBasedCompositionAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (CompositeType) type, columnPosition, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(getType().getPropertyNullability()[subAttributeNumber]).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
                    } else {
                        final CompositeType cType = getType();
                        final boolean nullable = cType.getPropertyNullability() == null || cType.getPropertyNullability()[subAttributeNumber];
                        return new CompositeBasedBasicAttribute(AbstractCompositionAttribute.this, sessionFactory(), subAttributeNumber, name, type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
                    }
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Remove operation not supported here");
                }
            };
        }
    };
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AttributeDefinition(org.hibernate.persister.walking.spi.AttributeDefinition) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) Iterator(java.util.Iterator) BaselineAttributeInformation(org.hibernate.tuple.BaselineAttributeInformation) CompositeType(org.hibernate.type.CompositeType)

Example 5 with AssociationKey

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

the class EntityBasedAssociationAttribute method getAssociationKey.

@Override
public AssociationKey getAssociationKey() {
    final AssociationType type = getType();
    if (type.isAnyType()) {
        return new AssociationKey(JoinHelper.getLHSTableName(type, attributeNumber(), (OuterJoinLoadable) getSource()), JoinHelper.getLHSColumnNames(type, attributeNumber(), 0, (OuterJoinLoadable) getSource(), sessionFactory()));
    }
    final Joinable joinable = type.getAssociatedJoinable(sessionFactory());
    if (type.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT) {
        final String lhsTableName;
        final String[] lhsColumnNames;
        if (joinable.isCollection()) {
            final QueryableCollection collectionPersister = (QueryableCollection) joinable;
            lhsTableName = collectionPersister.getTableName();
            lhsColumnNames = collectionPersister.getElementColumnNames();
        } else {
            final OuterJoinLoadable entityPersister = (OuterJoinLoadable) source();
            lhsTableName = getLHSTableName(type, attributeNumber(), entityPersister);
            lhsColumnNames = getLHSColumnNames(type, attributeNumber(), entityPersister, sessionFactory());
        }
        return new AssociationKey(lhsTableName, lhsColumnNames);
    } else {
        return new AssociationKey(joinable.getTableName(), getRHSColumnNames(type, sessionFactory()));
    }
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Aggregations

AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)6 Joinable (org.hibernate.persister.entity.Joinable)5 AssociationType (org.hibernate.type.AssociationType)3 Iterator (java.util.Iterator)2 HibernateException (org.hibernate.HibernateException)2 FetchStrategy (org.hibernate.engine.FetchStrategy)2 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)2 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)2 AttributeDefinition (org.hibernate.persister.walking.spi.AttributeDefinition)2 CompositeType (org.hibernate.type.CompositeType)2 Type (org.hibernate.type.Type)2 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)1 PropertyPath (org.hibernate.loader.PropertyPath)1 CollectionReturnImpl (org.hibernate.loader.plan.build.internal.returns.CollectionReturnImpl)1 EntityReturnImpl (org.hibernate.loader.plan.build.internal.returns.EntityReturnImpl)1 ExpandingFetchSource (org.hibernate.loader.plan.build.spi.ExpandingFetchSource)1 CollectionReturn (org.hibernate.loader.plan.spi.CollectionReturn)1 FetchSource (org.hibernate.loader.plan.spi.FetchSource)1 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1