Search in sources :

Example 36 with PersistenceException

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

the class PersistTest method testCreateExceptionWithGeneratedId.

@Test
public void testCreateExceptionWithGeneratedId() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    NumberedNode dupe = new NumberedNode("dupe");
    em.persist(dupe);
    em.persist(dupe);
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        em.persist(dupe);
        fail();
    } catch (PersistenceException poe) {
    // verify that an exception is thrown!
    }
    em.getTransaction().rollback();
    em.close();
    NumberedNode nondupe = new NumberedNode("nondupe");
    nondupe.addChild(dupe);
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        em.persist(nondupe);
        fail();
    } catch (PersistenceException poe) {
    // verify that an exception is thrown!
    }
    em.getTransaction().rollback();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Example 37 with PersistenceException

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

the class TestConnectionPool method testConnectionPoolDoesNotConsumeAllConnections.

@Test
public void testConnectionPoolDoesNotConsumeAllConnections() {
    for (int i = 0; i < CONNECTION_POOL_SIZE + 1; ++i) {
        EntityManager entityManager = getOrCreateEntityManager();
        try {
            for (int j = 0; j < 2; j++) {
                try {
                    final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
                    final CriteriaQuery<TestEntity> criteriaQuery = builder.createQuery(TestEntity.class);
                    criteriaQuery.select(criteriaQuery.from(TestEntity.class));
                    entityManager.createQuery(criteriaQuery).getResultList();
                } catch (PersistenceException e) {
                    if (e.getCause() instanceof SQLGrammarException) {
                    // expected, the schema was not created
                    } else {
                        throw e;
                    }
                }
            }
        } finally {
            entityManager.close();
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) SQLGrammarException(org.hibernate.exception.SQLGrammarException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Example 38 with PersistenceException

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

the class TransactionJoiningTest method testMultiThreadTransactionTimeout.

/**
 * In certain JTA environments (JBossTM, etc.), a background thread (reaper)
 * can rollback a transaction if it times out.  These timeouts are rare and
 * typically come from server failures.  However, we need to handle the
 * multi-threaded nature of the transaction afterCompletion action.
 * Emulate a timeout with a simple afterCompletion call in a thread.
 * See HHH-7910
 */
@Test
@TestForIssue(jiraKey = "HHH-7910")
public void testMultiThreadTransactionTimeout() throws Exception {
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager em = entityManagerFactory().createEntityManager();
    final SessionImpl sImpl = em.unwrap(SessionImpl.class);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread thread = new Thread() {

        public void run() {
            ((JtaTransactionCoordinatorImpl) sImpl.getTransactionCoordinator()).getSynchronizationCallbackCoordinator().afterCompletion(Status.STATUS_ROLLEDBACK);
            latch.countDown();
        }
    };
    thread.start();
    latch.await();
    boolean caught = false;
    try {
        em.persist(new Book("The Book of Foo", 1));
    } catch (PersistenceException e) {
        caught = e.getCause().getClass().equals(HibernateException.class);
    }
    assertTrue(caught);
    // Ensure that the connection was closed by the background thread.
    caught = false;
    try {
        em.createQuery("from Book").getResultList();
    } catch (PersistenceException e) {
        // HHH-9312
        caught = true;
    } catch (Exception e) {
        caught = true;
    }
    assertTrue(caught);
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) SessionImpl(org.hibernate.internal.SessionImpl) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionRequiredException(javax.persistence.TransactionRequiredException) PersistenceException(javax.persistence.PersistenceException) HibernateException(org.hibernate.HibernateException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 39 with PersistenceException

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

the class EmbeddableIntegratorTest method testWithoutIntegrator.

/**
 * Throws a mapping exception because DollarValue is not mapped
 */
@Test
public void testWithoutIntegrator() {
    SessionFactory sf = new Configuration().addAnnotatedClass(Investor.class).setProperty("hibernate.hbm2ddl.auto", "create-drop").buildSessionFactory();
    try {
        Session sess = sf.openSession();
        try {
            sess.getTransaction().begin();
            Investor myInv = getInvestor();
            myInv.setId(1L);
            sess.save(myInv);
            sess.flush();
            fail("A JDBCException expected");
            sess.clear();
            Investor inv = (Investor) sess.get(Investor.class, 1L);
            assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount());
        } catch (PersistenceException e) {
            assertTyping(JDBCException.class, e.getCause());
            sess.getTransaction().rollback();
        }
        sess.close();
    } finally {
        sf.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) JDBCException(org.hibernate.JDBCException) PersistenceException(javax.persistence.PersistenceException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 40 with PersistenceException

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

the class AssociationOverrideTest method testOverriding.

@Test
public void testOverriding() throws Exception {
    Location paris = new Location();
    paris.setName("Paris");
    Location atlanta = new Location();
    atlanta.setName("Atlanta");
    Trip trip = new Trip();
    trip.setFrom(paris);
    // trip.setTo( atlanta );
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(paris);
    s.persist(atlanta);
    try {
        s.persist(trip);
        s.flush();
        fail("Should be non nullable");
    } catch (PersistenceException e) {
    // success
    } finally {
        tx.rollback();
        s.close();
    }
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) 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