mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#323 - DDL Generation support for foreign key cascade options
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import io.ebean.annotation.ConstraintMode;
|
||||
import io.ebean.annotation.DbForeignKey;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class DfkCascade {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@ManyToOne
|
||||
@DbForeignKey(onDelete = ConstraintMode.CASCADE)
|
||||
DfkCascadeOne one;
|
||||
|
||||
public DfkCascade(String name, DfkCascadeOne one) {
|
||||
this.name = name;
|
||||
this.one = one;
|
||||
}
|
||||
|
||||
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 DfkCascadeOne getOne() {
|
||||
return one;
|
||||
}
|
||||
|
||||
public void setOne(DfkCascadeOne one) {
|
||||
this.one = one;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class DfkCascadeOne {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToMany(mappedBy = "one", cascade = CascadeType.ALL)
|
||||
List<DfkCascade> details;
|
||||
|
||||
public DfkCascadeOne(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<DfkCascade> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public void setDetails(List<DfkCascade> details) {
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import io.ebean.annotation.DbForeignKey;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class DfkNone {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@ManyToOne
|
||||
@DbForeignKey(noConstraint = true)
|
||||
DfkOne one;
|
||||
|
||||
public DfkNone(String name, DfkOne one) {
|
||||
this.name = name;
|
||||
this.one = one;
|
||||
}
|
||||
|
||||
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 DfkOne getOne() {
|
||||
return one;
|
||||
}
|
||||
|
||||
public void setOne(DfkOne one) {
|
||||
this.one = one;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class DfkOne {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
public DfkOne(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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import io.ebean.annotation.ConstraintMode;
|
||||
import io.ebean.annotation.DbForeignKey;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class DfkSetNull {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@ManyToOne
|
||||
@DbForeignKey(onDelete = ConstraintMode.SET_NULL)
|
||||
DfkOne one;
|
||||
|
||||
public DfkSetNull(String name, DfkOne one) {
|
||||
this.name = name;
|
||||
this.one = one;
|
||||
}
|
||||
|
||||
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 DfkOne getOne() {
|
||||
return one;
|
||||
}
|
||||
|
||||
public void setOne(DfkOne one) {
|
||||
this.one = one;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package org.tests.ddl;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestForeignKeyModes extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void none() {
|
||||
|
||||
DfkOne one = new DfkOne("one");
|
||||
Ebean.save(one);
|
||||
|
||||
DfkNone none = new DfkNone("none", one);
|
||||
Ebean.save(none);
|
||||
|
||||
// fails unless there is no Foreign key ...
|
||||
Ebean.delete(one);
|
||||
|
||||
DfkNone found = Ebean.find(DfkNone.class, none.getId());
|
||||
assertThat(found).isNotNull();
|
||||
// we still reference one ... even though it does not exist anymore
|
||||
assertThat(found.getOne()).isNotNull();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setNullOnDelete() {
|
||||
|
||||
DfkOne one = new DfkOne("one2");
|
||||
Ebean.save(one);
|
||||
|
||||
DfkSetNull other = new DfkSetNull("none", one);
|
||||
Ebean.save(other);
|
||||
|
||||
// success with ... fkey value set to null
|
||||
Ebean.delete(one);
|
||||
|
||||
DfkSetNull found = Ebean.find(DfkSetNull.class, other.getId());
|
||||
assertThat(found).isNotNull();
|
||||
assertThat(found.getOne()).isNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeleteCascade() {
|
||||
|
||||
DfkCascadeOne one = new DfkCascadeOne("one3");
|
||||
|
||||
|
||||
DfkCascade other = new DfkCascade("cascade1", one);
|
||||
one.getDetails().add(other);
|
||||
one.getDetails().add(new DfkCascade("cascade2", one));
|
||||
one.getDetails().add(new DfkCascade("cascade3", one));
|
||||
|
||||
Ebean.save(one);
|
||||
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
Ebean.delete(one);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("delete from dfk_cascade_one where id=?");
|
||||
|
||||
DfkCascade found = Ebean.find(DfkCascade.class, other.getId());
|
||||
assertThat(found).isNull();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user