Skip to content

Commit b489c72

Browse files
committed
Fixing issues with generated association mappings
1 parent 75f7baf commit b489c72

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

src/main/java/org/ngbsn/generator/AssociationMappingsGenerator.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,28 @@ public static void generateMappings(final Table table) {
4343
Table table1 = tablesMap.get(foreignKeyConstraintList.get(0).getReferencedTableName());
4444
Table table2 = tablesMap.get(foreignKeyConstraintList.get(1).getReferencedTableName());
4545

46+
//Adding @ManyToMany and @JoinTable to table1
4647
Column column1 = new Column();
47-
column1.setFieldName(table2.getClassName().toLowerCase());
48+
column1.setFieldName(CaseUtils.toCamelCase(table2.getName(), false, '_'));
4849
column1.setType(table2.getClassName());
4950
column1.getAnnotations().add(ManyToManyAnnotation.builder().build().toString());
50-
5151
Set<JoinColumnAnnotation> joinColumnAnnotations = new HashSet<>();
5252
for (String column : foreignKeyConstraintList.get(0).getColumns()) {
5353
joinColumnAnnotations.add(JoinColumnAnnotation.builder().name(column).build());
5454
}
55-
5655
Set<JoinColumnAnnotation> joinInverseColumnAnnotations = new HashSet<>();
5756
for (String column : foreignKeyConstraintList.get(1).getColumns()) {
5857
joinInverseColumnAnnotations.add(JoinColumnAnnotation.builder().name(column).build());
5958
}
60-
6159
column1.getAnnotations().add(JoinTableAnnotation.builder().tableName(table.getName()).joinColumns(joinColumnAnnotations).inverseJoinColumns(joinInverseColumnAnnotations).build().toString());
60+
table1.getColumns().add(column1);
6261

62+
//Adding @ManyToMany(mappedBy) to table2
6363
Column column2 = new Column();
64-
column2.setFieldName(table1.getClassName().toLowerCase());
64+
column2.setFieldName(CaseUtils.toCamelCase(table1.getName(), false, '_'));
6565
column2.setType(table1.getClassName());
66-
column2.getAnnotations().add(ManyToManyAnnotation.builder().mappedBy(column1.getFieldName()).toString());
66+
column2.getAnnotations().add(ManyToManyAnnotation.builder().mappedBy(column1.getFieldName()).build().toString());
67+
table2.getColumns().add(column2);
6768

6869
} else {
6970
//Case1: There are some fields that are not foreign keys. So separate entity is needed to track Link Table
@@ -80,13 +81,13 @@ public static void generateMappings(final Table table) {
8081
private static void addBothUnidirectionalMappings(Table table, ForeignKeyConstraint foreignKeyConstraint) {
8182
Table referencedTable = tablesMap.get(foreignKeyConstraint.getReferencedTableName());
8283
Column foreignKeyColumn = new Column();
83-
foreignKeyColumn.setFieldName(referencedTable.getClassName().toLowerCase());
84+
foreignKeyColumn.setFieldName(CaseUtils.toCamelCase(referencedTable.getName(), false, '_'));
8485
foreignKeyColumn.setType(referencedTable.getClassName());
8586
foreignKeyColumn.getAnnotations().add(new ManyToOneAnnotation().toString());
8687
table.getColumns().add(foreignKeyColumn);
8788

8889
Column childKeyColumn = new Column();
89-
childKeyColumn.setFieldName(table.getClassName().toLowerCase());
90+
childKeyColumn.setFieldName(CaseUtils.toCamelCase(table.getName(), false, '_'));
9091
childKeyColumn.setType("Set<" + table.getClassName() + ">");
9192
childKeyColumn.getAnnotations().add(OneToManyAnnotation.builder().mappedBy(foreignKeyColumn.getFieldName()).build().toString());
9293
referencedTable.getColumns().add(childKeyColumn);

src/main/java/org/ngbsn/model/annotations/fieldAnnotations/JoinTableAnnotation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public class JoinTableAnnotation implements Annotation {
1616
public String toString() {
1717
return "@JoinTable(name = \"" + tableName
1818
+ "\", "
19-
+ "joinColumns = {\""
19+
+ "joinColumns = {"
2020
+ joinColumns.stream().map(JoinColumnAnnotation::toString).collect(Collectors.joining("," + System.lineSeparator()))
2121
+ "}, "
22-
+ "inverseJoinColumns = {\""
22+
+ "inverseJoinColumns = {"
2323
+ inverseJoinColumns.stream().map(JoinColumnAnnotation::toString).collect(Collectors.joining("," + System.lineSeparator()))
2424
+ "})";
2525
}

src/test/resources/sql/organization.sql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CREATE TABLE organization.contractors (
33
birth_date DATE NOT NULL,
44
first_name VARCHAR(14) NOT NULL,
55
last_name VARCHAR(16) NOT NULL,
6-
hire_date DATE NOT NULL,
6+
hire_date DATE,
77
PRIMARY KEY (contractor_no, first_name)
88
);
99

@@ -26,8 +26,6 @@ CREATE TABLE organization.departments (
2626
CREATE TABLE organization.dept_manager (
2727
dept_no CHAR(4) NOT NULL,
2828
emp_no INT NOT NULL,
29-
from_date DATE NOT NULL,
30-
to_date DATE NOT NULL,
3129
FOREIGN KEY (emp_no) REFERENCES organization.employees (emp_no) ON DELETE CASCADE,
3230
FOREIGN KEY (dept_no) REFERENCES organization.departments (dept_no) ON DELETE CASCADE,
3331
PRIMARY KEY (emp_no,dept_no)

0 commit comments

Comments
 (0)