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