use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class AbstractEntityWithOneToManyTest method testOneToManyCollectionOptimisticLockingWithMerge.
@Test
public void testOneToManyCollectionOptimisticLockingWithMerge() {
clearCounts();
Contract cOrig = new Contract(null, "gail", "phone");
Party partyOrig = new Party("party");
cOrig.addParty(partyOrig);
Session s = openSession();
Transaction t = s.beginTransaction();
s.persist(cOrig);
t.commit();
s.close();
assertInsertCount(2);
assertUpdateCount(0);
clearCounts();
s = openSession();
t = s.beginTransaction();
Contract c = (Contract) s.get(Contract.class, cOrig.getId());
Party newParty = new Party("new party");
c.addParty(newParty);
t.commit();
s.close();
assertInsertCount(1);
assertUpdateCount(isContractVersioned ? 1 : 0);
clearCounts();
s = openSession();
t = s.beginTransaction();
cOrig.removeParty(partyOrig);
try {
s.merge(cOrig);
assertFalse(isContractVersioned);
} catch (PersistenceException ex) {
assertTyping(StaleObjectStateException.class, ex.getCause());
assertTrue(isContractVersioned);
} finally {
t.rollback();
}
s.close();
s = openSession();
t = s.beginTransaction();
c = (Contract) s.createCriteria(Contract.class).uniqueResult();
s.delete(c);
assertEquals(Long.valueOf(0), s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult());
assertEquals(Long.valueOf(0), s.createCriteria(Party.class).setProjection(Projections.rowCount()).uniqueResult());
t.commit();
s.close();
assertUpdateCount(0);
assertDeleteCount(3);
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class ImmutableTest method testImmutableCollectionWithUpdate.
@Test
public void testImmutableCollectionWithUpdate() {
clearCounts();
Contract c = new Contract(null, "gavin", "phone");
ContractVariation cv1 = new ContractVariation(1, c);
cv1.setText("expensive");
ContractVariation cv2 = new ContractVariation(2, c);
cv2.setText("more expensive");
Session s = openSession();
Transaction t = s.beginTransaction();
s.persist(c);
t.commit();
s.close();
assertInsertCount(3);
assertUpdateCount(0);
s = openSession();
t = s.beginTransaction();
c.getVariations().add(new ContractVariation(3, c));
s.update(c);
try {
t.commit();
fail("should have failed because reassociated object has a dirty collection");
} catch (PersistenceException ex) {
// expected
} finally {
t.rollback();
s.close();
}
assertUpdateCount(0);
s = openSession();
t = s.beginTransaction();
c = (Contract) s.createCriteria(Contract.class).uniqueResult();
assertEquals(c.getCustomerName(), "gavin");
assertEquals(c.getVariations().size(), 2);
Iterator it = c.getVariations().iterator();
cv1 = (ContractVariation) it.next();
assertEquals(cv1.getText(), "expensive");
cv2 = (ContractVariation) it.next();
assertEquals(cv2.getText(), "more expensive");
s.delete(c);
assertEquals(s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult(), new Long(0));
assertEquals(s.createCriteria(ContractVariation.class).setProjection(Projections.rowCount()).uniqueResult(), new Long(0));
t.commit();
s.close();
assertUpdateCount(0);
assertDeleteCount(3);
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class AbstractEntityWithManyToManyTest method testManyToManyCollectionOptimisticLockingWithUpdate.
@Test
public void testManyToManyCollectionOptimisticLockingWithUpdate() {
clearCounts();
Plan pOrig = new Plan("plan");
Contract cOrig = new Contract(null, "gail", "phone");
pOrig.addContract(cOrig);
Session s = openSession();
Transaction t = s.beginTransaction();
s.persist(pOrig);
t.commit();
s.close();
assertInsertCount(2);
assertUpdateCount(0);
clearCounts();
s = openSession();
t = s.beginTransaction();
Plan p = (Plan) s.get(Plan.class, pOrig.getId());
Contract newC = new Contract(null, "yogi", "pawprint");
p.addContract(newC);
t.commit();
s.close();
assertInsertCount(1);
assertUpdateCount(isContractVersioned ? 1 : 0);
clearCounts();
s = openSession();
t = s.beginTransaction();
pOrig.removeContract(cOrig);
s.update(pOrig);
try {
t.commit();
assertFalse(isContractVersioned);
} catch (PersistenceException ex) {
t.rollback();
assertTrue(isContractVersioned);
if (!sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData()) {
assertTyping(StaleObjectStateException.class, ex.getCause());
} else {
assertTyping(StaleStateException.class, ex.getCause());
}
}
s.close();
s = openSession();
t = s.beginTransaction();
p = (Plan) s.createCriteria(Plan.class).uniqueResult();
s.delete(p);
s.createQuery("delete from Contract").executeUpdate();
assertEquals(new Long(0), s.createCriteria(Plan.class).setProjection(Projections.rowCount()).uniqueResult());
assertEquals(new Long(0), s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult());
t.commit();
s.close();
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class AbstractEntityWithManyToManyTest method testManyToManyCollectionOptimisticLockingWithMerge.
@Test
public void testManyToManyCollectionOptimisticLockingWithMerge() {
clearCounts();
Plan pOrig = new Plan("plan");
Contract cOrig = new Contract(null, "gail", "phone");
pOrig.addContract(cOrig);
Session s = openSession();
Transaction t = s.beginTransaction();
s.persist(pOrig);
t.commit();
s.close();
assertInsertCount(2);
assertUpdateCount(0);
clearCounts();
s = openSession();
t = s.beginTransaction();
Plan p = (Plan) s.get(Plan.class, pOrig.getId());
Contract newC = new Contract(null, "sherman", "note");
p.addContract(newC);
t.commit();
s.close();
assertInsertCount(1);
assertUpdateCount(isContractVersioned ? 1 : 0);
clearCounts();
s = openSession();
t = s.beginTransaction();
pOrig.removeContract(cOrig);
try {
s.merge(pOrig);
assertFalse(isContractVersioned);
} catch (PersistenceException ex) {
assertTyping(StaleObjectStateException.class, ex.getCause());
assertTrue(isContractVersioned);
} finally {
t.rollback();
}
s.close();
s = openSession();
t = s.beginTransaction();
p = (Plan) s.createCriteria(Plan.class).uniqueResult();
s.delete(p);
assertEquals(new Long(0), s.createCriteria(Plan.class).setProjection(Projections.rowCount()).uniqueResult());
assertEquals(new Long(0), s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult());
t.commit();
s.close();
assertUpdateCount(0);
assertDeleteCount(3);
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class AbstractEntityWithOneToManyTest method testOneToManyCollectionOptimisticLockingWithUpdate.
@Test
public void testOneToManyCollectionOptimisticLockingWithUpdate() {
clearCounts();
Contract cOrig = new Contract(null, "gail", "phone");
Party partyOrig = new Party("party");
cOrig.addParty(partyOrig);
Session s = openSession();
Transaction t = s.beginTransaction();
s.persist(cOrig);
t.commit();
s.close();
assertInsertCount(2);
assertUpdateCount(0);
clearCounts();
s = openSession();
t = s.beginTransaction();
Contract c = (Contract) s.get(Contract.class, cOrig.getId());
Party newParty = new Party("new party");
c.addParty(newParty);
t.commit();
s.close();
assertInsertCount(1);
assertUpdateCount(isContractVersioned ? 1 : 0);
clearCounts();
s = openSession();
t = s.beginTransaction();
cOrig.removeParty(partyOrig);
s.update(cOrig);
try {
t.commit();
assertFalse(isContractVersioned);
} catch (PersistenceException ex) {
t.rollback();
assertTrue(isContractVersioned);
if (!sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData()) {
assertTyping(StaleObjectStateException.class, ex.getCause());
} else {
assertTyping(StaleStateException.class, ex.getCause());
}
}
s.close();
s = openSession();
t = s.beginTransaction();
c = (Contract) s.createCriteria(Contract.class).uniqueResult();
s.createQuery("delete from Party").executeUpdate();
s.delete(c);
assertEquals(Long.valueOf(0), s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult());
assertEquals(Long.valueOf(0), s.createCriteria(Party.class).setProjection(Projections.rowCount()).uniqueResult());
t.commit();
s.close();
}
Aggregations