Search in sources :

Example 16 with TransientObjectException

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

the class ReadOnlyProxyTest method testSetReadOnlyAfterSessionClosedViaLazyInitializer.

@Test
public void testSetReadOnlyAfterSessionClosedViaLazyInitializer() {
    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().setReadOnly(true);
        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 17 with TransientObjectException

use of org.hibernate.TransientObjectException 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 18 with TransientObjectException

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

the class ExceptionConverterImpl method convert.

@Override
public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
    Throwable cause = e;
    if (cause instanceof StaleStateException) {
        final PersistenceException converted = wrapStaleStateException((StaleStateException) cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof LockingStrategyException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.exception.LockTimeoutException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.PessimisticLockException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.QueryTimeoutException) {
        final QueryTimeoutException converted = new QueryTimeoutException(cause.getMessage(), cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof ObjectNotFoundException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueObjectException) {
        final EntityExistsException converted = new EntityExistsException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueResultException) {
        final NonUniqueResultException converted = new NonUniqueResultException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof UnresolvableObjectException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof QueryException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof MultipleBagFetchException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof TransientObjectException) {
        try {
            sharedSessionContract.markForRollbackOnly();
        } catch (Exception ne) {
            //we do not want the subsequent exception to swallow the original one
            log.unableToMarkForRollbackOnTransientObjectException(ne);
        }
        //Spec 3.2.3 Synchronization rules
        return new IllegalStateException(e);
    } else {
        final PersistenceException converted = new PersistenceException(cause);
        handlePersistenceException(converted);
        return converted;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) TransientObjectException(org.hibernate.TransientObjectException) HibernateException(org.hibernate.HibernateException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityExistsException(javax.persistence.EntityExistsException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) NoResultException(javax.persistence.NoResultException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) SQLException(java.sql.SQLException) NonUniqueResultException(javax.persistence.NonUniqueResultException) JDBCException(org.hibernate.JDBCException) EntityNotFoundException(javax.persistence.EntityNotFoundException) StaleStateException(org.hibernate.StaleStateException) OptimisticEntityLockException(org.hibernate.dialect.lock.OptimisticEntityLockException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityExistsException(javax.persistence.EntityExistsException) OptimisticLockException(javax.persistence.OptimisticLockException) StaleObjectStateException(org.hibernate.StaleObjectStateException) PessimisticLockException(javax.persistence.PessimisticLockException) PessimisticEntityLockException(org.hibernate.dialect.lock.PessimisticEntityLockException) TransientObjectException(org.hibernate.TransientObjectException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) QueryTimeoutException(javax.persistence.QueryTimeoutException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) QueryTimeoutException(javax.persistence.QueryTimeoutException) QueryException(org.hibernate.QueryException) StaleStateException(org.hibernate.StaleStateException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) PersistenceException(javax.persistence.PersistenceException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException)

Example 19 with TransientObjectException

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

the class SessionImpl method getEntityName.

@Override
public String getEntityName(Object object) {
    checkOpen();
    //		checkTransactionSynchStatus();
    if (object instanceof HibernateProxy) {
        if (!persistenceContext.containsProxy(object)) {
            throw new TransientObjectException("proxy was not associated with the session");
        }
        object = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
    }
    EntityEntry entry = persistenceContext.getEntry(object);
    if (entry == null) {
        throwTransientObjectException(object);
    }
    return entry.getPersister().getEntityName();
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 20 with TransientObjectException

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

the class CascadeTest method testManyToOnePropertyRefAssignedIds.

public void testManyToOnePropertyRefAssignedIds() {
    try {
        Session s = openSession();
        s.beginTransaction();
        ParentAssigned p = new ParentAssigned(new Long(1), "parent");
        OtherAssigned other = new OtherAssigned(new Long(2));
        other.setOwner(p);
        s.persist(other);
        try {
            s.getTransaction().commit();
            fail("expecting TransientObjectException on flush");
        } catch (TransientObjectException e) {
            // expected result
            log.trace("handled expected exception", e);
            s.getTransaction().rollback();
        } finally {
            s.close();
        }
    } finally {
        cleanupData();
    }
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) Session(org.hibernate.Session)

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