use of org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer 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.LazyPropertyInitializer in project hibernate-orm by hibernate.
the class LazyAttributeLoadingInterceptor method loadAttribute.
protected Object loadAttribute(final Object target, final String attributeName) {
return EnhancementHelper.performWork(this, (session, isTemporarySession) -> {
final EntityPersister persister = session.getFactory().getMappingMetamodel().getEntityDescriptor(getEntityName());
if (isTemporarySession) {
final Object id = persister.getIdentifier(target, session);
// Add an entry for this entity in the PC of the temp Session
// NOTE : a few arguments that would be nice to pass along here...
// 1) loadedState if we know any
final Object[] loadedState = null;
// 2) does a row exist in the db for this entity?
final boolean existsInDb = true;
session.getPersistenceContextInternal().addEntity(target, Status.READ_ONLY, loadedState, session.generateEntityKey(id, persister), persister.getVersion(target), LockMode.NONE, existsInDb, persister, true);
}
final LazyPropertyInitializer initializer = (LazyPropertyInitializer) persister;
final Object loadedValue = initializer.initializeLazyProperty(attributeName, target, session);
takeCollectionSizeSnapshot(target, attributeName, loadedValue);
return loadedValue;
}, getEntityName(), attributeName);
}
Aggregations