mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#809 - Add test / example showing ... transaction.setPersistCascade(false) and transaction.commitAndContinue();
This commit is contained in:
@@ -2,6 +2,7 @@ package com.avaje.tests.transaction;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.tests.model.m2m.MnyB;
|
||||
import com.avaje.tests.model.m2m.MnyC;
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
@@ -13,6 +14,7 @@ import javax.persistence.PersistenceException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestBeanStateReset extends BaseTestCase {
|
||||
|
||||
@@ -47,4 +49,51 @@ public class TestBeanStateReset extends BaseTestCase {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alternativeVia_persistCascadeOff_with_commitAndContinue() {
|
||||
|
||||
// setup to fail foreign key constraint when using cascade save
|
||||
MnyC c = new MnyC();
|
||||
c.setId(Long.MAX_VALUE);
|
||||
|
||||
MnyB b = new MnyB();
|
||||
b.getCs().add(c);
|
||||
|
||||
Transaction transaction = Ebean.beginTransaction();
|
||||
|
||||
try {
|
||||
// turn off cascade ...
|
||||
transaction.setPersistCascade(false);
|
||||
b.save();
|
||||
|
||||
// commit at this point
|
||||
transaction.commitAndContinue();
|
||||
try {
|
||||
|
||||
// turn on cascade ... such that the ManyToMany is persisted
|
||||
transaction.setPersistCascade(true);
|
||||
|
||||
// save b again which this time cascades to the ManyToMany
|
||||
// but this fails due to FK on ManyToMany
|
||||
b.save();
|
||||
|
||||
// we actually don't get here due to the FK error
|
||||
transaction.commit();
|
||||
|
||||
} catch (PersistenceException e) {
|
||||
// so we failed the second save but that is ok'ish
|
||||
// we handle this exception knowing b got inserted and committed
|
||||
// and that the inserts into the intersection table failed
|
||||
logger.info("The ManyToMany intersection error: " + e.getMessage());
|
||||
}
|
||||
} finally {
|
||||
// performs a rollback as the commit at line:80 does not happen
|
||||
transaction.end();
|
||||
}
|
||||
|
||||
// assert our insert prior to the commitAndContinue succeeded
|
||||
MnyB madeIt = Ebean.find(MnyB.class, b.getId());
|
||||
assertNotNull(madeIt);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user