use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class ManyToManyAssociationClassGeneratedIdTest method testRemoveAndAddEqualElement.
@Test
public void testRemoveAndAddEqualElement() {
deleteMembership(getUser(), getGroup(), getMembership());
addMembership(getUser(), getGroup(), createMembership("membership"));
Session s = openSession();
s.beginTransaction();
try {
// The new membership is transient (it has a null surrogate ID), so
// Hibernate assumes that it should be added to the collection.
// Inserts are done beforeQuery deletes, so a ConstraintViolationException
// will be thrown on the insert because the unique constraint on the
// user and group IDs in the join table is violated. See HHH-2801.
s.merge(getUser());
s.getTransaction().commit();
fail("should have failed because inserts are beforeQuery deletes");
} catch (PersistenceException e) {
s.getTransaction().rollback();
// expected
assertTyping(ConstraintViolationException.class, e.getCause());
} finally {
s.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class ManyToManyAssociationClassGeneratedIdTest method testRemoveAndAddEqualElementNonKeyModified.
@Test
public void testRemoveAndAddEqualElementNonKeyModified() {
deleteMembership(getUser(), getGroup(), getMembership());
Membership membershipNew = createMembership("membership");
addMembership(getUser(), getGroup(), membershipNew);
membershipNew.setName("membership1");
Session s = openSession();
s.beginTransaction();
try {
// The new membership is transient (it has a null surrogate ID), so
// Hibernate assumes that it should be added to the collection.
// Inserts are done beforeQuery deletes, so a ConstraintViolationException
// will be thrown on the insert because the unique constraint on the
// user and group IDs in the join table is violated. See HHH-2801.
s.merge(getUser());
s.getTransaction().commit();
fail("should have failed because inserts are beforeQuery deletes");
} catch (PersistenceException e) {
s.getTransaction().rollback();
// expected
assertTyping(ConstraintViolationException.class, e.getCause());
} finally {
s.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class ManyToManyAssociationClassGeneratedIdTest method testRemoveAndAddEqualCollection.
@Test
public void testRemoveAndAddEqualCollection() {
deleteMembership(getUser(), getGroup(), getMembership());
getUser().setMemberships(new HashSet());
getGroup().setMemberships(new HashSet());
addMembership(getUser(), getGroup(), createMembership("membership"));
Session s = openSession();
s.beginTransaction();
try {
// The new membership is transient (it has a null surrogate ID), so
// Hibernate assumes that it should be added to the collection.
// Inserts are done beforeQuery deletes, so a ConstraintViolationException
// will be thrown on the insert because the unique constraint on the
// user and group IDs in the join table is violated. See HHH-2801.
s.merge(getUser());
s.getTransaction().commit();
fail("should have failed because inserts are beforeQuery deletes");
} catch (PersistenceException e) {
s.getTransaction().rollback();
// expected
assertTyping(ConstraintViolationException.class, e.getCause());
} finally {
s.close();
}
}
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();
}
Aggregations