Search in sources :

Example 11 with EnhancementAsProxyLazinessInterceptor

use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor in project hibernate-orm by hibernate.

the class StatelessSessionImpl method fetch.

@Override
public void fetch(Object association) {
    checkOpen();
    PersistenceContext persistenceContext = getPersistenceContext();
    if (association instanceof HibernateProxy) {
        LazyInitializer initializer = ((HibernateProxy) association).getHibernateLazyInitializer();
        if (initializer.isUninitialized()) {
            String entityName = initializer.getEntityName();
            Object id = initializer.getIdentifier();
            initializer.setSession(this);
            persistenceContext.beforeLoad();
            try {
                // forces the load to occur
                Object entity = initializer.getImplementation();
                if (entity == null) {
                    getFactory().getEntityNotFoundDelegate().handleEntityNotFound(entityName, id);
                }
                initializer.setImplementation(entity);
            } finally {
                initializer.unsetSession();
                persistenceContext.afterLoad();
                if (persistenceContext.isLoadFinished()) {
                    persistenceContext.clear();
                }
            }
        }
    } else if (association instanceof PersistentAttributeInterceptable) {
        final PersistentAttributeInterceptor interceptor = ((PersistentAttributeInterceptable) association).$$_hibernate_getInterceptor();
        if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
            EnhancementAsProxyLazinessInterceptor proxyInterceptor = (EnhancementAsProxyLazinessInterceptor) interceptor;
            proxyInterceptor.setSession(this);
            try {
                proxyInterceptor.forceInitialize(association, null);
            } finally {
                proxyInterceptor.unsetSession();
                if (persistenceContext.isLoadFinished()) {
                    persistenceContext.clear();
                }
            }
        }
    } else if (association instanceof PersistentCollection) {
        PersistentCollection<?> persistentCollection = (PersistentCollection<?>) association;
        if (!persistentCollection.wasInitialized()) {
            final CollectionPersister collectionDescriptor = getFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor(persistentCollection.getRole());
            Object key = persistentCollection.getKey();
            persistenceContext.addUninitializedCollection(collectionDescriptor, persistentCollection, key);
            persistentCollection.setCurrentSession(this);
            try {
                collectionDescriptor.initialize(key, this);
            } finally {
                persistentCollection.unsetSession(this);
                if (persistenceContext.isLoadFinished()) {
                    persistenceContext.clear();
                }
            }
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) LazyInitializer(org.hibernate.proxy.LazyInitializer) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) PersistentAttributeInterceptor(org.hibernate.engine.spi.PersistentAttributeInterceptor) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) StatefulPersistenceContext(org.hibernate.engine.internal.StatefulPersistenceContext) HibernateProxy(org.hibernate.proxy.HibernateProxy) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor)

Example 12 with EnhancementAsProxyLazinessInterceptor

use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor in project hibernate-reactive by hibernate.

the class DefaultReactiveMergeEventListener method unproxyManagedForDetachedMerging.

private Object unproxyManagedForDetachedMerging(Object incoming, Object managed, EntityPersister persister, EventSource source) {
    if (managed instanceof HibernateProxy) {
        return source.getPersistenceContextInternal().unproxy(managed);
    }
    if (incoming instanceof PersistentAttributeInterceptable && persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()) {
        final PersistentAttributeInterceptor incomingInterceptor = ((PersistentAttributeInterceptable) incoming).$$_hibernate_getInterceptor();
        final PersistentAttributeInterceptor managedInterceptor = ((PersistentAttributeInterceptable) managed).$$_hibernate_getInterceptor();
        // if the managed entity is not a proxy, we can just return it
        if (!(managedInterceptor instanceof EnhancementAsProxyLazinessInterceptor)) {
            return managed;
        }
        // if the incoming entity is still a proxy there is no need to force initialization of the managed one
        if (incomingInterceptor instanceof EnhancementAsProxyLazinessInterceptor) {
            return managed;
        }
        // TODO: probably needs to be made async!
        return persister.initializeEnhancedEntityUsedAsProxy(managed, null, source);
    }
    return managed;
}
Also used : PersistentAttributeInterceptor(org.hibernate.engine.spi.PersistentAttributeInterceptor) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) HibernateProxy(org.hibernate.proxy.HibernateProxy) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor)

Example 13 with EnhancementAsProxyLazinessInterceptor

use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor in project hibernate-reactive by hibernate.

the class DefaultReactiveMergeEventListener method reactiveOnMerge.

/**
 * Handle the given merge event.
 *
 * @param event The merge event to be handled.
 */
@Override
public CompletionStage<Void> reactiveOnMerge(MergeEvent event, MergeContext copyCache) throws HibernateException {
    final EventSource source = event.getSession();
    final Object original = event.getOriginal();
    if (original != null) {
        final Object entity;
        if (original instanceof HibernateProxy) {
            LazyInitializer li = ((HibernateProxy) original).getHibernateLazyInitializer();
            if (li.isUninitialized()) {
                LOG.trace("Ignoring uninitialized proxy");
                event.setResult(source.load(li.getEntityName(), li.getIdentifier()));
                // EARLY EXIT!
                return voidFuture();
            } else {
                entity = li.getImplementation();
            }
        } else if (original instanceof PersistentAttributeInterceptable) {
            final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) original;
            final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
            if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
                final EnhancementAsProxyLazinessInterceptor proxyInterceptor = (EnhancementAsProxyLazinessInterceptor) interceptor;
                LOG.trace("Ignoring uninitialized enhanced-proxy");
                // no need to go async, AFAICT ?
                event.setResult(source.load(proxyInterceptor.getEntityName(), (Serializable) proxyInterceptor.getIdentifier()));
                // EARLY EXIT!
                return voidFuture();
            } else {
                entity = original;
            }
        } else {
            entity = original;
        }
        if (copyCache.containsKey(entity) && (copyCache.isOperatedOn(entity))) {
            LOG.trace("Already in merge process");
            event.setResult(entity);
        } else {
            if (copyCache.containsKey(entity)) {
                LOG.trace("Already in copyCache; setting in merge process");
                copyCache.setOperatedOn(entity, true);
            }
            event.setEntity(entity);
            EntityState entityState = null;
            // Check the persistence context for an entry relating to this
            // entity to be merged...
            final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
            EntityEntry entry = persistenceContext.getEntry(entity);
            if (entry == null) {
                EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
                Serializable id = persister.getIdentifier(entity, source);
                if (id != null) {
                    final EntityKey key = source.generateEntityKey(id, persister);
                    final Object managedEntity = persistenceContext.getEntity(key);
                    entry = persistenceContext.getEntry(managedEntity);
                    if (entry != null) {
                        // we have specialized case of a detached entity from the
                        // perspective of the merge operation.  Specifically, we
                        // have an incoming entity instance which has a corresponding
                        // entry in the current persistence context, but registered
                        // under a different entity instance
                        entityState = DETACHED;
                    }
                }
            }
            if (entityState == null) {
                entityState = EntityState.getEntityState(entity, event.getEntityName(), entry, source, false);
            }
            switch(entityState) {
                case DETACHED:
                    return entityIsDetached(event, copyCache);
                case TRANSIENT:
                    return entityIsTransient(event, copyCache);
                case PERSISTENT:
                    return entityIsPersistent(event, copyCache);
                default:
                    // DELETED
                    throw new ObjectDeletedException("deleted instance passed to merge", null, EventUtil.getLoggableName(event.getEntityName(), entity));
            }
        }
    }
    return voidFuture();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LazyInitializer(org.hibernate.proxy.LazyInitializer) Serializable(java.io.Serializable) PersistentAttributeInterceptor(org.hibernate.engine.spi.PersistentAttributeInterceptor) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) EntityState(org.hibernate.event.internal.EntityState) HibernateProxy(org.hibernate.proxy.HibernateProxy) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry) ObjectDeletedException(org.hibernate.ObjectDeletedException)

Aggregations

EnhancementAsProxyLazinessInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor)13 PersistentAttributeInterceptable (org.hibernate.engine.spi.PersistentAttributeInterceptable)11 PersistentAttributeInterceptor (org.hibernate.engine.spi.PersistentAttributeInterceptor)10 HibernateProxy (org.hibernate.proxy.HibernateProxy)9 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)5 LazyInitializer (org.hibernate.proxy.LazyInitializer)5 EntityEntry (org.hibernate.engine.spi.EntityEntry)3 EntityKey (org.hibernate.engine.spi.EntityKey)3 EventSource (org.hibernate.event.spi.EventSource)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 ObjectDeletedException (org.hibernate.ObjectDeletedException)2 BytecodeLazyAttributeInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor)2 BytecodeEnhancementMetadata (org.hibernate.bytecode.spi.BytecodeEnhancementMetadata)2 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)2 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 Statistics (org.hibernate.stat.Statistics)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 CustomEntityDirtinessStrategy (org.hibernate.CustomEntityDirtinessStrategy)1