Search in sources :

Example 6 with BytecodeLazyAttributeInterceptor

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

the class JoinFetchedManyToOneAllowProxyTests method testOwnerIsProxy.

@Test
public void testOwnerIsProxy() {
    final EntityPersister orderDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor(Order.class);
    final BytecodeEnhancementMetadata orderEnhancementMetadata = orderDescriptor.getBytecodeEnhancementMetadata();
    assertThat(orderEnhancementMetadata.isEnhancedForLazyLoading(), is(true));
    final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor(Customer.class);
    final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata();
    assertThat(customerEnhancementMetadata.isEnhancedForLazyLoading(), is(true));
    inTransaction((session) -> {
        final Order order = session.byId(Order.class).getReference(1);
        // we should have an uninitialized enhanced proxy -  therefore no SQL statements should have been executed
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(0));
        final BytecodeLazyAttributeInterceptor initialInterceptor = orderEnhancementMetadata.extractLazyInterceptor(order);
        assertThat(initialInterceptor, instanceOf(EnhancementAsProxyLazinessInterceptor.class));
        // access the id - should do nothing with db
        order.getId();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(0));
        assertThat(initialInterceptor, sameInstance(orderEnhancementMetadata.extractLazyInterceptor(order)));
        // this should trigger loading the entity's base state which includes customer.
        // and since customer is defined for join fetch we
        order.getAmount();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
        assertThat(initialInterceptor, not(sameInstance(orderEnhancementMetadata.extractLazyInterceptor(order))));
        // should not trigger a load - Order's base fetch state includes customer
        final Customer customer = order.getCustomer();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
        // and since Order#customer is mapped for JOIN fetching, Customer should be fully initialized as well
        assertThat(Hibernate.isInitialized(customer), is(true));
        // just as above, accessing id should trigger no loads
        customer.getId();
        customer.getName();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) BytecodeLazyAttributeInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor) BytecodeEnhancementMetadata(org.hibernate.bytecode.spi.BytecodeEnhancementMetadata) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor) Test(org.junit.Test)

Example 7 with BytecodeLazyAttributeInterceptor

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

the class ManyToOneAllowProxyTests method testOwnerIsProxy.

@Test
public void testOwnerIsProxy() {
    final EntityPersister orderDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor(Order.class);
    final BytecodeEnhancementMetadata orderEnhancementMetadata = orderDescriptor.getBytecodeEnhancementMetadata();
    assertThat(orderEnhancementMetadata.isEnhancedForLazyLoading(), is(true));
    final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor(Customer.class);
    final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata();
    assertThat(customerEnhancementMetadata.isEnhancedForLazyLoading(), is(true));
    inTransaction((session) -> {
        final Order order = session.byId(Order.class).getReference(1);
        // we should have just the uninitialized proxy of the owner - and
        // therefore no SQL statements should have been executed
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(0));
        final BytecodeLazyAttributeInterceptor initialInterceptor = orderEnhancementMetadata.extractLazyInterceptor(order);
        assertThat(initialInterceptor, instanceOf(EnhancementAsProxyLazinessInterceptor.class));
        // access the id - should do nothing with db
        order.getId();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(0));
        assertThat(initialInterceptor, sameInstance(orderEnhancementMetadata.extractLazyInterceptor(order)));
        // this should trigger loading the entity's base state
        order.getAmount();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
        final BytecodeLazyAttributeInterceptor interceptor = orderEnhancementMetadata.extractLazyInterceptor(order);
        assertThat(initialInterceptor, not(sameInstance(interceptor)));
        assertThat(interceptor, instanceOf(LazyAttributeLoadingInterceptor.class));
        final LazyAttributeLoadingInterceptor attrInterceptor = (LazyAttributeLoadingInterceptor) interceptor;
        assertThat(attrInterceptor.hasAnyUninitializedAttributes(), is(false));
        // should not trigger a load and the `customer` reference should be an uninitialized enhanced proxy
        final Customer customer = order.getCustomer();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
        final BytecodeLazyAttributeInterceptor initialCustomerInterceptor = customerEnhancementMetadata.extractLazyInterceptor(customer);
        assertThat(initialCustomerInterceptor, instanceOf(EnhancementAsProxyLazinessInterceptor.class));
        // just as above, accessing id should trigger no loads
        customer.getId();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
        assertThat(initialCustomerInterceptor, sameInstance(customerEnhancementMetadata.extractLazyInterceptor(customer)));
        customer.getName();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(2));
        assertThat(customerEnhancementMetadata.extractLazyInterceptor(customer), instanceOf(LazyAttributeLoadingInterceptor.class));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LazyAttributeLoadingInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor) BytecodeLazyAttributeInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor) BytecodeEnhancementMetadata(org.hibernate.bytecode.spi.BytecodeEnhancementMetadata) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor) Test(org.junit.Test)

Example 8 with BytecodeLazyAttributeInterceptor

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

the class LazyGroupWithInheritanceTest method loadEntityWithAssociationToAbstract.

@Test
public void loadEntityWithAssociationToAbstract() {
    final Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    inTransaction((session) -> {
        final Order loaded = session.byId(Order.class).load(1);
        assert Hibernate.isPropertyInitialized(loaded, "customer");
        assertThat(stats.getPrepareStatementCount(), is(1L));
        assertThat(loaded, instanceOf(PersistentAttributeInterceptable.class));
        final PersistentAttributeInterceptor interceptor = ((PersistentAttributeInterceptable) loaded).$$_hibernate_getInterceptor();
        assertThat(interceptor, instanceOf(BytecodeLazyAttributeInterceptor.class));
        final BytecodeLazyAttributeInterceptor interceptor1 = (BytecodeLazyAttributeInterceptor) interceptor;
    });
}
Also used : PersistentAttributeInterceptor(org.hibernate.engine.spi.PersistentAttributeInterceptor) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) BytecodeLazyAttributeInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor) Statistics(org.hibernate.stat.Statistics) Test(org.junit.Test)

Example 9 with BytecodeLazyAttributeInterceptor

use of org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor 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 BytecodeLazyAttributeInterceptor

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

the class PersistenceUtilHelper method isLoadedWithoutReference.

/**
 * Is the given attribute (by name) loaded?  This form must take care to not access the attribute (trigger
 * initialization).
 *
 * @param entity The entity
 * @param attributeName The name of the attribute to check
 * @param cache The cache we maintain of attribute resolutions
 *
 * @return The LoadState
 */
public static LoadState isLoadedWithoutReference(Object entity, String attributeName, MetadataCache cache) {
    boolean sureFromUs = false;
    if (entity instanceof HibernateProxy) {
        LazyInitializer li = ((HibernateProxy) entity).getHibernateLazyInitializer();
        if (li.isUninitialized()) {
            // we have an uninitialized proxy, the attribute cannot be loaded
            return LoadState.NOT_LOADED;
        } else {
            // swap the proxy with target (for proper class name resolution)
            entity = li.getImplementation();
        }
        sureFromUs = true;
    }
    // we are instrumenting but we can't assume we are the only ones
    if (entity instanceof PersistentAttributeInterceptable) {
        final BytecodeLazyAttributeInterceptor interceptor = extractInterceptor((PersistentAttributeInterceptable) entity);
        final boolean isInitialized = interceptor == null || interceptor.isAttributeLoaded(attributeName);
        LoadState state;
        if (isInitialized && interceptor != null) {
            // it's ours, we can read
            try {
                final Class entityClass = entity.getClass();
                final Object attributeValue = cache.getClassMetadata(entityClass).getAttributeAccess(attributeName).extractValue(entity);
                state = isLoaded(attributeValue);
                // it's ours so we know it's loaded
                if (state == LoadState.UNKNOWN) {
                    state = LoadState.LOADED;
                }
            } catch (AttributeExtractionException ignore) {
                state = LoadState.UNKNOWN;
            }
        } else if (interceptor != null) {
            state = LoadState.NOT_LOADED;
        } else if (sureFromUs) {
            // it's ours, we can read
            try {
                final Class entityClass = entity.getClass();
                final Object attributeValue = cache.getClassMetadata(entityClass).getAttributeAccess(attributeName).extractValue(entity);
                state = isLoaded(attributeValue);
                // it's ours so we know it's loaded
                if (state == LoadState.UNKNOWN) {
                    state = LoadState.LOADED;
                }
            } catch (AttributeExtractionException ignore) {
                state = LoadState.UNKNOWN;
            }
        } else {
            state = LoadState.UNKNOWN;
        }
        return state;
    } else {
        return LoadState.UNKNOWN;
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) LoadState(jakarta.persistence.spi.LoadState) BytecodeLazyAttributeInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Aggregations

BytecodeLazyAttributeInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor)13 EnhancementAsProxyLazinessInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor)9 BytecodeEnhancementMetadata (org.hibernate.bytecode.spi.BytecodeEnhancementMetadata)9 Test (org.junit.Test)9 LazyAttributeLoadingInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 PersistentAttributeInterceptable (org.hibernate.engine.spi.PersistentAttributeInterceptable)3 HibernateProxy (org.hibernate.proxy.HibernateProxy)3 LazyInitializer (org.hibernate.proxy.LazyInitializer)3 LoadState (jakarta.persistence.spi.LoadState)1 LazyPropertyInitializer (org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer)1 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 PersistentAttributeInterceptor (org.hibernate.engine.spi.PersistentAttributeInterceptor)1 EventSource (org.hibernate.event.spi.EventSource)1 LoadEvent (org.hibernate.event.spi.LoadEvent)1 Statistics (org.hibernate.stat.Statistics)1