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();
}
}
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();
}
}
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());
}
}
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();
}
}
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());
}
}
Aggregations