#1397 - @DbForeignKey(noConstraint = true) is ignored on @ManyToMany ... foreign constraints still added

This commit is contained in:
rob bygrave
2018-05-31 10:55:58 +12:00
parent 0aa9208dcc
commit ddc00fedb6
8 changed files with 124 additions and 25 deletions
@@ -0,0 +1,54 @@
package org.tests.ddl;
import io.ebean.annotation.DbForeignKey;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.ArrayList;
import java.util.List;
@Entity
public class DfkNoneViaMtoM {
@Id
long id;
String name;
/**
* Have no Foreign key constraints on the intersection table.
*/
@ManyToMany(cascade = CascadeType.ALL)
@DbForeignKey(noConstraint = true)
List<DfkOne> ones = new ArrayList<>();
public DfkNoneViaMtoM(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<DfkOne> getOnes() {
return ones;
}
public void setOnes(List<DfkOne> ones) {
this.ones = ones;
}
}
@@ -48,6 +48,40 @@ public class TestForeignKeyModes extends BaseTestCase {
assertThat(found.getOne()).isNotNull();
}
@Test
public void noneViaManyToMany() {
DfkOne one = Ebean.getReference(DfkOne.class, 999L);
DfkNoneViaMtoM none = new DfkNoneViaMtoM("none2");
none.getOnes().add(one);
// Would normally fail as DfkOne id:999 is not actually in Database
// and with the foreign key constraint on the intersection table the
// insert into the intersection table would fail
Ebean.save(none);
DfkNoneViaMtoM found = Ebean.find(DfkNoneViaMtoM.class)
.setId(none.getId())
.fetch("ones", "id")
.findOne();
assertThat(found).isNotNull();
// but we can't actually fetch it via ORM M2M ... as it joins across
// to DfkOne and that doesn't exist
assertThat(found.getOnes()).isNotNull();
assertThat(found.getOnes()).hasSize(0);
LoggedSqlCollector.start();
Ebean.delete(none);
List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(2);
assertThat(sql.get(0)).contains("delete from dfk_none_via_mto_m_dfk_one where dfk_none_via_mto_m_id = ?");
assertThat(sql.get(1)).contains("delete from dfk_none_via_mto_m where id=?");
}
@Test
public void setNullOnDelete() {
@@ -63,7 +97,6 @@ public class TestForeignKeyModes extends BaseTestCase {
DfkSetNull found = Ebean.find(DfkSetNull.class, other.getId());
assertThat(found).isNotNull();
assertThat(found.getOne()).isNull();
}
@Test