Search in sources :

Example 26 with LazyInitializer

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

the class ReadOnlyProxyTest method testModifiableViaLazyInitializerBeforeInit.

@Test
public void testModifiableViaLazyInitializerBeforeInit() {
    DataPoint dpOrig = createDataPoint(CacheMode.IGNORE);
    Session s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    s.beginTransaction();
    DataPoint dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
    assertTrue(dp instanceof HibernateProxy);
    LazyInitializer dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
    assertTrue(dp instanceof HibernateProxy);
    assertTrue(dpLI.isUninitialized());
    checkReadOnly(s, dp, false);
    dp.setDescription("changed");
    assertFalse(dpLI.isUninitialized());
    assertEquals("changed", dp.getDescription());
    checkReadOnly(s, dp, false);
    s.flush();
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    dp = (DataPoint) s.get(DataPoint.class, dpOrig.getId());
    assertEquals(dpOrig.getId(), dp.getId());
    assertEquals("changed", dp.getDescription());
    assertEquals(dpOrig.getX(), dp.getX());
    assertEquals(dpOrig.getY(), dp.getY());
    s.delete(dp);
    s.getTransaction().commit();
    s.close();
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 27 with LazyInitializer

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

the class PersistenceUtilHelper method isLoadedWithoutReference.

/**
 * 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 isLoadedWithoutReference(Object entity, String attributeName, MetadataCache cache) {
    boolean sureFromUs = false;
    if (entity instanceof HibernateProxy) {
        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();
        }
        sureFromUs = true;
    }
    // we are instrumenting but we can't assume we are the only ones
    if (entity instanceof PersistentAttributeInterceptable) {
        final LazyAttributeLoadingInterceptor interceptor = extractInterceptor((PersistentAttributeInterceptable) entity);
        final boolean isInitialized = interceptor == null || interceptor.isAttributeLoaded(attributeName);
        LoadState state;
        if (isInitialized && interceptor != null) {
            // it's ours, we can read
            try {
                final Class entityClass = entity.getClass();
                final Object attributeValue = cache.getClassMetadata(entityClass).getAttributeAccess(attributeName).extractValue(entity);
                state = isLoaded(attributeValue);
                // it's ours so we know it's loaded
                if (state == LoadState.UNKNOWN) {
                    state = LoadState.LOADED;
                }
            } catch (AttributeExtractionException ignore) {
                state = LoadState.UNKNOWN;
            }
        } else if (interceptor != null) {
            state = LoadState.NOT_LOADED;
        } else if (sureFromUs) {
            // it's ours, we can read
            try {
                final Class entityClass = entity.getClass();
                final Object attributeValue = cache.getClassMetadata(entityClass).getAttributeAccess(attributeName).extractValue(entity);
                state = isLoaded(attributeValue);
                // it's ours so we know it's loaded
                if (state == LoadState.UNKNOWN) {
                    state = LoadState.LOADED;
                }
            } catch (AttributeExtractionException ignore) {
                state = LoadState.UNKNOWN;
            }
        } else {
            state = LoadState.UNKNOWN;
        }
        return state;
    } else {
        return LoadState.UNKNOWN;
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) LazyAttributeLoadingInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) LoadState(javax.persistence.spi.LoadState) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 28 with LazyInitializer

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

the class Hibernate method unproxy.

/**
 * Unproxies a {@link HibernateProxy}. If the proxy is uninitialized, it automatically triggers an initialization.
 * In case the supplied object is null or not a proxy, the object will be returned as-is.
 *
 * @param proxy the {@link HibernateProxy} to be unproxied
 * @return the proxy's underlying implementation object, or the supplied object otherwise
 */
public static Object unproxy(Object proxy) {
    if (proxy instanceof HibernateProxy) {
        HibernateProxy hibernateProxy = (HibernateProxy) proxy;
        LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();
        return initializer.getImplementation();
    } else {
        return proxy;
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 29 with LazyInitializer

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

the class StatefulPersistenceContext method unproxy.

@Override
public Object unproxy(Object maybeProxy) throws HibernateException {
    if (maybeProxy instanceof HibernateProxy) {
        final HibernateProxy proxy = (HibernateProxy) maybeProxy;
        final LazyInitializer li = proxy.getHibernateLazyInitializer();
        if (li.isUninitialized()) {
            throw new PersistentObjectException("object was an uninitialized proxy for " + li.getEntityName());
        }
        // unwrap the object and return
        return li.getImplementation();
    } else {
        return maybeProxy;
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) PersistentObjectException(org.hibernate.PersistentObjectException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 30 with LazyInitializer

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

the class StatefulPersistenceContext method unproxyAndReassociate.

@Override
public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException {
    if (maybeProxy instanceof HibernateProxy) {
        final HibernateProxy proxy = (HibernateProxy) maybeProxy;
        final LazyInitializer li = proxy.getHibernateLazyInitializer();
        reassociateProxy(li, proxy);
        // initialize + unwrap the object and return it
        return li.getImplementation();
    } else {
        return maybeProxy;
    }
}
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