Search in sources :

Example 81 with PersistenceException

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

the class OptionalOneToOnePKJCTest method testNullBidirForeignIdGenerator.

@Test
@TestForIssue(jiraKey = "HHH-4982")
public void testNullBidirForeignIdGenerator() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Person person = new Person();
    person.setPersonAddress(null);
    try {
        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)

Example 82 with PersistenceException

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

the class UniqueConstraintTest method testUniquenessConstraintWithSuperclassProperty.

@Test
public void testUniquenessConstraintWithSuperclassProperty() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Room livingRoom = new Room();
    livingRoom.setId(1l);
    livingRoom.setName("livingRoom");
    s.persist(livingRoom);
    s.flush();
    House house = new House();
    house.setId(1l);
    house.setCost(100);
    house.setHeight(1000l);
    house.setRoom(livingRoom);
    s.persist(house);
    s.flush();
    House house2 = new House();
    house2.setId(2l);
    house2.setCost(100);
    house2.setHeight(1001l);
    house2.setRoom(livingRoom);
    s.persist(house2);
    try {
        s.flush();
        fail("Database constraint non-existant");
    } 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 83 with PersistenceException

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

the class SQLTest method test_sql_hibernate_multi_entity_query_example.

@Test
public void test_sql_hibernate_multi_entity_query_example() {
    try {
        doInJPA(this::entityManagerFactory, entityManager -> {
            Session session = entityManager.unwrap(Session.class);
            List<Object> entities = session.createSQLQuery("SELECT * " + "FROM Person pr, Partner pt " + "WHERE pr.name = pt.name").list();
            assertEquals(2, entities.size());
        });
        fail("Should throw NonUniqueDiscoveredSqlAliasException!");
    } catch (NonUniqueDiscoveredSqlAliasException e) {
    // expected
    } catch (PersistenceException e) {
        assertTyping(NonUniqueDiscoveredSqlAliasException.class, e.getCause());
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) NonUniqueDiscoveredSqlAliasException(org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 84 with PersistenceException

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

the class QueryTest method testFailingNativeQuery.

@Test
@TestForIssue(jiraKey = "HHH-10269")
public void testFailingNativeQuery() {
    final EntityManager entityManager = getOrCreateEntityManager();
    try {
        // Tests that Oracle does not run out of cursors.
        for (int i = 0; i < 1000; i++) {
            try {
                entityManager.createNativeQuery("Select 1 from NotExistedTable").getResultList();
                fail("expected PersistenceException");
            } catch (PersistenceException e) {
            // expected
            }
        }
    } finally {
        entityManager.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 85 with PersistenceException

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

the class OracleFollowOnLockingTest method testPessimisticLockWithGroupByWhileExplicitlyDisablingFollowOnLockingThenFails.

@Test
public void testPessimisticLockWithGroupByWhileExplicitlyDisablingFollowOnLockingThenFails() {
    final Session session = openSession();
    session.beginTransaction();
    sqlStatementInterceptor.getSqlQueries().clear();
    try {
        List<Object[]> products = session.createQuery("select count(p), p " + "from Product p " + "group by p.id, p.name ").setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setFollowOnLocking(false)).getResultList();
        fail("Should throw exception since Oracle does not support GROUP 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)

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