Skip to content

Commit cd5da04

Browse files
author
Igor Polevoy
authored
Merge pull request #685 from cschabl/enhancement-676
#676 Make @many2many repeatable
2 parents a463b7d + 67c70b8 commit cd5da04

21 files changed

+324
-47
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/Registry.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ private void processOverrides(List<Class<? extends Model>> models) {
257257
HasMany hasManyAnnotation = modelClass.getAnnotation(HasMany.class);
258258
processOverridesHasMany(modelClass, hasManyAnnotation);
259259

260+
Many2Manies many2ManiesAnnotation = modelClass.getAnnotation(Many2Manies.class);
261+
if (many2ManiesAnnotation != null) {
262+
for (Many2Many many2Many : many2ManiesAnnotation.value()) {
263+
processManyToManyOverrides(many2Many, modelClass);
264+
}
265+
}
266+
260267
Many2Many many2manyAnnotation = modelClass.getAnnotation(Many2Many.class);
261268
if(many2manyAnnotation != null){
262269
processManyToManyOverrides(many2manyAnnotation, modelClass);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.javalite.activejdbc.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* @author Christof Schablinski
10+
*/
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@Target(ElementType.TYPE)
13+
public @interface Many2Manies
14+
{
15+
Many2Many[] value();
16+
}

activejdbc/src/main/java/org/javalite/activejdbc/annotations/Many2Many.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.javalite.activejdbc.Model;
2121

2222
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Repeatable;
2324
import java.lang.annotation.Retention;
2425
import java.lang.annotation.RetentionPolicy;
2526
import java.lang.annotation.Target;
@@ -32,6 +33,7 @@
3233
*/
3334
@Retention(RetentionPolicy.RUNTIME)
3435
@Target(ElementType.TYPE)
36+
@Repeatable(Many2Manies.class)
3537
//TODO: rename to ManyToMany?
3638
public @interface Many2Many {
3739
/**

activejdbc/src/test/java/org/javalite/activejdbc/IncludesTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ public void shouldIncludeMany2ManyInCaseJoinTableHasUnconventionalPKName() {
173173
a(recipe1).shouldBeTheSameAs(recipe2);
174174
}
175175

176+
@Test
177+
public void shouldIncludeMultipleMany2ManyOverridesOnOneModel() {
178+
Genre hardRock = Genre.createIt("name", "Hard rock");
179+
Genre garageRock = Genre.createIt("name", "Garage rock");
180+
Musician dankoJonesHimself = Musician.createIt("first_name", "Danko", "last_name", "Jones");
181+
Musician johnCalabrese = Musician.createIt("first_name", "John", "last_name", "Calabrese");
182+
Band dankoJones = Band.createIt("name", "Danko Jones");
183+
184+
dankoJones.add(hardRock);
185+
dankoJones.add(garageRock);
186+
dankoJones.add(dankoJonesHimself);
187+
dankoJones.add(johnCalabrese);
188+
189+
List<Band> bands = Band.findAll().include(Genre.class, Musician.class);
190+
List<Genre> genres = bands.get(0).getAll(Genre.class);
191+
a(genres.size()).shouldBeEqual(2);
192+
List<Musician> musicians = bands.get(0).getAll(Musician.class);
193+
a(musicians.size()).shouldBeEqual(2);
194+
}
195+
176196
@Test
177197
public void shouldFixDefect163NeedsToIncludeChildrenAndParentsInTreeStructure(){
178198
Node car = new Node("Car");

activejdbc/src/test/java/org/javalite/activejdbc/statement_providers/DB2StatementProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ public List<String> getPopulateStatements(String table) {
198198
"INSERT INTO players VALUES (3, 'Nick', 'Foles', 2)",
199199
"INSERT INTO players VALUES (4, 'Trey', 'Burton', 2)"
200200
);
201+
} else if (table.equals("bands")) {
202+
statements = Arrays.asList();
203+
} else if (table.equals("genres")) {
204+
statements = Arrays.asList();
205+
} else if (table.equals("musicians")) {
206+
statements = Arrays.asList();
207+
} else if (table.equals("bands_genres")) {
208+
statements = Arrays.asList();
209+
} else if (table.equals("bands_musicians")) {
210+
statements = Arrays.asList();
201211
} else {
202212
statements = Arrays.asList();
203213
}

activejdbc/src/test/java/org/javalite/activejdbc/statement_providers/H2StatementProvider.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,17 @@ public List<String> getPopulateStatements(String table) {
197197
"INSERT INTO players VALUES (3, 'Nick', 'Foles', 2);",
198198
"INSERT INTO players VALUES (4, 'Trey', 'Burton', 2);"
199199
);
200-
}
201-
else {
200+
} else if (table.equals("bands")) {
201+
statements = Arrays.asList();
202+
} else if (table.equals("genres")) {
203+
statements = Arrays.asList();
204+
} else if (table.equals("musicians")) {
205+
statements = Arrays.asList();
206+
} else if (table.equals("bands_genres")) {
207+
statements = Arrays.asList();
208+
} else if (table.equals("bands_musicians")) {
209+
statements = Arrays.asList();
210+
} else {
202211
statements = Arrays.asList();
203212
}
204213

activejdbc/src/test/java/org/javalite/activejdbc/statement_providers/MSSQLStatementProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ public List<String> getPopulateStatements(String table) {
196196
"INSERT INTO players (id, first_name, last_name, team_id) VALUES (3, 'Nick', 'Foles', 2);",
197197
"INSERT INTO players (id, first_name, last_name, team_id) VALUES (4, 'Trey', 'Burton', 2);"
198198
);
199+
} else if (table.equals("bands")) {
200+
statements = Arrays.asList();
201+
} else if (table.equals("genres")) {
202+
statements = Arrays.asList();
203+
} else if (table.equals("musicians")) {
204+
statements = Arrays.asList();
205+
} else if (table.equals("bands_genres")) {
206+
statements = Arrays.asList();
207+
} else if (table.equals("bands_musicians")) {
208+
statements = Arrays.asList();
199209
} else{
200210
statements = Arrays.asList();
201211
}

activejdbc/src/test/java/org/javalite/activejdbc/statement_providers/MySQLStatementProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ public List<String> getPopulateStatements(String table) {
214214
"INSERT INTO players VALUES (3, 'Nick', 'Foles', 2);",
215215
"INSERT INTO players VALUES (4, 'Trey', 'Burton', 2);"
216216
);
217+
} else if (table.equals("bands")) {
218+
statements = Arrays.asList();
219+
} else if (table.equals("genres")) {
220+
statements = Arrays.asList();
221+
} else if (table.equals("musicians")) {
222+
statements = Arrays.asList();
223+
} else if (table.equals("bands_genres")) {
224+
statements = Arrays.asList();
225+
} else if (table.equals("bands_musicians")) {
226+
statements = Arrays.asList();
217227
}else{
218228
statements = Arrays.asList();
219229
}

activejdbc/src/test/java/org/javalite/activejdbc/statement_providers/OracleStatementProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ public List<String> getPopulateStatements(String table) {
193193
"INSERT INTO players VALUES (3, 'Nick', 'Foles', 2)",
194194
"INSERT INTO players VALUES (4, 'Trey', 'Burton', 2)"
195195
);
196+
} else if (table.equals("bands")) {
197+
statements = Arrays.asList();
198+
} else if (table.equals("genres")) {
199+
statements = Arrays.asList();
200+
} else if (table.equals("musicians")) {
201+
statements = Arrays.asList();
202+
} else if (table.equals("bands_genres")) {
203+
statements = Arrays.asList();
204+
} else if (table.equals("bands_musicians")) {
205+
statements = Arrays.asList();
196206
} else{
197207
statements = Arrays.asList();
198208
}

activejdbc/src/test/java/org/javalite/activejdbc/statement_providers/PostgreSQLStatementProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ public List<String> getPopulateStatements(String table) {
194194
"INSERT INTO players VALUES (3, 'Nick', 'Foles', 2);",
195195
"INSERT INTO players VALUES (4, 'Trey', 'Burton', 2);"
196196
);
197+
} else if (table.equals("bands")) {
198+
statements = Arrays.asList();
199+
} else if (table.equals("genres")) {
200+
statements = Arrays.asList();
201+
} else if (table.equals("musicians")) {
202+
statements = Arrays.asList();
203+
} else if (table.equals("bands_genres")) {
204+
statements = Arrays.asList();
205+
} else if (table.equals("bands_musicians")) {
206+
statements = Arrays.asList();
197207
} else {
198208
statements = Arrays.asList();
199209
}

0 commit comments

Comments
 (0)