Search in sources :

Example 11 with PersistenceException

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

the class OptionalOneToOneMappedByTest method testBidirForeignIdGenerator.

// @OneToOne(mappedBy="address") with foreign generator
@Test
public void testBidirForeignIdGenerator() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    OwnerAddress address = new OwnerAddress();
    address.setOwner(null);
    try {
        s.persist(address);
        s.flush();
        fail("should have failed with 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)

Example 12 with PersistenceException

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

the class OracleFollowOnLockingTest method testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyDisablingFollowOnLockingThenFails.

@Test
public void testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyDisablingFollowOnLockingThenFails() {
    final Session session = openSession();
    session.beginTransaction();
    sqlStatementInterceptor.getSqlQueries().clear();
    try {
        List<Product> products = session.createQuery("select p from Product p order by p.id", Product.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setFollowOnLocking(false)).setMaxResults(10).getResultList();
        fail("Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled");
    } catch (PersistenceException expected) {
        assertEquals(SQLGrammarException.class, expected.getCause().getClass());
    }
}
Also used : LockOptions(org.hibernate.LockOptions) SQLGrammarException(org.hibernate.exception.SQLGrammarException) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 13 with PersistenceException

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

the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedToManyToOne.

@Test
public void testPersistOutsideTransactionCascadedToManyToOne() {
    long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
    Session s = openSession();
    try {
        MyEntity myEntity = new MyEntity("test-persist");
        myEntity.setSibling(new MySibling("test-persist-sibling-out"));
        s.persist(myEntity);
        assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
        assertNull(myEntity.getId());
        s.flush();
        fail("TransactionRequiredException expected 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)

Example 14 with PersistenceException

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

the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedToInverseCollection.

@Test
@SuppressWarnings({ "unchecked" })
public void testPersistOutsideTransactionCascadedToInverseCollection() {
    long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
    Session s = openSession();
    try {
        MyEntity myEntity2 = new MyEntity("test-persist-2");
        MyChild child = new MyChild("test-child-persist-inverse");
        myEntity2.getInverseChildren().add(child);
        child.setInverseParent(myEntity2);
        s.persist(myEntity2);
        assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
        assertNull(myEntity2.getId());
        s.flush();
        fail("TransactionRequiredException expected 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)

Example 15 with PersistenceException

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

the class IdentityGeneratedKeysTest method testPersistOutsideTransaction.

@Test
public void testPersistOutsideTransaction() {
    Session s = openSession();
    try {
        // first test save() which should force an immediate insert...
        MyEntity myEntity1 = new MyEntity("test-save");
        Long id = (Long) s.save(myEntity1);
        assertNotNull("identity column did not force immediate insert", id);
        assertEquals(id, myEntity1.getId());
        // next test persist() which should cause a delayed insert...
        long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
        MyEntity myEntity2 = new MyEntity("test-persist");
        s.persist(myEntity2);
        assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
        assertNull(myEntity2.getId());
        // an explicit flush should cause execution of the delayed insertion
        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)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