Search in sources :

Example 1 with BytecodeLazyAttributeInterceptor

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

the class StatefulPersistenceContext method reassociateIfUninitializedProxy.

@Override
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException {
    if (!Hibernate.isInitialized(value)) {
        // could be a proxy....
        if (value instanceof HibernateProxy) {
            final HibernateProxy proxy = (HibernateProxy) value;
            final LazyInitializer li = proxy.getHibernateLazyInitializer();
            reassociateProxy(li, proxy);
            return true;
        }
        // or an uninitialized enhanced entity ("bytecode proxy")...
        if (value instanceof PersistentAttributeInterceptable) {
            final PersistentAttributeInterceptable bytecodeProxy = (PersistentAttributeInterceptable) value;
            final BytecodeLazyAttributeInterceptor interceptor = (BytecodeLazyAttributeInterceptor) bytecodeProxy.$$_hibernate_getInterceptor();
            if (interceptor != null) {
                interceptor.setSession(getSession());
            }
            return true;
        }
    }
    return false;
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) BytecodeLazyAttributeInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 2 with BytecodeLazyAttributeInterceptor

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

the class AbstractEntityPersister method initializeEnhancedEntityUsedAsProxy.

@Override
public Object initializeEnhancedEntityUsedAsProxy(Object entity, String nameOfAttributeBeingAccessed, SharedSessionContractImplementor session) {
    final BytecodeEnhancementMetadata enhancementMetadata = getEntityMetamodel().getBytecodeEnhancementMetadata();
    final BytecodeLazyAttributeInterceptor currentInterceptor = enhancementMetadata.extractLazyInterceptor(entity);
    if (currentInterceptor instanceof EnhancementAsProxyLazinessInterceptor) {
        final EnhancementAsProxyLazinessInterceptor proxyInterceptor = (EnhancementAsProxyLazinessInterceptor) currentInterceptor;
        final EntityKey entityKey = proxyInterceptor.getEntityKey();
        final Object identifier = entityKey.getIdentifier();
        Object loaded = null;
        if (canReadFromCache && session instanceof EventSource) {
            LoadEvent loadEvent = new LoadEvent(identifier, entity, (EventSource) session, false);
            loaded = CacheEntityLoaderHelper.INSTANCE.loadFromSecondLevelCache(loadEvent, this, entityKey);
        }
        if (loaded == null) {
            loaded = singleIdEntityLoader.load(identifier, entity, LockOptions.NONE, session);
        }
        if (loaded == null) {
            final PersistenceContext persistenceContext = session.getPersistenceContext();
            persistenceContext.removeEntry(entity);
            persistenceContext.removeEntity(entityKey);
            session.getFactory().getEntityNotFoundDelegate().handleEntityNotFound(entityKey.getEntityName(), identifier);
        }
        final LazyAttributeLoadingInterceptor interceptor = enhancementMetadata.injectInterceptor(entity, identifier, session);
        final Object value;
        if (nameOfAttributeBeingAccessed == null) {
            return null;
        } else if (interceptor.isAttributeLoaded(nameOfAttributeBeingAccessed)) {
            value = getPropertyValue(entity, nameOfAttributeBeingAccessed);
        } else {
            value = ((LazyPropertyInitializer) this).initializeLazyProperty(nameOfAttributeBeingAccessed, entity, session);
        }
        return interceptor.readObject(entity, nameOfAttributeBeingAccessed, value);
    }
    throw new IllegalStateException();
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) LazyPropertyInitializer(org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer) EventSource(org.hibernate.event.spi.EventSource) LoadEvent(org.hibernate.event.spi.LoadEvent) LazyAttributeLoadingInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor) BytecodeLazyAttributeInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) StatefulPersistenceContext(org.hibernate.engine.internal.StatefulPersistenceContext) BytecodeEnhancementMetadata(org.hibernate.bytecode.spi.BytecodeEnhancementMetadata) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor)

Example 3 with BytecodeLazyAttributeInterceptor

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

the class JoinFetchedInverseToOneAllowProxyTests 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
        // - customer should be join fetched as part of loading this 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));
        // 4) Access SupplementalInfo's `customer` state
        final Customer customer = supplementalInfo.getCustomer();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
        customer.getId();
        customer.getName();
        assertThat(sqlStatementInterceptor.getSqlQueries().size(), is(1));
    });
}
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 4 with BytecodeLazyAttributeInterceptor

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

the class OneToOneAllowProxyTests 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 5 with BytecodeLazyAttributeInterceptor

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

the class ManyToOneExplicitOptionTests 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)

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