use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class BatchFetchQueue method addBatchLoadableCollection.
// collection batch support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* If an CollectionEntry represents a batch loadable collection, add
* it to the queue.
*/
public void addBatchLoadableCollection(PersistentCollection collection, CollectionEntry ce) {
final CollectionPersister persister = ce.getLoadedPersister();
LinkedHashMap<CollectionEntry, PersistentCollection> map = batchLoadableCollections.get(persister.getRole());
if (map == null) {
map = new LinkedHashMap<>(16);
batchLoadableCollections.put(persister.getRole(), map);
}
map.put(ce, collection);
}
use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class DirtyCollectionSearchVisitor method processCollection.
Object processCollection(Object collection, CollectionType type) throws HibernateException {
if (collection != null) {
final SessionImplementor session = getSession();
final PersistentCollection persistentCollection;
if (type.isArrayType()) {
persistentCollection = session.getPersistenceContext().getCollectionHolder(collection);
// if no array holder we found an unwrappered array (this can't occur,
// because we now always call wrap() beforeQuery getting to here)
// return (ah==null) ? true : searchForDirtyCollections(ah, type);
} else {
// if not wrappered yet, its dirty (this can't occur, because
// we now always call wrap() beforeQuery getting to here)
// return ( ! (obj instanceof PersistentCollection) ) ?
//true : searchForDirtyCollections( (PersistentCollection) obj, type );
persistentCollection = (PersistentCollection) collection;
}
if (persistentCollection.isDirty()) {
//we need to check even if it was not initialized, because of delayed adds!
dirty = true;
//NOTE: EARLY EXIT!
return null;
}
}
return null;
}
use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class FlushVisitor method processCollection.
Object processCollection(Object collection, CollectionType type) throws HibernateException {
if (collection == CollectionType.UNFETCHED_COLLECTION) {
return null;
}
if (collection != null) {
final PersistentCollection coll;
if (type.hasHolder()) {
coll = getSession().getPersistenceContext().getCollectionHolder(collection);
} else {
coll = (PersistentCollection) collection;
}
Collections.processReachableCollection(coll, type, owner, getSession());
}
return null;
}
use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class CollectionUpdateAction method execute.
@Override
public void execute() throws HibernateException {
final Serializable id = getKey();
final SharedSessionContractImplementor session = getSession();
final CollectionPersister persister = getPersister();
final PersistentCollection collection = getCollection();
final boolean affectedByFilters = persister.isAffectedByEnabledFilters(session);
preUpdate();
if (!collection.wasInitialized()) {
if (!collection.hasQueuedOperations()) {
throw new AssertionFailure("no queued adds");
}
//do nothing - we only need to notify the cache...
} else if (!affectedByFilters && collection.empty()) {
if (!emptySnapshot) {
persister.remove(id, session);
}
} else if (collection.needsRecreate(persister)) {
if (affectedByFilters) {
throw new HibernateException("cannot recreate collection while filter is enabled: " + MessageHelper.collectionInfoString(persister, collection, id, session));
}
if (!emptySnapshot) {
persister.remove(id, session);
}
persister.recreate(collection, id, session);
} else {
persister.deleteRows(collection, id, session);
persister.updateRows(collection, id, session);
persister.insertRows(collection, id, session);
}
getSession().getPersistenceContext().getCollectionEntry(collection).afterAction(collection);
evict();
postUpdate();
if (getSession().getFactory().getStatistics().isStatisticsEnabled()) {
getSession().getFactory().getStatistics().updateCollection(getPersister().getRole());
}
}
use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class AbstractEntityPersister method initializeLazyProperty.
public Object initializeLazyProperty(String fieldName, Object entity, SharedSessionContractImplementor session) {
final EntityEntry entry = session.getPersistenceContext().getEntry(entity);
final InterceptorImplementor interceptor = ((PersistentAttributeInterceptable) entity).$$_hibernate_getInterceptor();
assert interceptor != null : "Expecting bytecode interceptor to be non-null";
if (hasCollections()) {
final Type type = getPropertyType(fieldName);
if (type.isCollectionType()) {
// we have a condition where a collection attribute is being access via enhancement:
// we can circumvent all the rest and just return the PersistentCollection
final CollectionType collectionType = (CollectionType) type;
final CollectionPersister persister = factory.getMetamodel().collectionPersister(collectionType.getRole());
// Get/create the collection, and make sure it is initialized! This initialized part is
// different from proxy-based scenarios where we have to create the PersistentCollection
// reference "ahead of time" to add as a reference to the proxy. For bytecode solutions
// we are not creating the PersistentCollection ahead of time, but instead we are creating
// it on first request through the enhanced entity.
// see if there is already a collection instance associated with the session
// NOTE : can this ever happen?
final Serializable key = getCollectionKey(persister, entity, entry, session);
PersistentCollection collection = session.getPersistenceContext().getCollection(new CollectionKey(persister, key));
if (collection == null) {
collection = collectionType.instantiate(session, persister, key);
collection.setOwner(entity);
session.getPersistenceContext().addUninitializedCollection(persister, collection, key);
}
// HHH-11161 Initialize, if the collection is not extra lazy
if (!persister.isExtraLazy()) {
session.initializeCollection(collection, false);
}
interceptor.attributeInitialized(fieldName);
if (collectionType.isArrayType()) {
session.getPersistenceContext().addCollectionHolder(collection);
}
// update the "state" of the entity's EntityEntry to over-write UNFETCHED_PROPERTY reference
// for the collection to the just loaded collection
final EntityEntry ownerEntry = session.getPersistenceContext().getEntry(entity);
if (ownerEntry == null) {
// not good
throw new AssertionFailure("Could not locate EntityEntry for the collection owner in the PersistenceContext");
}
ownerEntry.overwriteLoadedStateCollectionValue(fieldName, collection);
// EARLY EXIT!!!
return collection;
}
}
final Serializable id = session.getContextEntityIdentifier(entity);
if (entry == null) {
throw new HibernateException("entity is not associated with the session: " + id);
}
if (LOG.isTraceEnabled()) {
LOG.tracev("Initializing lazy properties of: {0}, field access: {1}", MessageHelper.infoString(this, id, getFactory()), fieldName);
}
if (session.getCacheMode().isGetEnabled() && hasCache() && isLazyPropertiesCacheable()) {
final EntityRegionAccessStrategy cache = getCacheAccessStrategy();
final Object cacheKey = cache.generateCacheKey(id, this, session.getFactory(), session.getTenantIdentifier());
final Object ce = CacheHelper.fromSharedCache(session, cacheKey, cache);
if (ce != null) {
final CacheEntry cacheEntry = (CacheEntry) getCacheEntryStructure().destructure(ce, factory);
final Object initializedValue = initializeLazyPropertiesFromCache(fieldName, entity, session, entry, cacheEntry);
interceptor.attributeInitialized(fieldName);
// NOTE EARLY EXIT!!!
return initializedValue;
}
}
return initializeLazyPropertiesFromDatastore(fieldName, entity, session, id, entry);
}
Aggregations