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;
}
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);
}
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;
}
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));
}
Aggregations