Search in sources :

Example 6 with EnhancementAsProxyLazinessInterceptor

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

the class AbstractEntityEntry method isUnequivocallyNonDirty.

@SuppressWarnings({ "SimplifiableIfStatement" })
private boolean isUnequivocallyNonDirty(Object entity) {
    if (entity instanceof SelfDirtinessTracker) {
        boolean uninitializedProxy = false;
        if (entity instanceof PersistentAttributeInterceptable) {
            final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) entity;
            final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
            if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
                EnhancementAsProxyLazinessInterceptor enhancementAsProxyLazinessInterceptor = (EnhancementAsProxyLazinessInterceptor) interceptor;
                return !enhancementAsProxyLazinessInterceptor.hasWrittenFieldNames();
            }
        } else if (entity instanceof HibernateProxy) {
            uninitializedProxy = ((HibernateProxy) entity).getHibernateLazyInitializer().isUninitialized();
        }
        // we never have to check an uninitialized proxy
        return uninitializedProxy || !persister.hasCollections() && !persister.hasMutableProperties() && !((SelfDirtinessTracker) entity).$$_hibernate_hasDirtyAttributes();
    }
    if (entity instanceof PersistentAttributeInterceptable) {
        final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) entity;
        final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
        if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
            // we never have to check an uninitialized proxy
            return true;
        }
    }
    final CustomEntityDirtinessStrategy customEntityDirtinessStrategy = getPersistenceContext().getSession().getFactory().getCustomEntityDirtinessStrategy();
    if (customEntityDirtinessStrategy.canDirtyCheck(entity, getPersister(), (Session) getPersistenceContext().getSession())) {
        return !customEntityDirtinessStrategy.isDirty(entity, getPersister(), (Session) getPersistenceContext().getSession());
    }
    if (getPersister().hasMutableProperties()) {
        return false;
    }
    return false;
}
Also used : CustomEntityDirtinessStrategy(org.hibernate.CustomEntityDirtinessStrategy) PersistentAttributeInterceptor(org.hibernate.engine.spi.PersistentAttributeInterceptor) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) HibernateProxy(org.hibernate.proxy.HibernateProxy) SelfDirtinessTracker(org.hibernate.engine.spi.SelfDirtinessTracker) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor) Session(org.hibernate.Session)

Example 7 with EnhancementAsProxyLazinessInterceptor

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

the class Hibernate method initialize.

/**
 * Force initialization of a proxy or persistent collection. In the case of a
 * many-valued association, only the collection itself is initialized. It is not
 * guaranteed that the associated entities held within the collection will be
 * initialized.
 *
 * @param proxy a persistable object, proxy, persistent collection or {@code null}
 * @throws HibernateException if the proxy cannot be initialized at this time,
 * for example, if the {@code Session} was closed
 */
public static void initialize(Object proxy) throws HibernateException {
    if (proxy == null) {
        return;
    }
    if (proxy instanceof HibernateProxy) {
        ((HibernateProxy) proxy).getHibernateLazyInitializer().initialize();
    } else if (proxy instanceof PersistentCollection) {
        ((PersistentCollection<?>) proxy).forceInitialization();
    } else if (proxy instanceof PersistentAttributeInterceptable) {
        final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) proxy;
        final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
        if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
            ((EnhancementAsProxyLazinessInterceptor) interceptor).forceInitialize(proxy, null);
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) 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 8 with EnhancementAsProxyLazinessInterceptor

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

the class DefaultMergeEventListener 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;
        }
        // otherwise, force initialization
        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 9 with EnhancementAsProxyLazinessInterceptor

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

the class IdentifierLoadAccessImpl method initializeIfNecessary.

private void initializeIfNecessary(Object result) {
    if (result == null) {
        return;
    }
    if (result instanceof HibernateProxy) {
        final HibernateProxy hibernateProxy = (HibernateProxy) result;
        final LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();
        if (initializer.isUninitialized()) {
            initializer.initialize();
        }
    } else {
        final BytecodeEnhancementMetadata enhancementMetadata = entityPersister.getEntityMetamodel().getBytecodeEnhancementMetadata();
        if (enhancementMetadata.isEnhancedForLazyLoading()) {
            final BytecodeLazyAttributeInterceptor interceptor = enhancementMetadata.extractLazyInterceptor(result);
            if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
                ((EnhancementAsProxyLazinessInterceptor) interceptor).forceInitialize(result, null);
            }
        }
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) BytecodeLazyAttributeInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor) BytecodeEnhancementMetadata(org.hibernate.bytecode.spi.BytecodeEnhancementMetadata) HibernateProxy(org.hibernate.proxy.HibernateProxy) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor)

Example 10 with EnhancementAsProxyLazinessInterceptor

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

the class EnhancedProxyCacheTest method testPreferenceFor2LCOverUninitializedProxy.

@Test
public void testPreferenceFor2LCOverUninitializedProxy() throws Exception {
    final Statistics stats = sessionFactory().getStatistics();
    storeTestData();
    clearAllCaches();
    stats.clear();
    assertTrue(stats.isStatisticsEnabled());
    assertEquals(0, stats.getEntityFetchCount());
    assertEquals(0, stats.getSecondLevelCacheHitCount());
    // First we load the Country once, then trigger initialization of the related Continent proxy.
    // 2LC is empty, so stats should show that these objects are being loaded from the DB.
    inSession(s -> {
        Country nl = s.get(Country.class, countryId.get());
        assertNotNull(nl);
        assertEquals(0, stats.getSecondLevelCacheHitCount());
        assertEquals(1, stats.getSecondLevelCacheMissCount());
        assertEquals(1, stats.getEntityLoadCount());
        Continent continent = nl.getContinent();
        // Check that this is indeed an enhanced proxy so to ensure we're testing in the right conditions.
        // The following casts should not fail:
        final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) continent;
        final EnhancementAsProxyLazinessInterceptor interceptor = (EnhancementAsProxyLazinessInterceptor) interceptable.$$_hibernate_getInterceptor();
        assertFalse(interceptor.isInitialized());
        assertFalse(interceptor.isAttributeLoaded("code"));
        // Trigger initialization of the enhanced proxy:
        assertEquals("EU", continent.getCode());
        assertTrue(interceptor.isInitialized());
        assertEquals(0, stats.getSecondLevelCacheHitCount());
        assertEquals(2, stats.getEntityLoadCount());
    });
    stats.clear();
    // Now load the same objects again; we expect to hit 2LC this time,
    // and we should see no needs to hit the DB.
    // Also, since all data is readily available we won't need to make
    // all attributes lazy.
    inSession(s -> {
        assertEquals(0, stats.getSecondLevelCacheHitCount());
        assertEquals(0, stats.getSecondLevelCacheMissCount());
        assertEquals(0, stats.getEntityLoadCount());
        Country nl = s.get(Country.class, countryId.get());
        assertNotNull(nl);
        assertEquals(1, stats.getSecondLevelCacheHitCount());
        assertEquals(0, stats.getSecondLevelCacheMissCount());
        assertEquals(0, stats.getEntityLoadCount());
        Continent continent = nl.getContinent();
        final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) continent;
        final EnhancementAsProxyLazinessInterceptor interceptor = (EnhancementAsProxyLazinessInterceptor) interceptable.$$_hibernate_getInterceptor();
        assertFalse(interceptor.isInitialized());
        assertFalse(interceptor.isAttributeLoaded("code"));
        assertEquals(1, stats.getSecondLevelCacheHitCount());
        assertEquals(0, stats.getSecondLevelCacheMissCount());
        assertEquals(0, stats.getEntityLoadCount());
        // Trigger initialization of the enhanced proxy:
        assertEquals("EU", continent.getCode());
        assertTrue(interceptor.isInitialized());
        assertEquals(2, stats.getSecondLevelCacheHitCount());
        assertEquals(0, stats.getSecondLevelCacheMissCount());
        assertEquals(0, stats.getEntityLoadCount());
    });
}
Also used : PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) Statistics(org.hibernate.stat.Statistics) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor) Test(org.junit.Test)

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