Search in sources :

Example 86 with PersistenceException

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

the class OracleFollowOnLockingTest method testPessimisticLockWithUnionWhileExplicitlyDisablingFollowOnLockingThenFails.

@Test
public void testPessimisticLockWithUnionWhileExplicitlyDisablingFollowOnLockingThenFails() {
    final Session session = openSession();
    session.beginTransaction();
    sqlStatementInterceptor.getSqlQueries().clear();
    try {
        List<Vehicle> vehicles = session.createQuery("select v from Vehicle v").setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setFollowOnLocking(false)).getResultList();
        fail("Should throw exception since Oracle does not support UNION 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 87 with PersistenceException

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

the class OracleFollowOnLockingTest method testPessimisticLockWithFirstResultsWhileExplicitlyDisablingFollowOnLockingThenFails.

@Test
public void testPessimisticLockWithFirstResultsWhileExplicitlyDisablingFollowOnLockingThenFails() {
    final Session session = openSession();
    session.beginTransaction();
    sqlStatementInterceptor.getSqlQueries().clear();
    try {
        List<Product> products = session.createQuery("select p from Product p", Product.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setFollowOnLocking(false)).setFirstResult(40).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 88 with PersistenceException

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

the class OracleFollowOnLockingTest method testPessimisticLockWithDistinctWhileExplicitlyDisablingFollowOnLockingThenFails.

@Test
public void testPessimisticLockWithDistinctWhileExplicitlyDisablingFollowOnLockingThenFails() {
    final Session session = openSession();
    session.beginTransaction();
    sqlStatementInterceptor.getSqlQueries().clear();
    try {
        List<Product> products = session.createQuery("select distinct p from Product p where p.id > 40", Product.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setFollowOnLocking(false)).getResultList();
        fail("Should throw exception since Oracle does not support DISTINCT 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 89 with PersistenceException

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

the class ImmutableTest method testImmutableCollectionWithMerge.

@Test
public void testImmutableCollectionWithMerge() {
    clearCounts();
    Contract c = new Contract(null, "gavin", "phone");
    ContractVariation cv1 = new ContractVariation(1, c);
    cv1.setText("expensive");
    ContractVariation cv2 = new ContractVariation(2, c);
    cv2.setText("more expensive");
    Session s = openSession();
    Transaction t = s.beginTransaction();
    s.persist(c);
    t.commit();
    s.close();
    assertInsertCount(3);
    assertUpdateCount(0);
    clearCounts();
    s = openSession();
    t = s.beginTransaction();
    c.getVariations().add(new ContractVariation(3, c));
    s.merge(c);
    try {
        t.commit();
        fail("should have failed because an immutable collection was changed");
    } catch (PersistenceException ex) {
        // expected
        t.rollback();
    } finally {
        s.close();
    }
    s = openSession();
    t = s.beginTransaction();
    c = (Contract) s.createCriteria(Contract.class).uniqueResult();
    assertEquals(c.getCustomerName(), "gavin");
    assertEquals(c.getVariations().size(), 2);
    Iterator it = c.getVariations().iterator();
    cv1 = (ContractVariation) it.next();
    assertEquals(cv1.getText(), "expensive");
    cv2 = (ContractVariation) it.next();
    assertEquals(cv2.getText(), "more expensive");
    s.delete(c);
    assertEquals(s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult(), new Long(0));
    assertEquals(s.createCriteria(ContractVariation.class).setProjection(Projections.rowCount()).uniqueResult(), new Long(0));
    t.commit();
    s.close();
    assertUpdateCount(0);
    assertDeleteCount(3);
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) Iterator(java.util.Iterator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 90 with PersistenceException

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

the class ImmutableNaturalIdTest method testNaturalIdCheck.

@Test
public void testNaturalIdCheck() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    User u = new User("steve", "superSecret");
    s.persist(u);
    u.setUserName("Steve");
    try {
        s.flush();
        fail();
    } catch (PersistenceException e) {
        //expected
        t.rollback();
    }
    u.setUserName("steve");
    s.delete(u);
    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)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