use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class EntityTest method testUniqueConstraint.
@Test
public void testUniqueConstraint() throws Exception {
int id = 5;
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Sky sky = new Sky();
sky.id = Long.valueOf(id++);
sky.color = "green";
sky.day = "monday";
sky.month = "March";
Sky otherSky = new Sky();
otherSky.id = Long.valueOf(id++);
otherSky.color = "red";
otherSky.day = "friday";
otherSky.month = "March";
Sky sameSky = new Sky();
sameSky.id = Long.valueOf(id++);
sameSky.color = "green";
sameSky.day = "monday";
sameSky.month = "March";
s.save(sky);
s.flush();
s.save(otherSky);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
try {
s.save(sameSky);
tx.commit();
fail("unique constraints not respected");
} catch (PersistenceException e) {
//success
if (tx != null) {
tx.rollback();
}
} finally {
s.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class EntityTest method testVersion.
@Test
public void testVersion() throws Exception {
// put an object in DB
Session s = openSession();
Transaction tx = s.beginTransaction();
Flight firstOne = new Flight();
firstOne.setId(Long.valueOf(2));
firstOne.setName("AF3202");
firstOne.setDuration(Long.valueOf(500));
s.save(firstOne);
s.flush();
tx.commit();
s.close();
//read it
s = openSession();
tx = s.beginTransaction();
firstOne = (Flight) s.get(Flight.class, Long.valueOf(2));
tx.commit();
s.close();
//read it again
s = openSession();
tx = s.beginTransaction();
Flight concurrentOne = (Flight) s.get(Flight.class, Long.valueOf(2));
concurrentOne.setDuration(Long.valueOf(1000));
s.update(concurrentOne);
tx.commit();
s.close();
assertFalse(firstOne == concurrentOne);
assertFalse(firstOne.getVersion().equals(concurrentOne.getVersion()));
//reattach the first one
s = openSession();
tx = s.beginTransaction();
firstOne.setName("Second access");
s.update(firstOne);
try {
tx.commit();
fail("Optimistic locking should work");
} catch (PersistenceException expected) {
if (expected.getCause() instanceof StaleStateException) {
//expected
} else {
fail("StaleStateException expected but is " + expected.getCause());
}
} finally {
if (tx != null) {
tx.rollback();
}
s.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class FlushAndTransactionTest method testRollbackOnlyOnPersistenceException.
@Test
public void testRollbackOnlyOnPersistenceException() throws Exception {
Book book = new Book();
book.name = "Stolen keys";
//new Integer( 50 );
book.id = null;
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist(book);
em.flush();
em.clear();
book.setName("kitty kid");
em.merge(book);
em.flush();
em.clear();
//non updated version
book.setName("kitty kid2");
em.merge(book);
em.flush();
fail("optimistic locking exception");
} catch (PersistenceException e) {
//success
}
try {
em.getTransaction().commit();
fail("Commit should be rollbacked");
} catch (RollbackException e) {
//success
} finally {
em.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class JoinTest method testUniqueConstaintOnSecondaryTable.
@Test
public void testUniqueConstaintOnSecondaryTable() throws Exception {
Cat cat = new Cat();
cat.setStoryPart2("My long story");
Cat cat2 = new Cat();
cat2.setStoryPart2("My long story");
Session s = openSession();
Transaction tx = s.beginTransaction();
try {
s.persist(cat);
s.persist(cat2);
tx.commit();
fail("unique constraints violation on secondary table");
} catch (PersistenceException e) {
try {
assertTyping(ConstraintViolationException.class, e.getCause());
//success
} finally {
tx.rollback();
}
} finally {
s.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedToNonInverseCollection.
@Test
@SuppressWarnings({ "unchecked" })
public void testPersistOutsideTransactionCascadedToNonInverseCollection() {
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
Session s = openSession();
try {
MyEntity myEntity = new MyEntity("test-persist");
myEntity.getNonInverseChildren().add(new MyChild("test-child-persist-non-inverse"));
s.persist(myEntity);
assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
assertNull(myEntity.getId());
s.flush();
fail("TransactionRequiredException required upon flush");
} catch (PersistenceException ex) {
// expected
assertTyping(TransactionRequiredException.class, ex);
} finally {
s.close();
}
}
Aggregations