Search in sources :

Example 6 with BatchFetchQueue

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

the class CollectionLoaderSubSelectFetch method load.

@Override
public PersistentCollection<?> load(Object triggerKey, SharedSessionContractImplementor session) {
    final CollectionKey collectionKey = new CollectionKey(attributeMapping.getCollectionDescriptor(), triggerKey);
    final SessionFactoryImplementor sessionFactory = session.getFactory();
    final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
    final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
    final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    // try to find a registered SubselectFetch
    final PersistentCollection<?> collection = persistenceContext.getCollection(collectionKey);
    attributeMapping.getCollectionDescriptor().getCollectionType().getKeyOfOwner(collection.getOwner(), session);
    final EntityEntry ownerEntry = persistenceContext.getEntry(collection.getOwner());
    final BatchFetchQueue batchFetchQueue = persistenceContext.getBatchFetchQueue();
    final EntityKey triggerKeyOwnerKey = ownerEntry.getEntityKey();
    final SubselectFetch registeredFetch = batchFetchQueue.getSubselect(triggerKeyOwnerKey);
    List<PersistentCollection<?>> subSelectFetchedCollections = null;
    if (registeredFetch != null) {
        subSelectFetchedCollections = CollectionHelper.arrayList(registeredFetch.getResultingEntityKeys().size());
        // there was one, so we want to make sure to prepare the corresponding collection
        // reference for reading
        final Iterator<EntityKey> itr = registeredFetch.getResultingEntityKeys().iterator();
        while (itr.hasNext()) {
            final EntityKey key = itr.next();
            final PersistentCollection<?> containedCollection = persistenceContext.getCollection(new CollectionKey(attributeMapping.getCollectionDescriptor(), key.getIdentifier()));
            if (containedCollection != collection) {
                containedCollection.beginRead();
                containedCollection.beforeInitialize(getLoadable().getCollectionDescriptor(), -1);
                subSelectFetchedCollections.add(containedCollection);
            }
        }
    }
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(this.subselect.getLoadingJdbcParameterBindings(), QueryOptions.NONE);
    final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler(batchFetchQueue, sqlAst, this.subselect.getLoadingJdbcParameters(), this.subselect.getLoadingJdbcParameterBindings());
    jdbcServices.getJdbcSelectExecutor().list(jdbcSelect, this.subselect.getLoadingJdbcParameterBindings(), new ExecutionContext() {

        @Override
        public SharedSessionContractImplementor getSession() {
            return session;
        }

        @Override
        public QueryOptions getQueryOptions() {
            return QueryOptions.NONE;
        }

        @Override
        public String getQueryIdentifier(String sql) {
            return sql;
        }

        @Override
        public void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) {
            subSelectFetchableKeysHandler.addKey(entityKey, entry);
        }

        @Override
        public QueryParameterBindings getQueryParameterBindings() {
            return QueryParameterBindings.NO_PARAM_BINDINGS;
        }

        @Override
        public Callback getCallback() {
            return null;
        }
    }, RowTransformerPassThruImpl.instance(), ListResultsConsumer.UniqueSemantic.FILTER);
    if (subSelectFetchedCollections != null && !subSelectFetchedCollections.isEmpty()) {
        subSelectFetchedCollections.forEach(c -> {
            if (c.wasInitialized()) {
                return;
            }
            c.initializeEmptyCollection(getLoadable().getCollectionDescriptor());
            ResultsHelper.finalizeCollectionLoading(persistenceContext, getLoadable().getCollectionDescriptor(), c, c.getKey(), true);
        });
        subSelectFetchedCollections.clear();
    }
    return collection;
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CollectionKey(org.hibernate.engine.spi.CollectionKey) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) QueryOptions(org.hibernate.query.spi.QueryOptions) BatchFetchQueue(org.hibernate.engine.spi.BatchFetchQueue) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) EntityKey(org.hibernate.engine.spi.EntityKey) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) LoadingEntityEntry(org.hibernate.sql.results.graph.entity.LoadingEntityEntry) EntityEntry(org.hibernate.engine.spi.EntityEntry) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) LoadingEntityEntry(org.hibernate.sql.results.graph.entity.LoadingEntityEntry)

Example 7 with BatchFetchQueue

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

the class BatchFetchQueueHelper method removeBatchLoadableEntityKey.

/**
 * Remove the entity key with the specified {@code id} and {@code persister} from
 * the batch loadable entities {@link BatchFetchQueue}.
 *
 * @param id - the ID for the entity to be removed
 * @param persister - the entity persister
 * @param session - the session
 */
public static void removeBatchLoadableEntityKey(Serializable id, EntityPersister persister, SharedSessionContractImplementor session) {
    final EntityKey entityKey = session.generateEntityKey(id, persister);
    final BatchFetchQueue batchFetchQueue = session.getPersistenceContext().getBatchFetchQueue();
    batchFetchQueue.removeBatchLoadableEntityKey(entityKey);
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) BatchFetchQueue(org.hibernate.engine.spi.BatchFetchQueue)

Example 8 with BatchFetchQueue

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

the class StatefulPersistenceContext method removeEntity.

@Override
public Object removeEntity(EntityKey key) {
    final Object entity;
    if (entitiesByKey != null) {
        entity = entitiesByKey.remove(key);
        if (entitiesByUniqueKey != null) {
            final Iterator<?> itr = entitiesByUniqueKey.values().iterator();
            while (itr.hasNext()) {
                if (itr.next() == entity) {
                    itr.remove();
                }
            }
        }
    } else {
        entity = null;
    }
    // Clear all parent cache
    parentsByChild = null;
    if (entitySnapshotsByKey != null) {
        entitySnapshotsByKey.remove(key);
    }
    if (nullifiableEntityKeys != null) {
        nullifiableEntityKeys.remove(key);
    }
    final BatchFetchQueue fetchQueue = this.batchFetchQueue;
    if (fetchQueue != null) {
        fetchQueue.removeBatchLoadableEntityKey(key);
        fetchQueue.removeSubselect(key);
    }
    return entity;
}
Also used : BatchFetchQueue(org.hibernate.engine.spi.BatchFetchQueue)

Example 9 with BatchFetchQueue

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

the class BatchFetchNotFoundIgnoreDynamicStyleTest method checkInBatchFetchQueue.

private static void checkInBatchFetchQueue(long id, Session session, boolean expected) {
    final SessionImplementor sessionImplementor = (SessionImplementor) session;
    final EntityPersister persister = sessionImplementor.getFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(Task.class);
    final BatchFetchQueue batchFetchQueue = sessionImplementor.getPersistenceContextInternal().getBatchFetchQueue();
    assertThat("Checking BatchFetchQueue for entry for Task#" + id, batchFetchQueue.containsEntityKey(new EntityKey(id, persister)), is(expected));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityKey(org.hibernate.engine.spi.EntityKey) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) BatchFetchQueue(org.hibernate.engine.spi.BatchFetchQueue)

Aggregations

BatchFetchQueue (org.hibernate.engine.spi.BatchFetchQueue)9 EntityKey (org.hibernate.engine.spi.EntityKey)5 EntityEntry (org.hibernate.engine.spi.EntityEntry)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)1 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)1 CollectionKey (org.hibernate.engine.spi.CollectionKey)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 QueryOptions (org.hibernate.query.spi.QueryOptions)1 QueryParameterBindings (org.hibernate.query.spi.QueryParameterBindings)1 SqlAstTranslatorFactory (org.hibernate.sql.ast.SqlAstTranslatorFactory)1 Callback (org.hibernate.sql.exec.spi.Callback)1 ExecutionContext (org.hibernate.sql.exec.spi.ExecutionContext)1