Search in sources :

Example 1 with SubselectFetch

use of org.hibernate.engine.spi.SubselectFetch in project hibernate-orm by hibernate.

the class AbstractCollectionPersister method getSubselectInitializer.

private CollectionInitializer getSubselectInitializer(Serializable key, SharedSessionContractImplementor session) {
    if (!isSubselectLoadable()) {
        return null;
    }
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    SubselectFetch subselect = persistenceContext.getBatchFetchQueue().getSubselect(session.generateEntityKey(key, getOwnerEntityPersister()));
    if (subselect == null) {
        return null;
    } else {
        // Take care of any entities that might have
        // been evicted!
        Iterator iter = subselect.getResult().iterator();
        while (iter.hasNext()) {
            if (!persistenceContext.containsEntity((EntityKey) iter.next())) {
                iter.remove();
            }
        }
        // Run a subquery loader
        return createSubselectInitializer(subselect, session);
    }
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) Iterator(java.util.Iterator) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 2 with SubselectFetch

use of org.hibernate.engine.spi.SubselectFetch in project hibernate-orm by hibernate.

the class ResultSetProcessingContextImpl method createSubselects.

private void createSubselects() {
    if (subselectLoadableEntityKeyMap == null || nRowsRead <= 1) {
        LOG.tracef("Skipping create subselects because there are fewer than 2 results, so query by key is more efficient.", getClass().getName());
        // early return
        return;
    }
    final Map<String, int[]> namedParameterLocMap = ResultSetProcessorHelper.buildNamedParameterLocMap(queryParameters, namedParameterContext);
    final String subselectQueryString = SubselectFetch.createSubselectFetchQueryFragment(queryParameters);
    for (Map.Entry<EntityReference, Set<EntityKey>> entry : subselectLoadableEntityKeyMap.entrySet()) {
        if (!entry.getKey().getEntityPersister().hasSubselectLoadableCollections()) {
            continue;
        }
        SubselectFetch subselectFetch = new SubselectFetch(subselectQueryString, aliasResolutionContext.resolveSqlTableAliasFromQuerySpaceUid(entry.getKey().getQuerySpaceUid()), (Loadable) entry.getKey().getEntityPersister(), queryParameters, entry.getValue(), namedParameterLocMap);
        for (EntityKey key : entry.getValue()) {
            session.getPersistenceContext().getBatchFetchQueue().addSubselect(key, subselectFetch);
        }
    }
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) EntityReference(org.hibernate.loader.plan.spi.EntityReference) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 3 with SubselectFetch

use of org.hibernate.engine.spi.SubselectFetch in project hibernate-orm by hibernate.

the class Loader method createSubselects.

private void createSubselects(List keys, QueryParameters queryParameters, SharedSessionContractImplementor session) {
    if (keys.size() > 1) {
        //if we only returned one entity, query by key is more efficient
        Set[] keySets = transpose(keys);
        Map namedParameterLocMap = buildNamedParameterLocMap(queryParameters);
        final Loadable[] loadables = getEntityPersisters();
        final String[] aliases = getAliases();
        final String subselectQueryString = SubselectFetch.createSubselectFetchQueryFragment(queryParameters);
        for (Object key : keys) {
            final EntityKey[] rowKeys = (EntityKey[]) key;
            for (int i = 0; i < rowKeys.length; i++) {
                if (rowKeys[i] != null && loadables[i].hasSubselectLoadableCollections()) {
                    SubselectFetch subselectFetch = new SubselectFetch(subselectQueryString, aliases[i], loadables[i], queryParameters, keySets[i], namedParameterLocMap);
                    session.getPersistenceContext().getBatchFetchQueue().addSubselect(rowKeys[i], subselectFetch);
                }
            }
        }
    }
}
Also used : UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) Loadable(org.hibernate.persister.entity.Loadable) EntityKey(org.hibernate.engine.spi.EntityKey) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) ResultSet(java.sql.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

EntityKey (org.hibernate.engine.spi.EntityKey)3 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)3 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 IdentityHashMap (java.util.IdentityHashMap)1 Iterator (java.util.Iterator)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 EntityReference (org.hibernate.loader.plan.spi.EntityReference)1 Loadable (org.hibernate.persister.entity.Loadable)1 UniqueKeyLoadable (org.hibernate.persister.entity.UniqueKeyLoadable)1