use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class ExceptionTest method testConstraintViolationException.
@Test
public void testConstraintViolationException() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Music music = new Music();
music.setName("Jazz");
em.persist(music);
Musician lui = new Musician();
lui.setName("Lui Armstrong");
lui.setFavouriteMusic(music);
em.persist(lui);
em.getTransaction().commit();
try {
em.getTransaction().begin();
String hqlDelete = "delete Music where name = :name";
em.createQuery(hqlDelete).setParameter("name", "Jazz").executeUpdate();
em.getTransaction().commit();
fail();
} catch (PersistenceException e) {
Throwable t = e.getCause();
assertTrue("Should be a constraint violation", t instanceof ConstraintViolationException);
em.getTransaction().rollback();
} finally {
em.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class OrmVersionTest method testInvalidOrm1.
@Test
public void testInvalidOrm1() {
PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("invalid-orm1-test", "1.0").addMappingFileName("org/hibernate/jpa/test/jee/invalid-orm-1.xml");
HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
EntityManagerFactory emf = null;
try {
emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
Assert.fail("expecting 'invalid content' error");
} catch (InvalidMappingException | AnnotationException expected) {
// expected condition
} catch (PersistenceException expected) {
// expected condition
} finally {
if (emf != null) {
emf.close();
}
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class LockTest method testFindWithPessimisticWriteLockTimeoutException.
//5 minutes
@Test(timeout = 5 * 60 * 1000)
@TestForIssue(jiraKey = "HHH-7252")
@RequiresDialectFeature(value = DialectChecks.SupportsLockTimeouts.class, comment = "Test verifies proper exception throwing when a lock timeout is specified.", jiraKey = "HHH-7252")
public void testFindWithPessimisticWriteLockTimeoutException() {
Lock lock = new Lock();
lock.setName("name");
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(lock);
em.getTransaction().commit();
em.close();
EntityManager em2 = createIsolatedEntityManager();
em2.getTransaction().begin();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(AvailableSettings.LOCK_TIMEOUT, 0L);
Lock lock2 = em2.find(Lock.class, lock.getId(), LockModeType.PESSIMISTIC_WRITE, properties);
assertEquals("lock mode should be PESSIMISTIC_WRITE ", LockModeType.PESSIMISTIC_WRITE, em2.getLockMode(lock2));
EntityManager em3 = createIsolatedEntityManager();
em3.getTransaction().begin();
try {
em3.find(Lock.class, lock.getId(), LockModeType.PESSIMISTIC_WRITE, properties);
fail("Exception should be thrown");
} catch (LockTimeoutException lte) {
// Proper exception thrown for dialect supporting lock timeouts when an immediate timeout is set.
} catch (PessimisticLockException pe) {
fail("Find with immediate timeout should have thrown LockTimeoutException.");
} catch (PersistenceException pe) {
log.info("EntityManager.find() for PESSIMISTIC_WRITE with timeout of 0 threw a PersistenceException.\n" + "This is likely a consequence of " + getDialect().getClass().getName() + " not properly mapping SQL errors into the correct HibernateException subtypes.\n" + "See HHH-7251 for an example of one such situation.", pe);
fail("EntityManager should be throwing LockTimeoutException.");
} finally {
if (em3.getTransaction().getRollbackOnly()) {
em3.getTransaction().rollback();
} else {
em3.getTransaction().commit();
}
em3.close();
}
em2.getTransaction().commit();
em2.getTransaction().begin();
em2.remove(lock2);
em2.getTransaction().commit();
em2.close();
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class PackagedEntityManagerTest method testExcludeHbmPar.
@Test
public void testExcludeHbmPar() throws Exception {
File testPackage = buildExcludeHbmPar();
addPackageToClasspath(testPackage);
try {
emf = Persistence.createEntityManagerFactory("excludehbmpar", new HashMap());
} catch (PersistenceException e) {
emf.close();
Throwable nested = e.getCause();
if (nested == null) {
throw e;
}
nested = nested.getCause();
if (nested == null) {
throw e;
}
if (!(nested instanceof ClassNotFoundException)) {
throw e;
}
fail("Try to process hbm file: " + e.getMessage());
}
EntityManager em = emf.createEntityManager();
Caipirinha s = new Caipirinha("Strong");
em.getTransaction().begin();
em.persist(s);
em.getTransaction().commit();
em.getTransaction().begin();
s = em.find(Caipirinha.class, s.getId());
em.remove(s);
em.getTransaction().commit();
em.close();
emf.close();
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class ImmutableTest method testImmutableCollection.
@Test
public void testImmutableCollection() {
Country country = new Country();
country.setName("Germany");
List states = new ArrayList<State>();
State bayern = new State();
bayern.setName("Bayern");
State hessen = new State();
hessen.setName("Hessen");
State sachsen = new State();
sachsen.setName("Sachsen");
states.add(bayern);
states.add(hessen);
states.add(sachsen);
country.setStates(states);
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(country);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
Country germany = (Country) s.get(Country.class, country.getId());
assertNotNull(germany);
assertEquals("Wrong number of states", 3, germany.getStates().size());
// try adding a state
State foobar = new State();
foobar.setName("foobar");
s.save(foobar);
germany.getStates().add(foobar);
try {
tx.commit();
fail();
} catch (PersistenceException ex) {
// expected
assertTrue(ex.getMessage().contains("changed an immutable collection instance"));
log.debug("success");
}
s.close();
s = openSession();
tx = s.beginTransaction();
germany = (Country) s.get(Country.class, country.getId());
assertNotNull(germany);
assertEquals("Wrong number of states", 3, germany.getStates().size());
// try deleting a state
germany.getStates().remove(0);
try {
tx.commit();
fail();
} catch (PersistenceException e) {
assertTrue(e.getMessage().contains("changed an immutable collection instance"));
log.debug("success");
}
s.close();
s = openSession();
tx = s.beginTransaction();
germany = (Country) s.get(Country.class, country.getId());
assertNotNull(germany);
assertEquals("Wrong number of states", 3, germany.getStates().size());
tx.commit();
s.close();
}
Aggregations