Skip to content

Commit 7fc71cb

Browse files
committed
Fix issues related to Embeddable classes and add logic to set updatable,insertable = false
1 parent 9c19d7a commit 7fc71cb

File tree

9 files changed

+111
-56
lines changed

9 files changed

+111
-56
lines changed

src/main/java/io/github/ngbsn/generator/AssociationMappingsGenerator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.Map;
1010
import java.util.Optional;
1111

12-
import static io.github.ngbsn.generator.UniDirectionalMappingsGenerator.addBothSideUniDirectionalMappings;
12+
import static io.github.ngbsn.generator.OneToManyMappingsGenerator.addBiDirectionalMappings;
1313

1414
/**
1515
* Generate both UniDirectional and BiDirectional association mappings for all tables
@@ -33,7 +33,7 @@ static void generateMappings() {
3333
//Case: There is only 1 Foreign key or 1 Composite Foreign Key
3434
//Treat this as a regular table and add a new column with for parent field with @ManyToOne
3535
//and add a new column in ReferencedTable for child field with @OneToMany
36-
addBothSideUniDirectionalMappings(table, foreignKeyConstraintList.get(0));
36+
addBiDirectionalMappings(table, foreignKeyConstraintList.get(0));
3737

3838
} else {
3939
//Case: There are multiple Foreign Keys or multiple Composite Foreign Keys
@@ -44,13 +44,13 @@ static void generateMappings() {
4444
//Case: All fields are foreign keys. Also, the relation exits between 2 entities only
4545
//Remove this link entity from the tablesMap as separate entity is not needed to track Link Table. Use @ManyToMany on other 2 entities
4646
it.remove();
47-
BiDirectionalMappingsGenerator.addBiDirectionalMappings(table, foreignKeyConstraintList);
47+
ManyToManyMappingsGenerator.addManyToManyMappings(table, foreignKeyConstraintList);
4848

4949
} else {
5050
//Case1: There are some fields that are not foreign keys. So separate entity is needed to track Link Table
5151
//Case2: All fields are foreign keys. But, the relation exits between 2 or more entities
5252
//Add @ManyToOne for each foreignKey and corresponding @OneToMany in referenced Table
53-
foreignKeyConstraintList.forEach(foreignKeyConstraint -> addBothSideUniDirectionalMappings(table, foreignKeyConstraint));
53+
foreignKeyConstraintList.forEach(foreignKeyConstraint -> addBiDirectionalMappings(table, foreignKeyConstraint));
5454
}
5555
}
5656
}

src/main/java/io/github/ngbsn/generator/BiDirectionalMappingsGenerator.java renamed to src/main/java/io/github/ngbsn/generator/ManyToManyMappingsGenerator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
/**
1515
* Generate BiDirectional Mappings (many-to-many) for a specific table
1616
*/
17-
class BiDirectionalMappingsGenerator {
17+
class ManyToManyMappingsGenerator {
1818

19-
private BiDirectionalMappingsGenerator() {
19+
private ManyToManyMappingsGenerator() {
2020
}
2121

2222
/**
@@ -25,14 +25,14 @@ private BiDirectionalMappingsGenerator() {
2525
* @param table The table to be processed
2626
* @param foreignKeyConstraintList List of generated foreignKeyConstraintList models
2727
*/
28-
static void addBiDirectionalMappings(final Table table, final List<ForeignKeyConstraint> foreignKeyConstraintList) {
28+
static void addManyToManyMappings(final Table table, final List<ForeignKeyConstraint> foreignKeyConstraintList) {
2929
Table table1 = ModelGenerator.getTablesMap().get(foreignKeyConstraintList.get(0).getReferencedTableName().replaceAll("[\"']", ""));
3030
Table table2 = ModelGenerator.getTablesMap().get(foreignKeyConstraintList.get(1).getReferencedTableName().replaceAll("[\"']", ""));
3131

3232
//Adding @ManyToMany and @JoinTable to table1
3333
Column column1 = new Column();
3434
column1.setFieldName(Util.convertSnakeCaseToCamelCase(table2.getTableName(), false));
35-
column1.setType(table2.getClassName());
35+
column1.setType("Set<" + table2.getClassName() + ">");
3636
column1.getAnnotations().add(ManyToManyAnnotation.builder().build().toString());
3737
List<JoinColumnAnnotation> joinColumnAnnotations = new ArrayList<>();
3838
for (String column : foreignKeyConstraintList.get(0).getColumns()) {
@@ -48,7 +48,7 @@ static void addBiDirectionalMappings(final Table table, final List<ForeignKeyCon
4848
//Adding @ManyToMany(mappedBy) to table2
4949
Column column2 = new Column();
5050
column2.setFieldName(Util.convertSnakeCaseToCamelCase(table1.getTableName(), false));
51-
column2.setType(table1.getClassName());
51+
column2.setType("Set<" + table1.getClassName() + ">");
5252
column2.getAnnotations().add(ManyToManyAnnotation.builder().mappedBy(column1.getFieldName()).build().toString());
5353
table2.getColumns().add(column2);
5454
}

src/main/java/io/github/ngbsn/generator/ModelGenerator.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ private static void extractPrimaryKeys(final Index primaryKeyIndex, final Table
202202
EmbeddableClass embeddedId = new EmbeddableClass();
203203
embeddedId.setClassName(table.getClassName() + "PK");
204204
embeddedId.setFieldName(Util.convertSnakeCaseToCamelCase(table.getTableName(), false) + "PK");
205-
embeddedId.setEmbeddedId(true);
206-
table.getEmbeddableClasses().add(embeddedId);
205+
table.setEmbeddedId(embeddedId);
207206

208207
//remove the primary keys columns from table and add inside EmbeddedId
209208
primaryKeyColumns.forEach(column -> {
@@ -225,11 +224,11 @@ private static void extractColumns(final Table table, final CreateTable parsedTa
225224
parsedTable.getColumnDefinitions().forEach(columnDefinition -> {
226225
Column column = new Column();
227226
columns.add(column);
228-
List<String> columnAnnotations = new ArrayList<>();
229-
column.setAnnotations(columnAnnotations);
227+
List<String> fieldAnnotations = new ArrayList<>();
228+
column.setAnnotations(fieldAnnotations);
230229
column.setColumnName(columnDefinition.getColumnName().replaceAll(REGEX_ALL_QUOTES, ""));
231230
//Adding @Column
232-
columnAnnotations.add(ColumnAnnotation.builder().columnName(column.getColumnName()).build().toString());
231+
fieldAnnotations.add(ColumnAnnotation.builder().columnName(column.getColumnName()).build().toString());
233232
String fieldName = Util.convertSnakeCaseToCamelCase(column.getColumnName(), false);
234233
fieldName = SourceVersion.isKeyword(fieldName) ? fieldName + table.getClassName() : fieldName;
235234
column.setFieldName(fieldName);
@@ -242,7 +241,7 @@ private static void extractColumns(final Table table, final CreateTable parsedTa
242241
values.add(s.replaceAll(REGEX_ALL_QUOTES, ""));
243242
}
244243
column.setType(tableEnum.getEnumName());
245-
columnAnnotations.add(EnumeratedAnnotation.builder().value(EnumType.STRING).build().toString());
244+
fieldAnnotations.add(EnumeratedAnnotation.builder().value(EnumType.STRING).build().toString());
246245
} else {
247246
String mappedJavaType = SQLTypeToJpaTypeMapping.getTypeMapping(columnDefinition.getColDataType().getDataType());
248247
column.setType(Objects.requireNonNullElse(mappedJavaType, "Object"));
@@ -252,7 +251,7 @@ private static void extractColumns(final Table table, final CreateTable parsedTa
252251
if (columnDefinition.getColumnSpecs() != null) {
253252
String constraints = String.join(" ", columnDefinition.getColumnSpecs());
254253
if (constraints.contains("NOT NULL")) {
255-
columnAnnotations.add(NotNullAnnotation.builder().build().toString());
254+
fieldAnnotations.add(NotNullAnnotation.builder().build().toString());
256255
}
257256
}
258257
});

0 commit comments

Comments
 (0)