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