Search in sources :

Example 26 with TransientObjectException

use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.

the class ReadOnlyProxyTest method testDetachedSetReadOnlyAfterEvictViaSession.

@Test
public void testDetachedSetReadOnlyAfterEvictViaSession() {
    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);
    assertTrue(s.contains(dp));
    s.evict(dp);
    assertFalse(s.contains(dp));
    assertNull(((HibernateProxy) dp).getHibernateLazyInitializer().getSession());
    try {
        s.setReadOnly(dp, 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 27 with TransientObjectException

use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.

the class ReadOnlyProxyTest method testDetachedIsReadOnlyAfterEvictViaLazyInitializer.

@Test
public void testDetachedIsReadOnlyAfterEvictViaLazyInitializer() {
    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().isReadOnly();
        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)

Aggregations

TransientObjectException (org.hibernate.TransientObjectException)27 Session (org.hibernate.Session)16 HibernateProxy (org.hibernate.proxy.HibernateProxy)10 Test (org.junit.Test)8 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 Serializable (java.io.Serializable)4 EntityPersister (org.hibernate.persister.entity.EntityPersister)4 SQLException (java.sql.SQLException)2 HibernateException (org.hibernate.HibernateException)2 EntityKey (org.hibernate.engine.spi.EntityKey)2 EventSource (org.hibernate.event.spi.EventSource)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 NamingException (javax.naming.NamingException)1 EntityExistsException (javax.persistence.EntityExistsException)1 EntityManager (javax.persistence.EntityManager)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 LockTimeoutException (javax.persistence.LockTimeoutException)1 NoResultException (javax.persistence.NoResultException)1 NonUniqueResultException (javax.persistence.NonUniqueResultException)1