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;
}
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();
}
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));
});
}
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));
});
}
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));
});
}
Aggregations