Search in sources :

Example 46 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class ImmutableTest method testImmutableCollectionWithUpdate.

@Test
public void testImmutableCollectionWithUpdate() {
    clearCounts();
    Contract c = new Contract(null, "gavin", "phone");
    ContractVariation cv1 = new ContractVariation(1, c);
    cv1.setText("expensive");
    ContractVariation cv2 = new ContractVariation(2, c);
    cv2.setText("more expensive");
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.persist(c);
    t.commit();
    s.close();
    assertInsertCount(3);
    assertUpdateCount(0);
    s = openSession();
    t = s.beginTransaction();
    c.getVariations().add(new ContractVariation(3, c));
    s.update(c);
    try {
        t.commit();
        fail("should have failed because reassociated object has a dirty collection");
    } catch (PersistenceException ex) {
    // expected
    } finally {
        t.rollback();
        s.close();
    }
    assertUpdateCount(0);
    s = openSession();
    t = s.beginTransaction();
    c = (Contract) s.createCriteria(Contract.class).uniqueResult();
    assertEquals(c.getCustomerName(), "gavin");
    assertEquals(c.getVariations().size(), 2);
    Iterator it = c.getVariations().iterator();
    cv1 = (ContractVariation) it.next();
    assertEquals(cv1.getText(), "expensive");
    cv2 = (ContractVariation) it.next();
    assertEquals(cv2.getText(), "more expensive");
    s.delete(c);
    assertEquals(s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult(), new Long(0));
    assertEquals(s.createCriteria(ContractVariation.class).setProjection(Projections.rowCount()).uniqueResult(), new Long(0));
    t.commit();
    s.close();
    assertUpdateCount(0);
    assertDeleteCount(3);
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) Iterator(java.util.Iterator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 47 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class AbstractEntityWithManyToManyTest method testManyToManyCollectionOptimisticLockingWithUpdate.

@Test
public void testManyToManyCollectionOptimisticLockingWithUpdate() {
    clearCounts();
    Plan pOrig = new Plan("plan");
    Contract cOrig = new Contract(null, "gail", "phone");
    pOrig.addContract(cOrig);
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.persist(pOrig);
    t.commit();
    s.close();
    assertInsertCount(2);
    assertUpdateCount(0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    Plan p = (Plan) s.get(Plan.class, pOrig.getId());
    Contract newC = new Contract(null, "yogi", "pawprint");
    p.addContract(newC);
    t.commit();
    s.close();
    assertInsertCount(1);
    assertUpdateCount(isContractVersioned ? 1 : 0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    pOrig.removeContract(cOrig);
    s.update(pOrig);
    try {
        t.commit();
        assertFalse(isContractVersioned);
    } catch (PersistenceException ex) {
        t.rollback();
        assertTrue(isContractVersioned);
        if (!sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData()) {
            assertTyping(StaleObjectStateException.class, ex.getCause());
        } else {
            assertTyping(StaleStateException.class, ex.getCause());
        }
    }
    s.close();
    s = openSession();
    t = s.beginTransaction();
    p = (Plan) s.createCriteria(Plan.class).uniqueResult();
    s.delete(p);
    s.createQuery("delete from Contract").executeUpdate();
    assertEquals(new Long(0), s.createCriteria(Plan.class).setProjection(Projections.rowCount()).uniqueResult());
    assertEquals(new Long(0), s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult());
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) StaleStateException(org.hibernate.StaleStateException) PersistenceException(javax.persistence.PersistenceException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 48 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class AbstractEntityWithManyToManyTest method testManyToManyCollectionOptimisticLockingWithMerge.

@Test
public void testManyToManyCollectionOptimisticLockingWithMerge() {
    clearCounts();
    Plan pOrig = new Plan("plan");
    Contract cOrig = new Contract(null, "gail", "phone");
    pOrig.addContract(cOrig);
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.persist(pOrig);
    t.commit();
    s.close();
    assertInsertCount(2);
    assertUpdateCount(0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    Plan p = (Plan) s.get(Plan.class, pOrig.getId());
    Contract newC = new Contract(null, "sherman", "note");
    p.addContract(newC);
    t.commit();
    s.close();
    assertInsertCount(1);
    assertUpdateCount(isContractVersioned ? 1 : 0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    pOrig.removeContract(cOrig);
    try {
        s.merge(pOrig);
        assertFalse(isContractVersioned);
    } catch (PersistenceException ex) {
        assertTyping(StaleObjectStateException.class, ex.getCause());
        assertTrue(isContractVersioned);
    } finally {
        t.rollback();
    }
    s.close();
    s = openSession();
    t = s.beginTransaction();
    p = (Plan) s.createCriteria(Plan.class).uniqueResult();
    s.delete(p);
    assertEquals(new Long(0), s.createCriteria(Plan.class).setProjection(Projections.rowCount()).uniqueResult());
    assertEquals(new Long(0), s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult());
    t.commit();
    s.close();
    assertUpdateCount(0);
    assertDeleteCount(3);
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 49 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class AbstractEntityWithOneToManyTest method testOneToManyCollectionOptimisticLockingWithUpdate.

@Test
public void testOneToManyCollectionOptimisticLockingWithUpdate() {
    clearCounts();
    Contract cOrig = new Contract(null, "gail", "phone");
    Party partyOrig = new Party("party");
    cOrig.addParty(partyOrig);
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.persist(cOrig);
    t.commit();
    s.close();
    assertInsertCount(2);
    assertUpdateCount(0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    Contract c = (Contract) s.get(Contract.class, cOrig.getId());
    Party newParty = new Party("new party");
    c.addParty(newParty);
    t.commit();
    s.close();
    assertInsertCount(1);
    assertUpdateCount(isContractVersioned ? 1 : 0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    cOrig.removeParty(partyOrig);
    s.update(cOrig);
    try {
        t.commit();
        assertFalse(isContractVersioned);
    } catch (PersistenceException ex) {
        t.rollback();
        assertTrue(isContractVersioned);
        if (!sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData()) {
            assertTyping(StaleObjectStateException.class, ex.getCause());
        } else {
            assertTyping(StaleStateException.class, ex.getCause());
        }
    }
    s.close();
    s = openSession();
    t = s.beginTransaction();
    c = (Contract) s.createCriteria(Contract.class).uniqueResult();
    s.createQuery("delete from Party").executeUpdate();
    s.delete(c);
    assertEquals(Long.valueOf(0), s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult());
    assertEquals(Long.valueOf(0), s.createCriteria(Party.class).setProjection(Projections.rowCount()).uniqueResult());
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) StaleStateException(org.hibernate.StaleStateException) PersistenceException(javax.persistence.PersistenceException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 50 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class InterceptorTest method testTimeout.

/**
	 * Test that setting a transaction timeout will cause an Exception to occur
	 * if the transaction timeout is exceeded.
	 */
@Test
public void testTimeout() throws Exception {
    final int TIMEOUT = 2;
    final int WAIT = TIMEOUT + 1;
    Session s = openSession();
    // Get the transaction and set the timeout BEFORE calling begin()
    Transaction t = s.getTransaction();
    t.setTimeout(TIMEOUT);
    t.begin();
    // Sleep for an amount of time that exceeds the transaction timeout
    Thread.sleep(WAIT * 1000);
    try {
        // Do something with the transaction and try to commit it
        s.persist(new User("john", "test"));
        t.commit();
        fail("Transaction should have timed out");
    } catch (PersistenceException e) {
        assertTyping(TransactionException.class, e.getCause());
        assertTrue("Transaction failed for the wrong reason.  Expecting transaction timeout, but found [" + e.getCause().getMessage() + "]", e.getCause().getMessage().contains("transaction timeout expired"));
    }
}
Also used : TransactionException(org.hibernate.TransactionException) Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

PersistenceException (javax.persistence.PersistenceException)125 Test (org.junit.Test)66 Session (org.hibernate.Session)50 Transaction (org.hibernate.Transaction)29 EntityManager (javax.persistence.EntityManager)17 IOException (java.io.IOException)12 StaleObjectStateException (org.hibernate.StaleObjectStateException)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)9 SQLGrammarException (org.hibernate.exception.SQLGrammarException)8 TransactionRequiredException (javax.persistence.TransactionRequiredException)7 MessagesEvent (com.openmeap.event.MessagesEvent)5 EntityNotFoundException (javax.persistence.EntityNotFoundException)5 OptimisticLockException (javax.persistence.OptimisticLockException)5 TestForIssue (org.hibernate.testing.TestForIssue)5 GlobalSettings (com.openmeap.model.dto.GlobalSettings)4 NoResultException (javax.persistence.NoResultException)4 LockOptions (org.hibernate.LockOptions)4 StaleStateException (org.hibernate.StaleStateException)4