Search in sources :

Example 21 with HibernateProxy

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

the class ReadOnlyProxyTest method testIsReadOnlyAfterSessionClosedViaLazyInitializer.

@Test
public void testIsReadOnlyAfterSessionClosedViaLazyInitializer() {
    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();
    assertTrue(s.contains(dp));
    s.close();
    assertNull(((HibernateProxy) dp).getHibernateLazyInitializer().getSession());
    try {
        ((HibernateProxy) dp).getHibernateLazyInitializer().isReadOnly();
        fail("should have failed because session was detached");
    } catch (TransientObjectException ex) {
        // expected
        assertFalse(((HibernateProxy) dp).getHibernateLazyInitializer().isReadOnlySettingAvailable());
    } finally {
        s = openSession();
        s.beginTransaction();
        s.delete(dp);
        s.getTransaction().commit();
        s.close();
    }
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 22 with HibernateProxy

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

the class ReadOnlyProxyTest method testReadOnlyProxyMergeDetachedProxyWithChange.

@Test
public void testReadOnlyProxyMergeDetachedProxyWithChange() {
    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);
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    Hibernate.initialize(dp);
    assertTrue(Hibernate.isInitialized(dp));
    s.getTransaction().commit();
    s.close();
    // modify detached proxy
    dp.setDescription("changed");
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    s.beginTransaction();
    DataPoint dpLoaded = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
    assertTrue(dpLoaded instanceof HibernateProxy);
    checkReadOnly(s, dpLoaded, false);
    s.setReadOnly(dpLoaded, true);
    checkReadOnly(s, dpLoaded, true);
    assertFalse(Hibernate.isInitialized(dpLoaded));
    DataPoint dpMerged = (DataPoint) s.merge(dp);
    assertSame(dpLoaded, dpMerged);
    assertTrue(Hibernate.isInitialized(dpLoaded));
    assertEquals("changed", dpLoaded.getDescription());
    checkReadOnly(s, dpLoaded, 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 : HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 23 with HibernateProxy

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

the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerNoChangeAfterInit.

@Test
public void testReadOnlyViaLazyInitializerNoChangeAfterInit() {
    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();
    checkReadOnly(s, dp, false);
    assertTrue(dpLI.isUninitialized());
    Hibernate.initialize(dp);
    assertFalse(dpLI.isUninitialized());
    checkReadOnly(s, dp, false);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    s.beginTransaction();
    dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
    assertTrue(dp instanceof HibernateProxy);
    dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
    dpLI.setReadOnly(true);
    checkReadOnly(s, dp, true);
    assertTrue(dpLI.isUninitialized());
    Hibernate.initialize(dp);
    assertFalse(dpLI.isUninitialized());
    checkReadOnly(s, dp, true);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    s.beginTransaction();
    dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
    assertTrue(dp instanceof HibernateProxy);
    dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
    dpLI.setReadOnly(true);
    checkReadOnly(s, dp, true);
    assertTrue(dpLI.isUninitialized());
    dpLI.setReadOnly(false);
    checkReadOnly(s, dp, false);
    assertTrue(dpLI.isUninitialized());
    Hibernate.initialize(dp);
    assertFalse(dpLI.isUninitialized());
    checkReadOnly(s, dp, false);
    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 24 with HibernateProxy

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

the class ReadOnlyProxyTest method testDetachedSetReadOnlyAfterEvictViaLazyInitializer.

@Test
public void testDetachedSetReadOnlyAfterEvictViaLazyInitializer() {
    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.evict(dp);
    assertFalse(s.contains(dp));
    assertNull(((HibernateProxy) dp).getHibernateLazyInitializer().getSession());
    try {
        ((HibernateProxy) dp).getHibernateLazyInitializer().setReadOnly(true);
        fail("should have failed because proxy was detached");
    } catch (TransientObjectException ex) {
        // expected
        assertFalse(((HibernateProxy) dp).getHibernateLazyInitializer().isReadOnlySettingAvailable());
    } finally {
        s.delete(dp);
        s.getTransaction().commit();
        s.close();
    }
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 25 with HibernateProxy

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

the class ReadOnlyProxyTest method testSetModifiableBetweenTwoTransactionsSameSession.

@Test
public void testSetModifiableBetweenTwoTransactionsSameSession() {
    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.setReadOnly(dp, true);
    checkReadOnly(s, dp, true);
    dp.setDescription("changed");
    assertTrue(Hibernate.isInitialized(dp));
    assertEquals("changed", dp.getDescription());
    checkReadOnly(s, dp, true);
    s.flush();
    s.getTransaction().commit();
    checkReadOnly(s, dp, true);
    s.setReadOnly(dp, false);
    checkReadOnly(s, dp, false);
    s.beginTransaction();
    checkReadOnly(s, dp, false);
    assertEquals("changed", dp.getDescription());
    s.refresh(dp);
    assertEquals(dpOrig.getDescription(), dp.getDescription());
    checkReadOnly(s, dp, false);
    dp.setDescription("changed again");
    assertEquals("changed again", dp.getDescription());
    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 again", dp.getDescription());
    assertEquals(dpOrig.getX(), dp.getX());
    assertEquals(dpOrig.getY(), dp.getY());
    s.delete(dp);
    s.getTransaction().commit();
    s.close();
}
Also used : HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

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