use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class ImmutableEntityNaturalIdTest method testNaturalIdCheck.
@Test
public void testNaturalIdCheck() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Parent p = new Parent("alex");
Child c = new Child("billy", p);
s.persist(p);
s.persist(c);
t.commit();
s.close();
Field name = c.getClass().getDeclaredField("name");
name.setAccessible(true);
name.set(c, "phil");
s = openSession();
t = s.beginTransaction();
try {
s.saveOrUpdate(c);
s.flush();
fail("should have failed because immutable natural ID was altered");
} catch (PersistenceException e) {
// expected
} finally {
t.rollback();
s.close();
}
name.set(c, "billy");
s = openSession();
t = s.beginTransaction();
s.delete(c);
s.delete(p);
t.commit();
s.close();
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class MergeMultipleEntityCopiesAllowedTest method testNestedEntityNewerThanTopLevel.
@Test
public void testNestedEntityNewerThanTopLevel() {
Item item = new Item();
item.setName("item");
Category category = new Category();
category.setName("category");
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(item);
s.persist(category);
tx.commit();
s.close();
// Get category1_1 from a different session.
s = openSession();
Category category1_1 = (Category) s.get(Category.class, category.getId());
s.close();
// Get and update category1_2 to increment its version.
s = openSession();
tx = s.beginTransaction();
Category category1_2 = (Category) s.get(Category.class, category.getId());
category1_2.setName("new name");
tx.commit();
s.close();
assertTrue(category1_2.getVersion() > category1_1.getVersion());
category1_1.setExampleItem(item);
item.setCategory(category1_2);
s = openSession();
tx = s.beginTransaction();
try {
// nested representation is newer than top lever representation.
category1_1 = (Category) s.merge(category1_1);
fail("should have failed because one representation is an older version.");
} catch (PersistenceException e) {
// expected
assertTyping(StaleObjectStateException.class, e.getCause());
} finally {
tx.rollback();
s.close();
}
cleanup();
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class MergeTest method testMergeStaleVersionFails.
@Test
public void testMergeStaleVersionFails() throws Exception {
Session s = openSession();
s.beginTransaction();
VersionedEntity entity = new VersionedEntity("entity", "entity");
s.persist(entity);
s.getTransaction().commit();
s.close();
// make the detached 'entity' reference stale...
s = openSession();
s.beginTransaction();
VersionedEntity entity2 = (VersionedEntity) s.get(VersionedEntity.class, entity.getId());
entity2.setName("entity-name");
s.getTransaction().commit();
s.close();
// now try to reattch it
s = openSession();
s.beginTransaction();
try {
s.merge(entity);
s.getTransaction().commit();
fail("was expecting staleness error");
} catch (PersistenceException e) {
// expected
assertTyping(StaleObjectStateException.class, e.getCause());
} finally {
s.getTransaction().rollback();
s.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class OptimisticLockTest method testDeleteOptimisticLockFailure.
private void testDeleteOptimisticLockFailure(String entityName) {
Session mainSession = openSession();
mainSession.beginTransaction();
Document doc = new Document();
doc.setTitle("Hibernate in Action");
doc.setAuthor("Bauer et al");
doc.setSummary("Very boring book about persistence");
doc.setText("blah blah yada yada yada");
doc.setPubDate(new PublicationDate(2004));
mainSession.save(entityName, doc);
mainSession.flush();
doc.setSummary("A modern classic");
mainSession.flush();
doc.getPubDate().setMonth(Integer.valueOf(3));
mainSession.flush();
mainSession.getTransaction().commit();
mainSession.close();
mainSession = openSession();
mainSession.beginTransaction();
doc = (Document) mainSession.get(entityName, doc.getId());
Session otherSession = openSession();
otherSession.beginTransaction();
Document otherDoc = (Document) otherSession.get(entityName, doc.getId());
otherDoc.setSummary("my other summary");
otherSession.flush();
otherSession.getTransaction().commit();
otherSession.close();
try {
mainSession.delete(doc);
mainSession.flush();
fail("expecting opt lock failure");
} catch (StaleObjectStateException e) {
// expected
} catch (PersistenceException e) {
// expected
checkException(mainSession, e);
}
mainSession.clear();
mainSession.getTransaction().rollback();
mainSession.close();
mainSession = openSession();
mainSession.beginTransaction();
doc = (Document) mainSession.load(entityName, doc.getId());
mainSession.delete(entityName, doc);
mainSession.getTransaction().commit();
mainSession.close();
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class OptimisticLockTest method testUpdateOptimisticLockFailure.
private void testUpdateOptimisticLockFailure(String entityName) {
Session mainSession = openSession();
mainSession.beginTransaction();
Document doc = new Document();
doc.setTitle("Hibernate in Action");
doc.setAuthor("Bauer et al");
doc.setSummary("Very boring book about persistence");
doc.setText("blah blah yada yada yada");
doc.setPubDate(new PublicationDate(2004));
mainSession.save(entityName, doc);
mainSession.getTransaction().commit();
mainSession.close();
mainSession = openSession();
mainSession.beginTransaction();
doc = (Document) mainSession.get(entityName, doc.getId());
Session otherSession = sessionFactory().openSession();
otherSession.beginTransaction();
Document otherDoc = (Document) otherSession.get(entityName, doc.getId());
otherDoc.setSummary("A modern classic");
otherSession.getTransaction().commit();
otherSession.close();
try {
doc.setSummary("A machiavellian achievement of epic proportions");
mainSession.flush();
fail("expecting opt lock failure");
} catch (PersistenceException e) {
// expected
checkException(mainSession, e);
}
mainSession.clear();
mainSession.getTransaction().rollback();
mainSession.close();
mainSession = openSession();
mainSession.beginTransaction();
doc = (Document) mainSession.load(entityName, doc.getId());
mainSession.delete(entityName, doc);
mainSession.getTransaction().commit();
mainSession.close();
}
Aggregations