use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor in project hibernate-orm by hibernate.
the class AbstractEntityEntry method isUnequivocallyNonDirty.
@SuppressWarnings({ "SimplifiableIfStatement" })
private boolean isUnequivocallyNonDirty(Object entity) {
if (entity instanceof SelfDirtinessTracker) {
boolean uninitializedProxy = false;
if (entity instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) entity;
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
EnhancementAsProxyLazinessInterceptor enhancementAsProxyLazinessInterceptor = (EnhancementAsProxyLazinessInterceptor) interceptor;
return !enhancementAsProxyLazinessInterceptor.hasWrittenFieldNames();
}
} else if (entity instanceof HibernateProxy) {
uninitializedProxy = ((HibernateProxy) entity).getHibernateLazyInitializer().isUninitialized();
}
// we never have to check an uninitialized proxy
return uninitializedProxy || !persister.hasCollections() && !persister.hasMutableProperties() && !((SelfDirtinessTracker) entity).$$_hibernate_hasDirtyAttributes();
}
if (entity instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) entity;
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
// we never have to check an uninitialized proxy
return true;
}
}
final CustomEntityDirtinessStrategy customEntityDirtinessStrategy = getPersistenceContext().getSession().getFactory().getCustomEntityDirtinessStrategy();
if (customEntityDirtinessStrategy.canDirtyCheck(entity, getPersister(), (Session) getPersistenceContext().getSession())) {
return !customEntityDirtinessStrategy.isDirty(entity, getPersister(), (Session) getPersistenceContext().getSession());
}
if (getPersister().hasMutableProperties()) {
return false;
}
return false;
}
use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor in project hibernate-orm by hibernate.
the class Hibernate method initialize.
/**
* Force initialization of a proxy or persistent collection. In the case of a
* many-valued association, only the collection itself is initialized. It is not
* guaranteed that the associated entities held within the collection will be
* initialized.
*
* @param proxy a persistable object, proxy, persistent collection or {@code null}
* @throws HibernateException if the proxy cannot be initialized at this time,
* for example, if the {@code Session} was closed
*/
public static void initialize(Object proxy) throws HibernateException {
if (proxy == null) {
return;
}
if (proxy instanceof HibernateProxy) {
((HibernateProxy) proxy).getHibernateLazyInitializer().initialize();
} else if (proxy instanceof PersistentCollection) {
((PersistentCollection<?>) proxy).forceInitialization();
} else if (proxy instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) proxy;
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if (interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
((EnhancementAsProxyLazinessInterceptor) interceptor).forceInitialize(proxy, null);
}
}
}
use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor in project hibernate-orm by hibernate.
the class DefaultMergeEventListener method unproxyManagedForDetachedMerging.
private Object unproxyManagedForDetachedMerging(Object incoming, Object managed, EntityPersister persister, EventSource source) {
if (managed instanceof HibernateProxy) {
return source.getPersistenceContextInternal().unproxy(managed);
}
if (incoming instanceof PersistentAttributeInterceptable && persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()) {
final PersistentAttributeInterceptor incomingInterceptor = ((PersistentAttributeInterceptable) incoming).$$_hibernate_getInterceptor();
final PersistentAttributeInterceptor managedInterceptor = ((PersistentAttributeInterceptable) managed).$$_hibernate_getInterceptor();
// if the managed entity is not a proxy, we can just return it
if (!(managedInterceptor instanceof EnhancementAsProxyLazinessInterceptor)) {
return managed;
}
// if the incoming entity is still a proxy there is no need to force initialization of the managed one
if (incomingInterceptor instanceof EnhancementAsProxyLazinessInterceptor) {
return managed;
}
// otherwise, force initialization
persister.initializeEnhancedEntityUsedAsProxy(managed, null, source);
}
return managed;
}
use of org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor 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.EnhancementAsProxyLazinessInterceptor in project hibernate-orm by hibernate.
the class EnhancedProxyCacheTest method testPreferenceFor2LCOverUninitializedProxy.
@Test
public void testPreferenceFor2LCOverUninitializedProxy() throws Exception {
final Statistics stats = sessionFactory().getStatistics();
storeTestData();
clearAllCaches();
stats.clear();
assertTrue(stats.isStatisticsEnabled());
assertEquals(0, stats.getEntityFetchCount());
assertEquals(0, stats.getSecondLevelCacheHitCount());
// First we load the Country once, then trigger initialization of the related Continent proxy.
// 2LC is empty, so stats should show that these objects are being loaded from the DB.
inSession(s -> {
Country nl = s.get(Country.class, countryId.get());
assertNotNull(nl);
assertEquals(0, stats.getSecondLevelCacheHitCount());
assertEquals(1, stats.getSecondLevelCacheMissCount());
assertEquals(1, stats.getEntityLoadCount());
Continent continent = nl.getContinent();
// Check that this is indeed an enhanced proxy so to ensure we're testing in the right conditions.
// The following casts should not fail:
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) continent;
final EnhancementAsProxyLazinessInterceptor interceptor = (EnhancementAsProxyLazinessInterceptor) interceptable.$$_hibernate_getInterceptor();
assertFalse(interceptor.isInitialized());
assertFalse(interceptor.isAttributeLoaded("code"));
// Trigger initialization of the enhanced proxy:
assertEquals("EU", continent.getCode());
assertTrue(interceptor.isInitialized());
assertEquals(0, stats.getSecondLevelCacheHitCount());
assertEquals(2, stats.getEntityLoadCount());
});
stats.clear();
// Now load the same objects again; we expect to hit 2LC this time,
// and we should see no needs to hit the DB.
// Also, since all data is readily available we won't need to make
// all attributes lazy.
inSession(s -> {
assertEquals(0, stats.getSecondLevelCacheHitCount());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(0, stats.getEntityLoadCount());
Country nl = s.get(Country.class, countryId.get());
assertNotNull(nl);
assertEquals(1, stats.getSecondLevelCacheHitCount());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(0, stats.getEntityLoadCount());
Continent continent = nl.getContinent();
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) continent;
final EnhancementAsProxyLazinessInterceptor interceptor = (EnhancementAsProxyLazinessInterceptor) interceptable.$$_hibernate_getInterceptor();
assertFalse(interceptor.isInitialized());
assertFalse(interceptor.isAttributeLoaded("code"));
assertEquals(1, stats.getSecondLevelCacheHitCount());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(0, stats.getEntityLoadCount());
// Trigger initialization of the enhanced proxy:
assertEquals("EU", continent.getCode());
assertTrue(interceptor.isInitialized());
assertEquals(2, stats.getSecondLevelCacheHitCount());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(0, stats.getEntityLoadCount());
});
}
Aggregations