Search in sources :

Example 36 with HibernateProxy

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

the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerAfterInit.

@Test
public void testReadOnlyViaLazyInitializerAfterInit() {
    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(dpLI.isUninitialized());
    checkReadOnly(s, dp, false);
    dp.setDescription("changed");
    assertFalse(dpLI.isUninitialized());
    assertEquals("changed", dp.getDescription());
    checkReadOnly(s, dp, false);
    dpLI.setReadOnly(true);
    checkReadOnly(s, dp, true);
    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(dpOrig.getDescription(), 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 37 with HibernateProxy

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

the class ReadOnlyProxyTest method testSetReadOnlyAfterSessionClosed.

@Test
public void testSetReadOnlyAfterSessionClosed() {
    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);
    assertFalse(Hibernate.isInitialized(dp));
    checkReadOnly(s, dp, false);
    s.getTransaction().commit();
    s.close();
    try {
        s.setReadOnly(dp, true);
        fail("should have failed because session was closed");
    } catch (IllegalStateException ex) {
        // expected
        assertFalse(((HibernateProxy) dp).getHibernateLazyInitializer().isReadOnlySettingAvailable());
    } finally {
        s = openSession();
        s.beginTransaction();
        s.delete(dp);
        s.getTransaction().commit();
        s.close();
    }
}
Also used : HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 38 with HibernateProxy

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

the class StatefulPersistenceContext method reassociateIfUninitializedProxy.

@Override
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException {
    if (!Hibernate.isInitialized(value)) {
        final HibernateProxy proxy = (HibernateProxy) value;
        final LazyInitializer li = proxy.getHibernateLazyInitializer();
        reassociateProxy(li, proxy);
        return true;
    } else {
        return false;
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 39 with HibernateProxy

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

the class SessionImpl method bestGuessEntityName.

@Override
public String bestGuessEntityName(Object object) {
    if (object instanceof HibernateProxy) {
        LazyInitializer initializer = ((HibernateProxy) object).getHibernateLazyInitializer();
        // so make certain that we do not accidentally initialize an uninitialized proxy
        if (initializer.isUninitialized()) {
            return initializer.getEntityName();
        }
        object = initializer.getImplementation();
    }
    EntityEntry entry = persistenceContext.getEntry(object);
    if (entry == null) {
        return guessEntityName(object);
    } else {
        return entry.getPersister().getEntityName();
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 40 with HibernateProxy

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

the class SessionImpl method getCurrentLockMode.

@Override
public LockMode getCurrentLockMode(Object object) throws HibernateException {
    checkOpen();
    checkTransactionSynchStatus();
    if (object == null) {
        throw new NullPointerException("null object passed to getCurrentLockMode()");
    }
    if (object instanceof HibernateProxy) {
        object = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation(this);
        if (object == null) {
            return LockMode.NONE;
        }
    }
    EntityEntry e = persistenceContext.getEntry(object);
    if (e == null) {
        throw new TransientObjectException("Given object not associated with the session");
    }
    if (e.getStatus() != Status.MANAGED) {
        throw new ObjectDeletedException("The given object was deleted", e.getId(), e.getPersister().getEntityName());
    }
    return e.getLockMode();
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) EntityEntry(org.hibernate.engine.spi.EntityEntry) ObjectDeletedException(org.hibernate.ObjectDeletedException) 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