Search in sources :

Example 11 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class SessionImpl method getIdentifier.

// not for internal use:
@Override
public Serializable getIdentifier(Object object) throws HibernateException {
    checkOpen();
    checkTransactionSynchStatus();
    if (object instanceof HibernateProxy) {
        LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
        if (li.getSession() != this) {
            throw new TransientObjectException("The proxy was not associated with this session");
        }
        return li.getIdentifier();
    } else {
        EntityEntry entry = persistenceContext.getEntry(object);
        if (entry == null) {
            throw new TransientObjectException("The instance was not associated with this session");
        }
        return entry.getId();
    }
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 12 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class PersistenceUtilHelper method isLoadedWithReference.

/**
 * 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 isLoadedWithReference(Object entity, String attributeName, MetadataCache cache) {
    if (entity instanceof HibernateProxy) {
        final 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();
        }
    }
    try {
        final Class entityClass = entity.getClass();
        final Object attributeValue = cache.getClassMetadata(entityClass).getAttributeAccess(attributeName).extractValue(entity);
        return isLoaded(attributeValue);
    } catch (AttributeExtractionException ignore) {
        return LoadState.UNKNOWN;
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 13 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class SessionImpl method contains.

@Override
public boolean contains(Object object) {
    checkOpen();
    checkTransactionSynchStatus();
    if (object == null) {
        return false;
    }
    try {
        if (object instanceof HibernateProxy) {
            // do not use proxiesByKey, since not all
            // proxies that point to this session's
            // instances are in that collection!
            LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
            if (li.isUninitialized()) {
                // the underlying instance will be "contained"
                return li.getSession() == this;
            } else {
                // if it is initialized, see if the underlying
                // instance is contained, since we need to
                // account for the fact that it might have been
                // evicted
                object = li.getImplementation();
            }
        }
        // A session is considered to contain an entity only if the entity has
        // an entry in the session's persistence context and the entry reports
        // that the entity has not been removed
        EntityEntry entry = persistenceContext.getEntry(object);
        delayedAfterCompletion();
        if (entry == null) {
            if (!HibernateProxy.class.isInstance(object) && persistenceContext.getEntry(object) == null) {
                // check if it is even an entity -> if not throw an exception (per JPA)
                try {
                    final String entityName = getEntityNameResolver().resolveEntityName(object);
                    if (entityName == null) {
                        throw new IllegalArgumentException("Could not resolve entity-name [" + object + "]");
                    }
                    getSessionFactory().getMetamodel().entityPersister(entityName);
                } catch (HibernateException e) {
                    throw new IllegalArgumentException("Not an entity [" + object.getClass() + "]", e);
                }
            }
            return false;
        } else {
            return entry.getStatus() != Status.DELETED && entry.getStatus() != Status.GONE;
        }
    } catch (MappingException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e);
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException)

Example 14 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class SessionImpl method contains.

@Override
public boolean contains(String entityName, Object object) {
    checkOpen();
    checkTransactionSynchStatus();
    if (object == null) {
        return false;
    }
    try {
        if (!HibernateProxy.class.isInstance(object) && persistenceContext.getEntry(object) == null) {
            // check if it is an entity -> if not throw an exception (per JPA)
            try {
                getSessionFactory().getMetamodel().entityPersister(entityName);
            } catch (HibernateException e) {
                throw new IllegalArgumentException("Not an entity [" + entityName + "] : " + object);
            }
        }
        if (object instanceof HibernateProxy) {
            // do not use proxiesByKey, since not all
            // proxies that point to this session's
            // instances are in that collection!
            LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
            if (li.isUninitialized()) {
                // the underlying instance will be "contained"
                return li.getSession() == this;
            } else {
                // if it is initialized, see if the underlying
                // instance is contained, since we need to
                // account for the fact that it might have been
                // evicted
                object = li.getImplementation();
            }
        }
        // A session is considered to contain an entity only if the entity has
        // an entry in the session's persistence context and the entry reports
        // that the entity has not been removed
        EntityEntry entry = persistenceContext.getEntry(object);
        delayedAfterCompletion();
        return entry != null && entry.getStatus() != Status.DELETED && entry.getStatus() != Status.GONE;
    } catch (MappingException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e);
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException)

Example 15 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class DefaultLoadEventListener method returnNarrowedProxy.

/**
 * Given a proxy, initialize it and/or narrow it provided either
 * is necessary.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 * @param persistenceContext The originating session
 * @param proxy The proxy to narrow
 *
 * @return The created/existing proxy
 */
private Object returnNarrowedProxy(final LoadEvent event, final EntityPersister persister, final EntityKey keyToLoad, final LoadEventListener.LoadType options, final PersistenceContext persistenceContext, final Object proxy) {
    if (traceEnabled) {
        LOG.trace("Entity proxy found in session cache");
    }
    LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
    if (li.isUnwrap()) {
        return li.getImplementation();
    }
    Object impl = null;
    if (!options.isAllowProxyCreation()) {
        impl = load(event, persister, keyToLoad, options);
        if (impl == null) {
            event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound(persister.getEntityName(), keyToLoad.getIdentifier());
        }
    }
    return persistenceContext.narrowProxy(proxy, persister, keyToLoad, impl);
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Aggregations

HibernateProxy (org.hibernate.proxy.HibernateProxy)33 LazyInitializer (org.hibernate.proxy.LazyInitializer)33 Session (org.hibernate.Session)7 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 Test (org.junit.Test)7 Serializable (java.io.Serializable)3 HibernateException (org.hibernate.HibernateException)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 MappingException (org.hibernate.MappingException)2 ObjectDeletedException (org.hibernate.ObjectDeletedException)2 PersistentObjectException (org.hibernate.PersistentObjectException)2 SessionImplementor (org.hibernate.engine.SessionImplementor)2 EntityKey (org.hibernate.engine.spi.EntityKey)2 EventSource (org.hibernate.event.spi.EventSource)2 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1