Search in sources :

Example 6 with PersistenceException

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

the class ExceptionTest method testConstraintViolationException.

@Test
public void testConstraintViolationException() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Music music = new Music();
    music.setName("Jazz");
    em.persist(music);
    Musician lui = new Musician();
    lui.setName("Lui Armstrong");
    lui.setFavouriteMusic(music);
    em.persist(lui);
    em.getTransaction().commit();
    try {
        em.getTransaction().begin();
        String hqlDelete = "delete Music where name = :name";
        em.createQuery(hqlDelete).setParameter("name", "Jazz").executeUpdate();
        em.getTransaction().commit();
        fail();
    } catch (PersistenceException e) {
        Throwable t = e.getCause();
        assertTrue("Should be a constraint violation", t instanceof ConstraintViolationException);
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) Test(org.junit.Test)

Example 7 with PersistenceException

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

the class OrmVersionTest method testInvalidOrm1.

@Test
public void testInvalidOrm1() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("invalid-orm1-test", "1.0").addMappingFileName("org/hibernate/jpa/test/jee/invalid-orm-1.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = null;
    try {
        emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
        Assert.fail("expecting 'invalid content' error");
    } catch (InvalidMappingException | AnnotationException expected) {
    // expected condition
    } catch (PersistenceException expected) {
    // expected condition
    } finally {
        if (emf != null) {
            emf.close();
        }
    }
}
Also used : InvalidMappingException(org.hibernate.InvalidMappingException) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) AnnotationException(org.hibernate.AnnotationException) Test(org.junit.Test)

Example 8 with PersistenceException

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

the class LockTest method testFindWithPessimisticWriteLockTimeoutException.

//5 minutes
@Test(timeout = 5 * 60 * 1000)
@TestForIssue(jiraKey = "HHH-7252")
@RequiresDialectFeature(value = DialectChecks.SupportsLockTimeouts.class, comment = "Test verifies proper exception throwing when a lock timeout is specified.", jiraKey = "HHH-7252")
public void testFindWithPessimisticWriteLockTimeoutException() {
    Lock lock = new Lock();
    lock.setName("name");
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(lock);
    em.getTransaction().commit();
    em.close();
    EntityManager em2 = createIsolatedEntityManager();
    em2.getTransaction().begin();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(AvailableSettings.LOCK_TIMEOUT, 0L);
    Lock lock2 = em2.find(Lock.class, lock.getId(), LockModeType.PESSIMISTIC_WRITE, properties);
    assertEquals("lock mode should be PESSIMISTIC_WRITE ", LockModeType.PESSIMISTIC_WRITE, em2.getLockMode(lock2));
    EntityManager em3 = createIsolatedEntityManager();
    em3.getTransaction().begin();
    try {
        em3.find(Lock.class, lock.getId(), LockModeType.PESSIMISTIC_WRITE, properties);
        fail("Exception should be thrown");
    } catch (LockTimeoutException lte) {
    // Proper exception thrown for dialect supporting lock timeouts when an immediate timeout is set.
    } catch (PessimisticLockException pe) {
        fail("Find with immediate timeout should have thrown LockTimeoutException.");
    } catch (PersistenceException pe) {
        log.info("EntityManager.find() for PESSIMISTIC_WRITE with timeout of 0 threw a PersistenceException.\n" + "This is likely a consequence of " + getDialect().getClass().getName() + " not properly mapping SQL errors into the correct HibernateException subtypes.\n" + "See HHH-7251 for an example of one such situation.", pe);
        fail("EntityManager should be throwing LockTimeoutException.");
    } finally {
        if (em3.getTransaction().getRollbackOnly()) {
            em3.getTransaction().rollback();
        } else {
            em3.getTransaction().commit();
        }
        em3.close();
    }
    em2.getTransaction().commit();
    em2.getTransaction().begin();
    em2.remove(lock2);
    em2.getTransaction().commit();
    em2.close();
}
Also used : EntityManager(javax.persistence.EntityManager) HashMap(java.util.HashMap) PersistenceException(javax.persistence.PersistenceException) LockTimeoutException(javax.persistence.LockTimeoutException) PessimisticLockException(javax.persistence.PessimisticLockException) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) TestForIssue(org.hibernate.testing.TestForIssue)

Example 9 with PersistenceException

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

the class PackagedEntityManagerTest method testExcludeHbmPar.

@Test
public void testExcludeHbmPar() throws Exception {
    File testPackage = buildExcludeHbmPar();
    addPackageToClasspath(testPackage);
    try {
        emf = Persistence.createEntityManagerFactory("excludehbmpar", new HashMap());
    } catch (PersistenceException e) {
        emf.close();
        Throwable nested = e.getCause();
        if (nested == null) {
            throw e;
        }
        nested = nested.getCause();
        if (nested == null) {
            throw e;
        }
        if (!(nested instanceof ClassNotFoundException)) {
            throw e;
        }
        fail("Try to process hbm file: " + e.getMessage());
    }
    EntityManager em = emf.createEntityManager();
    Caipirinha s = new Caipirinha("Strong");
    em.getTransaction().begin();
    em.persist(s);
    em.getTransaction().commit();
    em.getTransaction().begin();
    s = em.find(Caipirinha.class, s.getId());
    em.remove(s);
    em.getTransaction().commit();
    em.close();
    emf.close();
}
Also used : EntityManager(javax.persistence.EntityManager) HashMap(java.util.HashMap) PersistenceException(javax.persistence.PersistenceException) Caipirinha(org.hibernate.jpa.test.pack.excludehbmpar.Caipirinha) File(java.io.File) Test(org.junit.Test)

Example 10 with PersistenceException

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

the class ImmutableTest method testImmutableCollection.

@Test
public void testImmutableCollection() {
    Country country = new Country();
    country.setName("Germany");
    List states = new ArrayList<State>();
    State bayern = new State();
    bayern.setName("Bayern");
    State hessen = new State();
    hessen.setName("Hessen");
    State sachsen = new State();
    sachsen.setName("Sachsen");
    states.add(bayern);
    states.add(hessen);
    states.add(sachsen);
    country.setStates(states);
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(country);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    Country germany = (Country) s.get(Country.class, country.getId());
    assertNotNull(germany);
    assertEquals("Wrong number of states", 3, germany.getStates().size());
    // try adding a state
    State foobar = new State();
    foobar.setName("foobar");
    s.save(foobar);
    germany.getStates().add(foobar);
    try {
        tx.commit();
        fail();
    } catch (PersistenceException ex) {
        // expected
        assertTrue(ex.getMessage().contains("changed an immutable collection instance"));
        log.debug("success");
    }
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    germany = (Country) s.get(Country.class, country.getId());
    assertNotNull(germany);
    assertEquals("Wrong number of states", 3, germany.getStates().size());
    // try deleting a state
    germany.getStates().remove(0);
    try {
        tx.commit();
        fail();
    } catch (PersistenceException e) {
        assertTrue(e.getMessage().contains("changed an immutable collection instance"));
        log.debug("success");
    }
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    germany = (Country) s.get(Country.class, country.getId());
    assertNotNull(germany);
    assertEquals("Wrong number of states", 3, germany.getStates().size());
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) PersistenceException(javax.persistence.PersistenceException) ArrayList(java.util.ArrayList) List(java.util.List) 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