Search in sources :

Example 11 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class SessionImpl method getFilterQueryPlan.

private FilterQueryPlan getFilterQueryPlan(Object collection, String filter, QueryParameters parameters, boolean shallow) throws HibernateException {
    if (collection == null) {
        throw new NullPointerException("null collection passed to filter");
    }
    CollectionEntry entry = persistenceContext.getCollectionEntryOrNull(collection);
    final CollectionPersister roleBeforeFlush = (entry == null) ? null : entry.getLoadedPersister();
    FilterQueryPlan plan = null;
    if (roleBeforeFlush == null) {
        // if it was previously unreferenced, we need to flush in order to
        // get its state into the database in order to execute query
        flush();
        entry = persistenceContext.getCollectionEntryOrNull(collection);
        CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
        if (roleAfterFlush == null) {
            throw new QueryException("The collection was unreferenced");
        }
        plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleAfterFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
    } else {
        // otherwise, we only need to flush if there are in-memory changes
        // to the queried tables
        plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleBeforeFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
        if (autoFlushIfRequired(plan.getQuerySpaces())) {
            // might need to run a different filter entirely afterQuery the flush
            // because the collection role may have changed
            entry = persistenceContext.getCollectionEntryOrNull(collection);
            CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
            if (roleBeforeFlush != roleAfterFlush) {
                if (roleAfterFlush == null) {
                    throw new QueryException("The collection was dereferenced");
                }
                plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleAfterFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
            }
        }
    }
    if (parameters != null) {
        parameters.getPositionalParameterValues()[0] = entry.getLoadedKey();
        parameters.getPositionalParameterTypes()[0] = entry.getLoadedPersister().getKeyType();
    }
    return plan;
}
Also used : FilterQueryPlan(org.hibernate.engine.query.spi.FilterQueryPlan) QueryException(org.hibernate.QueryException) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister)

Example 12 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class BasicLoader method postInstantiate.

protected void postInstantiate() {
    Loadable[] persisters = getEntityPersisters();
    String[] suffixes = getSuffixes();
    descriptors = new EntityAliases[persisters.length];
    for (int i = 0; i < descriptors.length; i++) {
        descriptors[i] = new DefaultEntityAliases(persisters[i], suffixes[i]);
    }
    CollectionPersister[] collectionPersisters = getCollectionPersisters();
    List bagRoles = null;
    if (collectionPersisters != null) {
        String[] collectionSuffixes = getCollectionSuffixes();
        collectionDescriptors = new CollectionAliases[collectionPersisters.length];
        for (int i = 0; i < collectionPersisters.length; i++) {
            if (isBag(collectionPersisters[i])) {
                if (bagRoles == null) {
                    bagRoles = new ArrayList();
                }
                bagRoles.add(collectionPersisters[i].getRole());
            }
            collectionDescriptors[i] = new GeneratedCollectionAliases(collectionPersisters[i], collectionSuffixes[i]);
        }
    } else {
        collectionDescriptors = null;
    }
    if (bagRoles != null && bagRoles.size() > 1) {
        throw new MultipleBagFetchException(bagRoles);
    }
}
Also used : Loadable(org.hibernate.persister.entity.Loadable) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 13 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class AbstractCollectionReference method buildElementGraph.

private CollectionFetchableElement buildElementGraph() {
    final CollectionPersister persister = collectionQuerySpace.getCollectionPersister();
    final Type type = persister.getElementType();
    if (type.isAssociationType()) {
        if (type.isEntityType()) {
            final EntityPersister elementPersister = persister.getFactory().getEntityPersister(((EntityType) type).getAssociatedEntityName());
            final ExpandingEntityQuerySpace entityQuerySpace = QuerySpaceHelper.INSTANCE.makeEntityQuerySpace(collectionQuerySpace, elementPersister, CollectionPropertyNames.COLLECTION_ELEMENTS, (EntityType) persister.getElementType(), collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(), collectionQuerySpace.canJoinsBeRequired(), allowElementJoin);
            return new CollectionFetchableElementEntityGraph(this, entityQuerySpace);
        } else if (type.isAnyType()) {
            return new CollectionFetchableElementAnyGraph(this);
        }
    } else if (type.isComponentType()) {
        final ExpandingCompositeQuerySpace compositeQuerySpace = QuerySpaceHelper.INSTANCE.makeCompositeQuerySpace(collectionQuerySpace, new CompositePropertyMapping((CompositeType) persister.getElementType(), (PropertyMapping) persister, ""), CollectionPropertyNames.COLLECTION_ELEMENTS, (CompositeType) persister.getElementType(), collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(), collectionQuerySpace.canJoinsBeRequired(), allowElementJoin);
        return new CollectionFetchableElementCompositeGraph(this, compositeQuerySpace);
    }
    return null;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ExpandingCompositeQuerySpace(org.hibernate.loader.plan.build.spi.ExpandingCompositeQuerySpace) EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CompositePropertyMapping(org.hibernate.loader.plan.build.internal.spaces.CompositePropertyMapping) ExpandingEntityQuerySpace(org.hibernate.loader.plan.build.spi.ExpandingEntityQuerySpace) CompositeType(org.hibernate.type.CompositeType)

Example 14 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class CriteriaJoinWalker method generateTableAlias.

@Override
protected String generateTableAlias(int n, PropertyPath path, Joinable joinable) {
    // TODO: deal with side-effects (changes to includeInResultRowList, userAliasList, resultTypeList)!!!
    // for collection-of-entity, we are called twice for given "path"
    // once for the collection Joinable, once for the entity Joinable.
    // the second call will/must "consume" the alias + perform side effects according to consumesEntityAlias()
    // for collection-of-other, however, there is only one call 
    // it must "consume" the alias + perform side effects, despite what consumeEntityAlias() return says
    // 
    // note: the logic for adding to the userAliasList is still strictly based on consumesEntityAlias return value
    boolean checkForSqlAlias = joinable.consumesEntityAlias();
    if (!checkForSqlAlias && joinable.isCollection()) {
        // is it a collection-of-other (component or value) ?
        CollectionPersister collectionPersister = (CollectionPersister) joinable;
        Type elementType = collectionPersister.getElementType();
        if (elementType.isComponentType() || !elementType.isEntityType()) {
            checkForSqlAlias = true;
        }
    }
    String sqlAlias = null;
    if (checkForSqlAlias) {
        final Criteria subcriteria = translator.getCriteria(path.getFullPath());
        sqlAlias = subcriteria == null ? null : translator.getSQLAlias(subcriteria);
        if (joinable.consumesEntityAlias() && !translator.hasProjection()) {
            includeInResultRowList.add(subcriteria != null && subcriteria.getAlias() != null);
            if (sqlAlias != null) {
                if (subcriteria.getAlias() != null) {
                    userAliasList.add(subcriteria.getAlias());
                    resultTypeList.add(translator.getResultType(subcriteria));
                }
            }
        }
    }
    if (sqlAlias == null) {
        sqlAlias = super.generateTableAlias(n + translator.getSQLAliasCount(), path, joinable);
    }
    return sqlAlias;
}
Also used : JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Criteria(org.hibernate.Criteria)

Example 15 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class QuerySpaceHelper method makeCollectionQuerySpace.

public ExpandingCollectionQuerySpace makeCollectionQuerySpace(ExpandingQuerySpace lhsQuerySpace, AssociationAttributeDefinition attributeDefinition, String querySpaceUid, FetchStrategy fetchStrategy) {
    final CollectionType fetchedType = (CollectionType) attributeDefinition.getType();
    final CollectionPersister fetchedPersister = attributeDefinition.toCollectionDefinition().getCollectionPersister();
    if (fetchedPersister == null) {
        throw new WalkingException(String.format("Unable to locate CollectionPersister [%s] for fetch [%s]", fetchedType.getRole(), attributeDefinition.getName()));
    }
    final boolean required = lhsQuerySpace.canJoinsBeRequired() && !attributeDefinition.isNullable();
    final ExpandingCollectionQuerySpace rhs = lhsQuerySpace.getExpandingQuerySpaces().makeCollectionQuerySpace(querySpaceUid, fetchedPersister, required);
    if (shouldIncludeJoin(fetchStrategy)) {
        final JoinDefinedByMetadata join = JoinHelper.INSTANCE.createCollectionJoin(lhsQuerySpace, attributeDefinition.getName(), rhs, required, (CollectionType) attributeDefinition.getType(), fetchedPersister.getFactory());
        lhsQuerySpace.addJoin(join);
    }
    return rhs;
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionType(org.hibernate.type.CollectionType) JoinDefinedByMetadata(org.hibernate.loader.plan.spi.JoinDefinedByMetadata) WalkingException(org.hibernate.persister.walking.spi.WalkingException) ExpandingCollectionQuerySpace(org.hibernate.loader.plan.build.spi.ExpandingCollectionQuerySpace)

Aggregations

CollectionPersister (org.hibernate.persister.collection.CollectionPersister)44 HibernateException (org.hibernate.HibernateException)11 Type (org.hibernate.type.Type)9 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)8 Serializable (java.io.Serializable)7 CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)7 CollectionType (org.hibernate.type.CollectionType)7 HashMap (java.util.HashMap)6 Session (org.hibernate.Session)6 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)6 Test (org.junit.Test)6 Map (java.util.Map)5 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)5 EntityEntry (org.hibernate.engine.spi.EntityEntry)5 CompositeType (org.hibernate.type.CompositeType)5 EntityType (org.hibernate.type.EntityType)5 ResultSet (java.sql.ResultSet)4 AssertionFailure (org.hibernate.AssertionFailure)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4