1- package io .github .ngbsn .generator ;
1+ package io .github .ngbsn .generator . associations ;
22
3+ import io .github .ngbsn .generator .models .ModelGenerator ;
34import io .github .ngbsn .model .Column ;
45import io .github .ngbsn .model .EmbeddableClass ;
56import io .github .ngbsn .model .ForeignKeyConstraint ;
@@ -30,7 +31,7 @@ private OneToManyMappingsGenerator() {
3031 * @param table Table model
3132 * @param foreignKeyConstraint ForeignKeyConstraint model
3233 */
33- static void addBiDirectionalMappings (final Table table , final ForeignKeyConstraint foreignKeyConstraint ) {
34+ public static void addBiDirectionalMappings (final Table table , final ForeignKeyConstraint foreignKeyConstraint ) {
3435 Table parentTable = ModelGenerator .getTablesMap ().get (foreignKeyConstraint .getReferencedTableName ().replaceAll ("[\" ']" , "" ));
3536
3637 //In the Child table, create a new column having field name as Parent(Referenced) Table, with @ManyTOne annotation
@@ -72,33 +73,33 @@ private static void handleSingleForeignKey(final Table table, final ForeignKeyCo
7273 //Get the foreign key column from the table.
7374 List <Column > listOfForeignKeyColumns = listOfForeignKeys (table , foreignKeyConstraint );
7475 Column foreignKeyColumn = !listOfForeignKeyColumns .isEmpty () ? listOfForeignKeyColumns .get (0 ) : null ;
75-
76- if ( foreignKeyColumn != null ) {
77- //Check if foreign key is also a primary key, by iterating through the primary key list
78- Optional < Column > optionalColumnPrimaryForeign = allPrimaryKeyColumns . stream ()
79- . filter ( column -> column . getColumnName () != null && column . getColumnName (). equals ( foreignKeyColumn . getColumnName ())). findFirst ();
80- optionalColumnPrimaryForeign . ifPresentOrElse (column -> {
81- //Case: Shared Primary key
82- //If foreign key is a primary key, don't remove it from table. Set SharedPrimaryKey as true
83- column . setSharedPrimaryKey ( true );
84-
85- //Remove existing column annotations and add again with updatable=false, insertable=false
86- //This is necessary as the column is inserted/updated through foreign key
87- column . getAnnotations (). removeIf ( s -> s . contains ( "@Column" ));
88- column . getAnnotations (). add ( ColumnAnnotation . builder ()
89- . columnName ( column . getColumnName ())
90- . updatable ( false )
91- . insertable ( false )
92- . build (). toString ());
93-
94- //Add a @MapsId annotation to the referenced table field
95- parentTableField . getAnnotations (). add ( MapsIdAnnotation . builder (). fieldName ( column . getFieldName ()) .build ().toString ());
96- }
97- , () ->
98- //If foreign key is not a primary key, then remove it from the table
99- table . getColumns (). remove ( foreignKeyColumn )
100- );
101- }
76+ if ( foreignKeyColumn == null ) throw new UnsupportedOperationException (); //some issue in the SQL
77+
78+ //Case: Shared Single Primary Key
79+ //Check if foreign key is also a primary key, by iterating through the primary key list
80+ Optional < Column > optionalColumnPrimaryForeign = allPrimaryKeyColumns . stream ()
81+ . filter (column -> column . getColumnName () != null && column . getColumnName (). equals ( foreignKeyColumn . getColumnName ())). findFirst ();
82+ optionalColumnPrimaryForeign . ifPresentOrElse ( column -> {
83+ //Case: Shared Primary key
84+ //If foreign key is a primary key, don't remove it from table. Set SharedPrimaryKey as true
85+ column . setSharedPrimaryKey ( true );
86+ //Add a @MapsId annotation to the referenced table field
87+ parentTableField . getAnnotations (). add ( MapsIdAnnotation . builder (). fieldName ( column . getFieldName ()). build (). toString ());
88+
89+ //Remove existing column annotations and add again with updatable=false, insertable=false
90+ //This is necessary as the column is inserted/updated through foreign key
91+ column . getAnnotations (). removeIf ( s -> s . contains ( "@Column" ));
92+ column . getAnnotations (). add ( ColumnAnnotation . builder ( )
93+ . columnName ( column . getColumnName ())
94+ . updatable ( false )
95+ . insertable ( false )
96+ .build ().toString ());
97+
98+ }
99+ , () ->
100+ //If foreign key is not a primary key, then remove it from the table
101+ table . getColumns (). remove ( foreignKeyColumn )
102+ );
102103
103104 //Add a @JoinColumn annotation for the referenced table field
104105 parentTableField .getAnnotations ().add (JoinColumnAnnotation .builder ()
@@ -118,6 +119,7 @@ private static void handleCompositeForeignKey(final Table table, final ForeignKe
118119 handleSharedCompositePrimaryKey (table , parentTableField , embeddableId , listOfForeignKeyColumns );
119120 } else {
120121 //Case1: There is no Composite primary key
122+ //TODO can part of Composite foreign key be a primary key. Is this applicable only to self referencing cases?
121123 //Case2: If composite foreign key is not inside the composite primary key, then remove it from the table
122124 listOfForeignKeyColumns .forEach (column -> table .getColumns ().remove (column ));
123125 }
0 commit comments