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