Search in sources :

Example 1 with LazyPropertyInitializer

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();
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) LazyPropertyInitializer(org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer) EventSource(org.hibernate.event.spi.EventSource) LoadEvent(org.hibernate.event.spi.LoadEvent) LazyAttributeLoadingInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor) BytecodeLazyAttributeInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) StatefulPersistenceContext(org.hibernate.engine.internal.StatefulPersistenceContext) BytecodeEnhancementMetadata(org.hibernate.bytecode.spi.BytecodeEnhancementMetadata) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor)

Example 2 with LazyPropertyInitializer

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);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LazyPropertyInitializer(org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer)

Aggregations

LazyPropertyInitializer (org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer)2 BytecodeLazyAttributeInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor)1 EnhancementAsProxyLazinessInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor)1 LazyAttributeLoadingInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor)1 BytecodeEnhancementMetadata (org.hibernate.bytecode.spi.BytecodeEnhancementMetadata)1 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 EventSource (org.hibernate.event.spi.EventSource)1 LoadEvent (org.hibernate.event.spi.LoadEvent)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1