Search in sources :

Example 6 with TransientObjectException

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

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

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

the class ReadOnlyProxyTest method testDetachedIsReadOnlyAfterEvictViaSession.

@Test
public void testDetachedIsReadOnlyAfterEvictViaSession() {
    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.isReadOnly(dp);
        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 9 with TransientObjectException

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

the class StatefulPersistenceContext method setEntityReadOnly.

private void setEntityReadOnly(Object entity, boolean readOnly) {
    final EntityEntry entry = getEntry(entity);
    if (entry == null) {
        throw new TransientObjectException("Instance was not associated with this persistence context");
    }
    entry.setReadOnly(readOnly, entity);
    hasNonReadOnlyEntities = hasNonReadOnlyEntities || !readOnly;
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) EntityEntry(org.hibernate.engine.spi.EntityEntry)

Example 10 with TransientObjectException

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

the class DefaultDeleteEventListener method onDelete.

/**
	 * Handle the given delete event.  This is the cascaded form.
	 *
	 * @param event The delete event.
	 * @param transientEntities The cache of entities already deleted
	 *
	 * @throws HibernateException
	 */
public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
    final EventSource source = event.getSession();
    final PersistenceContext persistenceContext = source.getPersistenceContext();
    Object entity = persistenceContext.unproxyAndReassociate(event.getObject());
    EntityEntry entityEntry = persistenceContext.getEntry(entity);
    final EntityPersister persister;
    final Serializable id;
    final Object version;
    if (entityEntry == null) {
        LOG.trace("Entity was not persistent in delete processing");
        persister = source.getEntityPersister(event.getEntityName(), entity);
        if (ForeignKeys.isTransient(persister.getEntityName(), entity, null, source)) {
            deleteTransientEntity(source, entity, event.isCascadeDeleteEnabled(), persister, transientEntities);
            // EARLY EXIT!!!
            return;
        }
        performDetachedEntityDeletionCheck(event);
        id = persister.getIdentifier(entity, source);
        if (id == null) {
            throw new TransientObjectException("the detached instance passed to delete() had a null identifier");
        }
        final EntityKey key = source.generateEntityKey(id, persister);
        persistenceContext.checkUniqueness(key, entity);
        new OnUpdateVisitor(source, id, entity).process(entity, persister);
        version = persister.getVersion(entity);
        entityEntry = persistenceContext.addEntity(entity, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), persister.getPropertyValues(entity), key, version, LockMode.NONE, true, persister, false);
    } else {
        LOG.trace("Deleting a persistent instance");
        if (entityEntry.getStatus() == Status.DELETED || entityEntry.getStatus() == Status.GONE) {
            LOG.trace("Object was already deleted");
            return;
        }
        persister = entityEntry.getPersister();
        id = entityEntry.getId();
        version = entityEntry.getVersion();
    }
    if (invokeDeleteLifecycle(source, entity, persister)) {
        return;
    }
    deleteEntity(source, entity, entityEntry, event.isCascadeDeleteEnabled(), event.isOrphanRemovalBeforeUpdates(), persister, transientEntities);
    if (source.getFactory().getSettings().isIdentifierRollbackEnabled()) {
        persister.resetIdentifier(entity, id, version, source);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) TransientObjectException(org.hibernate.TransientObjectException) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

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