Search in sources :

Example 31 with HibernateProxy

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

the class ReadOnlyProxyTest method testReadOnlyViaSessionBeforeInitByReadOnlyQuery.

@Test
public void testReadOnlyViaSessionBeforeInitByReadOnlyQuery() {
    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);
    DataPoint dpFromQuery = (DataPoint) s.createQuery("from DataPoint where id=" + dpOrig.getId()).setReadOnly(true).uniqueResult();
    assertTrue(Hibernate.isInitialized(dpFromQuery));
    assertSame(dp, dpFromQuery);
    checkReadOnly(s, dp, true);
    dp.setDescription("changed");
    assertEquals("changed", 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(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 32 with HibernateProxy

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

the class ReadOnlyProxyTest method testReadOnlyViaSessionDoesNotInit.

@Test
public void testReadOnlyViaSessionDoesNotInit() {
    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));
    s.setReadOnly(dp, true);
    checkReadOnly(s, dp, true);
    assertFalse(Hibernate.isInitialized(dp));
    s.setReadOnly(dp, false);
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    s.flush();
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    s.getTransaction().commit();
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    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 33 with HibernateProxy

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

the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerDoesNotInit.

@Test
public void testReadOnlyViaLazyInitializerDoesNotInit() {
    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);
    assertFalse(Hibernate.isInitialized(dp));
    dpLI.setReadOnly(true);
    checkReadOnly(s, dp, true);
    assertFalse(Hibernate.isInitialized(dp));
    dpLI.setReadOnly(false);
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    s.flush();
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    s.getTransaction().commit();
    checkReadOnly(s, dp, false);
    assertFalse(Hibernate.isInitialized(dp));
    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 34 with HibernateProxy

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

the class ReadOnlyProxyTest method testModifiableViaSessionBeforeInitByReadOnlyQuery.

@Test
public void testModifiableViaSessionBeforeInitByReadOnlyQuery() {
    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));
    DataPoint dpFromQuery = (DataPoint) s.createQuery("from DataPoint where id=" + dpOrig.getId()).setReadOnly(true).uniqueResult();
    assertTrue(Hibernate.isInitialized(dpFromQuery));
    assertSame(dp, dpFromQuery);
    checkReadOnly(s, dp, false);
    dp.setDescription("changed");
    assertEquals("changed", 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", 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 35 with HibernateProxy

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

the class ReadOnlyProxyTest method testReadOnlyRefreshDeleted.

@Test
public void testReadOnlyRefreshDeleted() {
    Session s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    Transaction t = s.beginTransaction();
    DataPoint dp = new DataPoint();
    dp.setDescription("original");
    dp.setX(new BigDecimal(0.1d).setScale(19, BigDecimal.ROUND_DOWN));
    dp.setY(new BigDecimal(Math.cos(dp.getX().doubleValue())).setScale(19, BigDecimal.ROUND_DOWN));
    s.save(dp);
    t.commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    t = s.beginTransaction();
    HibernateProxy dpProxy = (HibernateProxy) s.load(DataPoint.class, dp.getId());
    assertFalse(Hibernate.isInitialized(dpProxy));
    t.commit();
    s.close();
    s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    t = s.beginTransaction();
    dp = (DataPoint) s.get(DataPoint.class, dp.getId());
    s.delete(dp);
    s.flush();
    try {
        s.refresh(dp);
        fail("should have thrown UnresolvableObjectException");
    } catch (UnresolvableObjectException ex) {
    // expected
    } finally {
        t.rollback();
        s.close();
    }
    s = openSession();
    t = s.beginTransaction();
    s.setCacheMode(CacheMode.IGNORE);
    DataPoint dpProxyInit = (DataPoint) s.load(DataPoint.class, dp.getId());
    assertEquals("original", dp.getDescription());
    s.delete(dpProxyInit);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    assertTrue(dpProxyInit instanceof HibernateProxy);
    assertTrue(Hibernate.isInitialized(dpProxyInit));
    try {
        s.refresh(dpProxyInit);
        fail("should have thrown UnresolvableObjectException");
    } catch (UnresolvableObjectException ex) {
    // expected
    } finally {
        t.rollback();
        s.close();
    }
    s = openSession();
    t = s.beginTransaction();
    assertTrue(dpProxy instanceof HibernateProxy);
    try {
        s.refresh(dpProxy);
        assertFalse(Hibernate.isInitialized(dpProxy));
        Hibernate.initialize(dpProxy);
        fail("should have thrown UnresolvableObjectException");
    } catch (UnresolvableObjectException ex) {
    // expected
    } finally {
        t.rollback();
        s.close();
    }
}
Also used : Transaction(org.hibernate.Transaction) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) HibernateProxy(org.hibernate.proxy.HibernateProxy) BigDecimal(java.math.BigDecimal) 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