Search in sources :

Example 1 with TransientPropertyValueException

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

the class MultiCircleJpaCascadeTest method testPersistThenUpdateNoCascadeToTransient.

@Test
public void testPersistThenUpdateNoCascadeToTransient() {
    // expected to fail, so nothing will be persisted.
    skipCleanup = true;
    // remove elements from collections and persist
    c.getBCollection().clear();
    c.getDCollection().clear();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(c);
    // now add the elements back
    c.getBCollection().add(b);
    c.getDCollection().add(d);
    try {
        em.getTransaction().commit();
        fail("should have thrown IllegalStateException");
    } catch (RollbackException ex) {
        assertTrue(ex.getCause() instanceof IllegalStateException);
        IllegalStateException ise = (IllegalStateException) ex.getCause();
        // should fail on entity g (due to no cascade to f.g);
        // instead it fails on entity e ( due to no cascade to d.e)
        // because e is not in the process of being saved yet.
        // when HHH-6999 is fixed, this test should be changed to
        // check for g and f.g
        //noinspection ThrowableResultOfMethodCallIgnored
        TransientPropertyValueException tpve = assertTyping(TransientPropertyValueException.class, ise.getCause());
        assertEquals(E.class.getName(), tpve.getTransientEntityName());
        assertEquals(D.class.getName(), tpve.getPropertyOwnerEntityName());
        assertEquals("e", tpve.getPropertyName());
    }
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) TransientPropertyValueException(org.hibernate.TransientPropertyValueException) RollbackException(javax.persistence.RollbackException) Test(org.junit.Test)

Example 2 with TransientPropertyValueException

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

the class UnresolvedEntityInsertActions method checkNoUnresolvedActionsAfterOperation.

/**
	 * Throws {@link org.hibernate.PropertyValueException} if there are any unresolved
	 * entity insert actions that depend on non-nullable associations with
	 * a transient entity. This method should be called on completion of
	 * an operation (afterQuery all cascades are completed) that saves an entity.
	 *
	 * @throws org.hibernate.PropertyValueException if there are any unresolved entity
	 * insert actions; {@link org.hibernate.PropertyValueException#getEntityName()}
	 * and {@link org.hibernate.PropertyValueException#getPropertyName()} will
	 * return the entity name and property value for the first unresolved
	 * entity insert action.
	 */
public void checkNoUnresolvedActionsAfterOperation() throws PropertyValueException {
    if (isEmpty()) {
        LOG.trace("No entity insert actions have non-nullable, transient entity dependencies.");
    } else {
        final AbstractEntityInsertAction firstDependentAction = dependenciesByAction.keySet().iterator().next();
        logCannotResolveNonNullableTransientDependencies(firstDependentAction.getSession());
        final NonNullableTransientDependencies nonNullableTransientDependencies = dependenciesByAction.get(firstDependentAction);
        final Object firstTransientDependency = nonNullableTransientDependencies.getNonNullableTransientEntities().iterator().next();
        final String firstPropertyPath = nonNullableTransientDependencies.getNonNullableTransientPropertyPaths(firstTransientDependency).iterator().next();
        throw new TransientPropertyValueException("Not-null property references a transient value - transient instance must be saved beforeQuery current operation", firstDependentAction.getSession().guessEntityName(firstTransientDependency), firstDependentAction.getEntityName(), firstPropertyPath);
    }
}
Also used : NonNullableTransientDependencies(org.hibernate.engine.internal.NonNullableTransientDependencies) TransientPropertyValueException(org.hibernate.TransientPropertyValueException)

Example 3 with TransientPropertyValueException

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

the class MultiCircleJpaCascadeTest method testPersistNoCascadeToTransient.

@Test
public void testPersistNoCascadeToTransient() {
    skipCleanup = true;
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        em.persist(c);
        fail("should have failed.");
    } catch (IllegalStateException ex) {
        assertTrue(TransientPropertyValueException.class.isInstance(ex.getCause()));
        TransientPropertyValueException pve = (TransientPropertyValueException) ex.getCause();
        assertEquals(G.class.getName(), pve.getTransientEntityName());
        assertEquals(F.class.getName(), pve.getPropertyOwnerEntityName());
        assertEquals("g", pve.getPropertyName());
    }
    em.getTransaction().rollback();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) TransientPropertyValueException(org.hibernate.TransientPropertyValueException) Test(org.junit.Test)

Aggregations

TransientPropertyValueException (org.hibernate.TransientPropertyValueException)3 EntityManager (javax.persistence.EntityManager)2 Test (org.junit.Test)2 RollbackException (javax.persistence.RollbackException)1 NonNullableTransientDependencies (org.hibernate.engine.internal.NonNullableTransientDependencies)1