Search in sources :

Example 81 with HibernateProxy

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

the class MultiPathCascadeTest method testMultiPathMergeModifiedDetachedIntoProxy.

@Test
public void testMultiPathMergeModifiedDetachedIntoProxy() throws Exception {
    // persist a simple A in the database
    Session s = openSession();
    s.beginTransaction();
    A a = new A();
    a.setData("Anna");
    s.save(a);
    s.getTransaction().commit();
    s.close();
    // modify detached entity
    modifyEntity(a);
    s = openSession();
    s.beginTransaction();
    A aLoaded = (A) s.load(A.class, new Long(a.getId()));
    assertTrue(aLoaded instanceof HibernateProxy);
    assertSame(aLoaded, s.merge(a));
    s.getTransaction().commit();
    s.close();
    verifyModifications(a.getId());
}
Also used : HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 82 with HibernateProxy

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

the class StatefulPersistenceContext method reassociateProxy.

@Override
public void reassociateProxy(Object value, Serializable id) throws MappingException {
    if (value instanceof HibernateProxy) {
        LOG.debugf("Setting proxy identifier: %s", id);
        final HibernateProxy proxy = (HibernateProxy) value;
        final LazyInitializer li = proxy.getHibernateLazyInitializer();
        li.setIdentifier(id);
        reassociateProxy(li, proxy);
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 83 with HibernateProxy

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

the class StatefulPersistenceContext method isReadOnly.

@Override
public boolean isReadOnly(Object entityOrProxy) {
    if (entityOrProxy == null) {
        throw new AssertionFailure("object must be non-null.");
    }
    boolean isReadOnly;
    if (entityOrProxy instanceof HibernateProxy) {
        isReadOnly = ((HibernateProxy) entityOrProxy).getHibernateLazyInitializer().isReadOnly();
    } else {
        final EntityEntry ee = getEntry(entityOrProxy);
        if (ee == null) {
            throw new TransientObjectException("Instance was not associated with this persistence context");
        }
        isReadOnly = ee.isReadOnly();
    }
    return isReadOnly;
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 84 with HibernateProxy

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

the class StatefulPersistenceContext method setReadOnly.

@Override
public void setReadOnly(Object object, boolean readOnly) {
    if (object == null) {
        throw new AssertionFailure("object must be non-null.");
    }
    if (isReadOnly(object) == readOnly) {
        return;
    }
    if (object instanceof HibernateProxy) {
        final HibernateProxy proxy = (HibernateProxy) object;
        setProxyReadOnly(proxy, readOnly);
        if (Hibernate.isInitialized(proxy)) {
            setEntityReadOnly(proxy.getHibernateLazyInitializer().getImplementation(), readOnly);
        }
    } else {
        setEntityReadOnly(object, readOnly);
        // PersistenceContext.proxyFor( entity ) returns entity if there is no proxy for that entity
        // so need to check the return value to be sure it is really a proxy
        final Object maybeProxy = getSession().getPersistenceContext().proxyFor(object);
        if (maybeProxy instanceof HibernateProxy) {
            setProxyReadOnly((HibernateProxy) maybeProxy, readOnly);
        }
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 85 with HibernateProxy

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

the class StatefulPersistenceContext method narrowProxy.

@Override
@SuppressWarnings("unchecked")
public Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key, Object object) throws HibernateException {
    final Class concreteProxyClass = persister.getConcreteProxyClass();
    final boolean alreadyNarrow = concreteProxyClass.isInstance(proxy);
    if (!alreadyNarrow) {
        LOG.narrowingProxy(concreteProxyClass);
        // It would just be extra processing.  Just return the impl
        if (object != null) {
            proxiesByKey.remove(key);
            return object;
        }
        // Similarly, if the original HibernateProxy is initialized, there
        // is again no point in creating a proxy.  Just return the impl
        final HibernateProxy originalHibernateProxy = (HibernateProxy) proxy;
        if (!originalHibernateProxy.getHibernateLazyInitializer().isUninitialized()) {
            final Object impl = originalHibernateProxy.getHibernateLazyInitializer().getImplementation();
            // can we return it?
            if (concreteProxyClass.isInstance(impl)) {
                proxiesByKey.remove(key);
                return impl;
            }
        }
        // Otherwise, create the narrowed proxy
        final HibernateProxy narrowedProxy = (HibernateProxy) persister.createProxy(key.getIdentifier(), session);
        // set the read-only/modifiable mode in the new proxy to what it was in the original proxy
        final boolean readOnlyOrig = originalHibernateProxy.getHibernateLazyInitializer().isReadOnly();
        narrowedProxy.getHibernateLazyInitializer().setReadOnly(readOnlyOrig);
        return narrowedProxy;
    } else {
        if (object != null) {
            final LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
            li.setImplementation(object);
        }
        return proxy;
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Aggregations

HibernateProxy (org.hibernate.proxy.HibernateProxy)130 Session (org.hibernate.Session)58 Test (org.junit.Test)56 LazyInitializer (org.hibernate.proxy.LazyInitializer)33 DefaultPostLoaderDao (org.broadleafcommerce.common.persistence.DefaultPostLoaderDao)16 PostLoaderDao (org.broadleafcommerce.common.persistence.PostLoaderDao)16 EntityEntry (org.hibernate.engine.spi.EntityEntry)13 Serializable (java.io.Serializable)11 Transaction (org.hibernate.Transaction)10 TransientObjectException (org.hibernate.TransientObjectException)10 EntityPersister (org.hibernate.persister.entity.EntityPersister)10 HibernateException (org.hibernate.HibernateException)8 AdminPresentationMergeOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride)5 EntityKey (org.hibernate.engine.spi.EntityKey)4 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 Iterator (java.util.Iterator)3 ObjectDeletedException (org.hibernate.ObjectDeletedException)3 EventSource (org.hibernate.event.spi.EventSource)3 BigDecimal (java.math.BigDecimal)2