Search in sources :

Example 16 with PersistenceException

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

the class AbstractEntityWithOneToManyTest method testOneToManyCollectionOptimisticLockingWithMerge.

@Test
public void testOneToManyCollectionOptimisticLockingWithMerge() {
    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);
    try {
        s.merge(cOrig);
        assertFalse(isContractVersioned);
    } catch (PersistenceException ex) {
        assertTyping(StaleObjectStateException.class, ex.getCause());
        assertTrue(isContractVersioned);
    } finally {
        t.rollback();
    }
    s.close();
    s = openSession();
    t = s.beginTransaction();
    c = (Contract) s.createCriteria(Contract.class).uniqueResult();
    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();
    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 17 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 18 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 19 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 20 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)

Aggregations

PersistenceException (javax.persistence.PersistenceException)191 Test (org.junit.Test)73 Session (org.hibernate.Session)57 EntityManager (javax.persistence.EntityManager)39 Transaction (org.hibernate.Transaction)34 EntityTransaction (javax.persistence.EntityTransaction)21 IOException (java.io.IOException)18 Query (javax.persistence.Query)18 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)18 List (java.util.List)16 ArrayList (java.util.ArrayList)14 TypedQuery (javax.persistence.TypedQuery)11 JPAQuery (org.datanucleus.api.jpa.JPAQuery)10 StaleObjectStateException (org.hibernate.StaleObjectStateException)10 SQLGrammarException (org.hibernate.exception.SQLGrammarException)8 HashMap (java.util.HashMap)6 Iterator (java.util.Iterator)6 Person (org.datanucleus.samples.annotations.models.company.Person)6 MessagesEvent (com.openmeap.event.MessagesEvent)5 SQLException (java.sql.SQLException)5