From 2be3099d55147249ebc78743c1a35f7697342230 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Mon, 19 Feb 2018 22:45:47 +1300 Subject: [PATCH] #1228 - Add support to not generate FK Constraint when @JoinColumn(foreignKey=@ForeignKey(ConstraintMode.NO_CONSTRAINT)) --- .../server/deploy/PropertyForeignKey.java | 13 +++++ .../deploy/parse/AnnotationAssocOnes.java | 10 ++++ .../java/org/tests/ddl/DfkNoneViaJoin.java | 50 +++++++++++++++++++ .../org/tests/ddl/TestForeignKeyModes.java | 18 +++++++ 4 files changed, 91 insertions(+) create mode 100644 src/test/java/org/tests/ddl/DfkNoneViaJoin.java diff --git a/src/main/java/io/ebeaninternal/server/deploy/PropertyForeignKey.java b/src/main/java/io/ebeaninternal/server/deploy/PropertyForeignKey.java index ab5b75111..8badce0ce 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/PropertyForeignKey.java +++ b/src/main/java/io/ebeaninternal/server/deploy/PropertyForeignKey.java @@ -10,6 +10,19 @@ public class PropertyForeignKey { private final ConstraintMode onDelete; private final ConstraintMode onUpdate; + /** + * Construct for "No Constraint". + */ + public PropertyForeignKey() { + this.noConstraint = true; + this.noIndex = false; + this.onDelete = ConstraintMode.GLOBAL_DEFAULT; + this.onUpdate = ConstraintMode.GLOBAL_DEFAULT; + } + + /** + * Construct for the mapping annotation. + */ public PropertyForeignKey(DbForeignKey dbForeignKey) { this.noIndex = dbForeignKey.noIndex(); this.noConstraint = dbForeignKey.noConstraint(); diff --git a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationAssocOnes.java b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationAssocOnes.java index 6294c5e82..15a3ed4de 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationAssocOnes.java +++ b/src/main/java/io/ebeaninternal/server/deploy/parse/AnnotationAssocOnes.java @@ -16,8 +16,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.persistence.Column; +import javax.persistence.ConstraintMode; import javax.persistence.Embedded; import javax.persistence.EmbeddedId; +import javax.persistence.ForeignKey; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; @@ -131,6 +133,7 @@ public class AnnotationAssocOnes extends AnnotationParser { if (!joinColumn.nullable()) { prop.setNullable(false); } + checkForNoConstraint(prop, joinColumn); } @@ -171,6 +174,13 @@ public class AnnotationAssocOnes extends AnnotationParser { } } + private void checkForNoConstraint(DeployBeanPropertyAssocOne prop, JoinColumn joinColumn) { + ForeignKey foreignKey = joinColumn.foreignKey(); + if (foreignKey != null && foreignKey.value() == ConstraintMode.NO_CONSTRAINT) { + prop.setForeignKey(new PropertyForeignKey()); + } + } + private String errorMsgMissingBeanTable(Class type, String from) { return "Error with association to [" + type + "] from [" + from + "]. Is " + type + " registered?"; } diff --git a/src/test/java/org/tests/ddl/DfkNoneViaJoin.java b/src/test/java/org/tests/ddl/DfkNoneViaJoin.java new file mode 100644 index 000000000..148e219b9 --- /dev/null +++ b/src/test/java/org/tests/ddl/DfkNoneViaJoin.java @@ -0,0 +1,50 @@ +package org.tests.ddl; + +import javax.persistence.ConstraintMode; +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity +public class DfkNoneViaJoin { + + @Id + long id; + + String name; + + @ManyToOne + @JoinColumn(name = "one_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) + DfkOne one; + + public DfkNoneViaJoin(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; + } +} diff --git a/src/test/java/org/tests/ddl/TestForeignKeyModes.java b/src/test/java/org/tests/ddl/TestForeignKeyModes.java index d0e8bdccf..a9b1216b2 100644 --- a/src/test/java/org/tests/ddl/TestForeignKeyModes.java +++ b/src/test/java/org/tests/ddl/TestForeignKeyModes.java @@ -30,6 +30,24 @@ public class TestForeignKeyModes extends BaseTestCase { } + @Test + public void noneViaJoin() { + + DfkOne one = new DfkOne("one2"); + Ebean.save(one); + + DfkNoneViaJoin none = new DfkNoneViaJoin("none2", one); + Ebean.save(none); + + // fails unless there is no Foreign key ... + Ebean.delete(one); + + DfkNoneViaJoin found = Ebean.find(DfkNoneViaJoin.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() {