#1847 - Delete bean with ManyToMany to always delete from intersection (not required cascade REMOVE)

This commit is contained in:
rob bygrave
2019-10-18 15:35:21 +13:00
parent de75bcdaf9
commit dac55ab8e9
5 changed files with 37 additions and 31 deletions
@@ -68,5 +68,7 @@ public class TestSkippable extends BaseTestCase {
Assert.assertEquals(value2.getId(), value2_DB_2.getId());
Assert.assertTrue("Cascade failed", value2.getId().equals(value2_DB_2.getId()));
Ebean.delete(listAttributeDB_2);
}
}
@@ -4,26 +4,33 @@ import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.annotation.IgnorePlatform;
import io.ebean.annotation.Platform;
import org.ebeantest.LoggedSqlCollector;
import org.junit.BeforeClass;
import org.junit.Test;
import org.tests.model.basic.MnocRole;
import org.tests.model.basic.MnocUser;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.PersistenceException;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestM2MDeleteNoCascade extends BaseTestCase {
private static MnocRole r0 = new MnocRole("r0");
private static MnocRole r1 = new MnocRole("r1");
private static MnocRole r2 = new MnocRole("r2");
@BeforeClass
public static void setup() {
Ebean.save(r0);
Ebean.save(r1);
Ebean.save(r2);
}
@IgnorePlatform(Platform.NUODB)
@Test
public void test() {
MnocRole r0 = new MnocRole("r0");
MnocRole r1 = new MnocRole("r0");
Ebean.save(r0);
Ebean.save(r1);
MnocUser u0 = new MnocUser("usr0");
u0.addValidRole(r0);
u0.addValidRole(r1);
@@ -32,21 +39,17 @@ public class TestM2MDeleteNoCascade extends BaseTestCase {
MnocUser loadedUser = Ebean.find(MnocUser.class, u0.getUserId());
List<MnocRole> validRoles = loadedUser.getValidRoles();
assertThat(validRoles).hasSize(2);
Assert.assertEquals(2, validRoles.size());
try {
Ebean.delete(u0);
Assert.assertTrue("expecting an exception", false);
} catch (PersistenceException e) {
// we expect this
Assert.assertTrue(true);
}
u0.getValidRoles().clear();
Ebean.save(u0);
LoggedSqlCollector.start();
Ebean.delete(u0);
final List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(2);
assertThat(sql.get(0)).contains("delete from mnoc_user_mnoc_role where mnoc_user_user_id = ?");
assertThat(sql.get(1)).contains("delete from mnoc_user where user_id=? and version=?");
final MnocUser found = Ebean.find(MnocUser.class, u0.getUserId());
assertThat(found).isNull();
}
}
@@ -15,8 +15,13 @@ public class MnocRole {
@Version
Integer version;
public MnocRole() {
/**
* Only for testing.
*/
public MnocRole(MnocRole other) {
this.roleId = other.getRoleId();
this.roleName = other.getRoleName();
this.version = other.getVersion();
}
public MnocRole(String roleName) {
@@ -21,14 +21,10 @@ public class MnocUser {
Integer version;
// No cascade REMOVE
@ManyToMany(cascade = CascadeType.PERSIST)
@ManyToMany
@OrderBy("roleName")
List<MnocRole> validRoles;
public MnocUser() {
}
public MnocUser(String userName) {
this.userName = userName;
}