Search in sources :

Example 96 with PersistenceException

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

the class EntityTest method testUniqueConstraint.

@Test
public void testUniqueConstraint() throws Exception {
    int id = 5;
    Session s;
    Transaction tx;
    s = openSession();
    tx = s.beginTransaction();
    Sky sky = new Sky();
    sky.id = Long.valueOf(id++);
    sky.color = "green";
    sky.day = "monday";
    sky.month = "March";
    Sky otherSky = new Sky();
    otherSky.id = Long.valueOf(id++);
    otherSky.color = "red";
    otherSky.day = "friday";
    otherSky.month = "March";
    Sky sameSky = new Sky();
    sameSky.id = Long.valueOf(id++);
    sameSky.color = "green";
    sameSky.day = "monday";
    sameSky.month = "March";
    s.save(sky);
    s.flush();
    s.save(otherSky);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    try {
        s.save(sameSky);
        tx.commit();
        fail("unique constraints not respected");
    } catch (PersistenceException e) {
        //success
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        s.close();
    }
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 97 with PersistenceException

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

the class EntityTest method testVersion.

@Test
public void testVersion() throws Exception {
    //		put an object in DB
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Flight firstOne = new Flight();
    firstOne.setId(Long.valueOf(2));
    firstOne.setName("AF3202");
    firstOne.setDuration(Long.valueOf(500));
    s.save(firstOne);
    s.flush();
    tx.commit();
    s.close();
    //read it
    s = openSession();
    tx = s.beginTransaction();
    firstOne = (Flight) s.get(Flight.class, Long.valueOf(2));
    tx.commit();
    s.close();
    //read it again
    s = openSession();
    tx = s.beginTransaction();
    Flight concurrentOne = (Flight) s.get(Flight.class, Long.valueOf(2));
    concurrentOne.setDuration(Long.valueOf(1000));
    s.update(concurrentOne);
    tx.commit();
    s.close();
    assertFalse(firstOne == concurrentOne);
    assertFalse(firstOne.getVersion().equals(concurrentOne.getVersion()));
    //reattach the first one
    s = openSession();
    tx = s.beginTransaction();
    firstOne.setName("Second access");
    s.update(firstOne);
    try {
        tx.commit();
        fail("Optimistic locking should work");
    } catch (PersistenceException expected) {
        if (expected.getCause() instanceof StaleStateException) {
        //expected
        } else {
            fail("StaleStateException expected but is " + expected.getCause());
        }
    } finally {
        if (tx != null) {
            tx.rollback();
        }
        s.close();
    }
}
Also used : Transaction(org.hibernate.Transaction) StaleStateException(org.hibernate.StaleStateException) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 98 with PersistenceException

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

the class FlushAndTransactionTest method testRollbackOnlyOnPersistenceException.

@Test
public void testRollbackOnlyOnPersistenceException() throws Exception {
    Book book = new Book();
    book.name = "Stolen keys";
    //new Integer( 50 );
    book.id = null;
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        em.persist(book);
        em.flush();
        em.clear();
        book.setName("kitty kid");
        em.merge(book);
        em.flush();
        em.clear();
        //non updated version
        book.setName("kitty kid2");
        em.merge(book);
        em.flush();
        fail("optimistic locking exception");
    } catch (PersistenceException e) {
    //success
    }
    try {
        em.getTransaction().commit();
        fail("Commit should be rollbacked");
    } catch (RollbackException e) {
    //success
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) Test(org.junit.Test)

Example 99 with PersistenceException

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

the class JoinTest method testUniqueConstaintOnSecondaryTable.

@Test
public void testUniqueConstaintOnSecondaryTable() throws Exception {
    Cat cat = new Cat();
    cat.setStoryPart2("My long story");
    Cat cat2 = new Cat();
    cat2.setStoryPart2("My long story");
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    try {
        s.persist(cat);
        s.persist(cat2);
        tx.commit();
        fail("unique constraints violation on secondary table");
    } catch (PersistenceException e) {
        try {
            assertTyping(ConstraintViolationException.class, e.getCause());
        //success
        } finally {
            tx.rollback();
        }
    } finally {
        s.close();
    }
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 100 with PersistenceException

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

the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedToNonInverseCollection.

@Test
@SuppressWarnings({ "unchecked" })
public void testPersistOutsideTransactionCascadedToNonInverseCollection() {
    long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
    Session s = openSession();
    try {
        MyEntity myEntity = new MyEntity("test-persist");
        myEntity.getNonInverseChildren().add(new MyChild("test-child-persist-non-inverse"));
        s.persist(myEntity);
        assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
        assertNull(myEntity.getId());
        s.flush();
        fail("TransactionRequiredException required upon flush");
    } catch (PersistenceException ex) {
        // expected
        assertTyping(TransactionRequiredException.class, ex);
    } finally {
        s.close();
    }
}
Also used : TransactionRequiredException(javax.persistence.TransactionRequiredException) 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