Search in sources :

Example 11 with BytecodeLazyAttributeInterceptor

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

the class DirtyTrackingCollectionInDefaultFetchGroupTest method test.

@Test
public void test() {
    doInJPA(this::sessionFactory, entityManager -> {
        StringsEntity entity = entityManager.find(StringsEntity.class, 1L);
        entityManager.flush();
        BytecodeLazyAttributeInterceptor interceptor = (BytecodeLazyAttributeInterceptor) ((PersistentAttributeInterceptable) entity).$$_hibernate_getInterceptor();
        assertTrue(interceptor.hasAnyUninitializedAttributes());
        assertFalse(interceptor.isAttributeLoaded("someStrings"));
        assertFalse(interceptor.isAttributeLoaded("someStringEntities"));
    });
}
Also used : BytecodeLazyAttributeInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor) Test(org.junit.Test)

Example 12 with BytecodeLazyAttributeInterceptor

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

the class OneToOneExplicitOptionTests method testOwnerIsProxy.

@Test
public void testOwnerIsProxy() {
    final EntityPersister supplementalInfoDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor(SupplementalInfo.class);
    final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata();
    assertThat(supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is(true));
    final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor(Customer.class);
    final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata();
    assertThat(customerEnhancementMetadata.isEnhancedForLazyLoading(), is(true));
    inTransaction((session) -> {
        final SupplementalInfo supplementalInfo = session.byId(SupplementalInfo.class).getReference(1);
        // we should have just the uninitialized SupplementalInfo proxy
        // - therefore no SQL statements should have been executed
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(0));
        final BytecodeLazyAttributeInterceptor initialInterceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor(supplementalInfo);
        assertThat(initialInterceptor, instanceOf(EnhancementAsProxyLazinessInterceptor.class));
        // access the id - should do nothing with db
        supplementalInfo.getId();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(0));
        assertThat(supplementalInfoEnhancementMetadata.extractLazyInterceptor(supplementalInfo), sameInstance(initialInterceptor));
        // this should trigger loading the entity's base state
        supplementalInfo.getSomething();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
        final BytecodeLazyAttributeInterceptor interceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor(supplementalInfo);
        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 = supplementalInfo.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 13 with BytecodeLazyAttributeInterceptor

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

the class InverseToOneAllowProxyTests method testOwnerIsProxy.

@Test
public void testOwnerIsProxy() {
    final EntityPersister supplementalInfoDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor(SupplementalInfo.class);
    final BytecodeEnhancementMetadata supplementalInfoEnhancementMetadata = supplementalInfoDescriptor.getBytecodeEnhancementMetadata();
    assertThat(supplementalInfoEnhancementMetadata.isEnhancedForLazyLoading(), is(true));
    final EntityPersister customerDescriptor = sessionFactory().getMappingMetamodel().getEntityDescriptor(Customer.class);
    final BytecodeEnhancementMetadata customerEnhancementMetadata = customerDescriptor.getBytecodeEnhancementMetadata();
    assertThat(customerEnhancementMetadata.isEnhancedForLazyLoading(), is(true));
    inTransaction((session) -> {
        // Get a reference to the SupplementalInfo we created
        final SupplementalInfo supplementalInfo = session.byId(SupplementalInfo.class).getReference(1);
        // 1) we should have just the uninitialized SupplementalInfo enhanced proxy
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(0));
        final BytecodeLazyAttributeInterceptor initialInterceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor(supplementalInfo);
        assertThat(initialInterceptor, instanceOf(EnhancementAsProxyLazinessInterceptor.class));
        // (2) Access the SupplementalInfo's id value - should trigger no SQL
        supplementalInfo.getId();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(0));
        assertThat(initialInterceptor, sameInstance(supplementalInfoEnhancementMetadata.extractLazyInterceptor(supplementalInfo)));
        // 3) Access SupplementalInfo's `something` state
        // - should trigger loading the "base group" state, which only include `something`.
        // NOTE: `customer` is not part of this lazy group because we do not know the
        // Customer PK from this side
        supplementalInfo.getSomething();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
        final BytecodeLazyAttributeInterceptor interceptor = supplementalInfoEnhancementMetadata.extractLazyInterceptor(supplementalInfo);
        assertThat(initialInterceptor, not(sameInstance(interceptor)));
        assertThat(interceptor, instanceOf(LazyAttributeLoadingInterceptor.class));
        // 4) Access SupplementalInfo's `customer` state
        // - should trigger load from Customer table, by FK
        final Customer customer = supplementalInfo.getCustomer();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(2));
        // just as above, accessing id should trigger no loads
        customer.getId();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(2));
        customer.getName();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(2));
    });
}
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)

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