Search in sources :

Example 76 with PersistenceException

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

the class EntityManagerTest method testEntityNotFoundException.

@Test
public void testEntityNotFoundException() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Wallet w = new Wallet();
    w.setBrand("Lacoste");
    w.setModel("Minimic");
    w.setSerial("0324");
    em.persist(w);
    Wallet wallet = em.find(Wallet.class, w.getSerial());
    em.createNativeQuery("delete from Wallet").executeUpdate();
    try {
        em.refresh(wallet);
    } catch (EntityNotFoundException enfe) {
        // success
        if (em.getTransaction() != null) {
            em.getTransaction().rollback();
        }
        em.close();
        return;
    }
    try {
        em.getTransaction().commit();
        fail("Should have raised an EntityNotFoundException");
    } catch (PersistenceException pe) {
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) EntityNotFoundException(javax.persistence.EntityNotFoundException) Test(org.junit.Test)

Example 77 with PersistenceException

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

the class DDLWithoutCallbackTest method assertDatabaseConstraintViolationThrown.

private void assertDatabaseConstraintViolationThrown(Object o) {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    try {
        s.persist(o);
        s.flush();
        fail("expecting SQL constraint violation");
    } catch (PersistenceException pe) {
        final Throwable cause = pe.getCause();
        if (cause instanceof ConstraintViolationException) {
            fail("invalid object should not be validated");
        } else if (cause instanceof org.hibernate.exception.ConstraintViolationException) {
            if (getDialect().supportsColumnCheck()) {
            // expected
            } else {
                org.hibernate.exception.ConstraintViolationException cve = (org.hibernate.exception.ConstraintViolationException) cause;
                fail("Unexpected SQL constraint violation [" + cve.getConstraintName() + "] : " + cve.getSQLException());
            }
        }
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session)

Example 78 with PersistenceException

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

the class TablePerClassTest method testConstraintsOnSuperclassProperties.

@Test
public void testConstraintsOnSuperclassProperties() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Product product1 = new Product();
    product1.setId(1l);
    product1.setManufacturerId(1l);
    product1.setManufacturerPartNumber("AAFR");
    s.persist(product1);
    s.flush();
    Product product2 = new Product();
    product2.setId(2l);
    product2.setManufacturerId(1l);
    product2.setManufacturerPartNumber("AAFR");
    s.persist(product2);
    try {
        s.flush();
        fail("Database Exception not handled");
    } catch (PersistenceException e) {
        assertTyping(JDBCException.class, e.getCause());
    //success
    } finally {
        tx.rollback();
        s.close();
    }
}
Also used : Transaction(org.hibernate.Transaction) JDBCException(org.hibernate.JDBCException) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 79 with PersistenceException

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

the class OneToOneJoinTableTest method storeNonUniqueRelationship.

@Test
public void storeNonUniqueRelationship() throws Throwable {
    Session session = null;
    try {
        session = openSession();
        Transaction tx = session.beginTransaction();
        Item someItem = new Item("Some Item");
        session.save(someItem);
        Shipment shipment1 = new Shipment(someItem);
        session.save(shipment1);
        Shipment shipment2 = new Shipment(someItem);
        session.save(shipment2);
        tx.commit();
        fail();
    } catch (PersistenceException e) {
        assertTyping(ConstraintViolationException.class, e.getCause());
    // expected
    } finally {
        if (session != null) {
            session.getTransaction().rollback();
            session.close();
        }
        cleanUpData();
    }
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 80 with PersistenceException

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

the class OptionalOneToOnePKJCTest method testNotFoundBidirForeignIdGenerator.

@Test
@TestForIssue(jiraKey = "HHH-4982")
public void testNotFoundBidirForeignIdGenerator() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Person person = new Person();
    person.setPersonAddress(null);
    person.setId(1);
    try {
        // Hibernate resets the ID to null beforeQuery executing the foreign generator
        s.persist(person);
        s.flush();
        fail("should have thrown IdentifierGenerationException.");
    } catch (PersistenceException ex) {
        assertTyping(IdentifierGenerationException.class, ex.getCause());
    // expected
    } finally {
        tx.rollback();
        s.close();
    }
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) IdentifierGenerationException(org.hibernate.id.IdentifierGenerationException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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