diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java b/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java index db911864d..1a878215f 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java @@ -213,8 +213,8 @@ public class ModelContainer { /** * Add a table (typically from reading EbeanServer meta data). */ - public void addTable(MTable table) { - tables.put(table.getName(), table); + public MTable addTable(MTable table) { + return tables.put(table.getName(), table); } /** diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildContext.java b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildContext.java index 0b3c2de07..0cd86310e 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildContext.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildContext.java @@ -67,8 +67,8 @@ public class ModelBuildContext { return maxLength(constraintNaming.checkConstraintName(tableName, columnName), checkCount); } - public void addTable(MTable table) { - model.addTable(table); + public MTable addTable(MTable table) { + return model.addTable(table); } public void addIndex(String indexName, String tableName, String columnName) { diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildIntersectionTable.java b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildIntersectionTable.java index a1ed1a2da..b64e5177e 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildIntersectionTable.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/build/ModelBuildIntersectionTable.java @@ -35,7 +35,11 @@ public class ModelBuildIntersectionTable { public void build() { intersectionTable = createTable(); - ctx.addTable(intersectionTable); + MTable existingTable = ctx.addTable(intersectionTable); + if (existingTable != null) { + throw new IllegalStateException("Property " + manyProp.getFullBeanName() + " has duplicate ManyToMany intersection table " + intersectionTable.getName() + + ". Please use @JoinTable to define unique table to use"); + } buildFkConstraints(); } diff --git a/src/test/java/com/avaje/tests/m2m/TestM2MMultipleLists.java b/src/test/java/com/avaje/tests/m2m/TestM2MMultipleLists.java new file mode 100644 index 000000000..e42fb973b --- /dev/null +++ b/src/test/java/com/avaje/tests/m2m/TestM2MMultipleLists.java @@ -0,0 +1,31 @@ +package com.avaje.tests.m2m; + +import com.avaje.ebean.BaseTestCase; +import com.avaje.ebean.Ebean; +import com.avaje.tests.model.m2m.MailBox; +import com.avaje.tests.model.m2m.MailUser; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestM2MMultipleLists extends BaseTestCase { + + @Test + public void test() { + + MailUser u = new MailUser(); + u.setName("mailUser1"); + + MailBox m1 = new MailBox(); + m1.setName("mailBox1"); + + u.getInbox().add(m1); + Ebean.save(u); + + u = Ebean.find(MailUser.class, u.getId()); + + assertEquals(1, u.getInbox().size()); + assertEquals(0, u.getOutbox().size()); + + } +} diff --git a/src/test/java/com/avaje/tests/model/m2m/MailBox.java b/src/test/java/com/avaje/tests/model/m2m/MailBox.java new file mode 100644 index 000000000..5dd830fac --- /dev/null +++ b/src/test/java/com/avaje/tests/model/m2m/MailBox.java @@ -0,0 +1,42 @@ +package com.avaje.tests.model.m2m; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Version; + +@Entity +public class MailBox { + + @Id + Long id; + + @Version + Long version; + + String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version = version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} + diff --git a/src/test/java/com/avaje/tests/model/m2m/MailUser.java b/src/test/java/com/avaje/tests/model/m2m/MailUser.java new file mode 100644 index 000000000..dfc8ffe43 --- /dev/null +++ b/src/test/java/com/avaje/tests/model/m2m/MailUser.java @@ -0,0 +1,70 @@ +package com.avaje.tests.model.m2m; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.Version; +import java.util.List; + +@Entity +public class MailUser { + + @Id + Long id; + + @Version + Long version; + + String name; + + @ManyToMany(cascade = CascadeType.ALL) + @JoinTable(name = "mail_user_inbox") + List inbox; + + @ManyToMany(cascade = CascadeType.ALL) + @JoinTable(name = "mail_user_outbox") + List outbox; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version = version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getInbox() { + return inbox; + } + + public void setInbox(List inbox) { + this.inbox = inbox; + } + + public List getOutbox() { + return outbox; + } + + public void setOutbox(List outbox) { + this.outbox = outbox; + } +} +