org.hibernate.cfg.reveng.ForeignKeysInfo Maven / Gradle / Ivy
package org.hibernate.cfg.reveng;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.Table;
public class ForeignKeysInfo {
final Map dependentTables;
final Map> dependentColumns;
final Map> referencedColumns;
private final Table referencedTable;
public ForeignKeysInfo(
Table referencedTable,
Map tables,
Map> columns,
Map> refColumns) {
this.referencedTable = referencedTable;
this.dependentTables = tables;
this.dependentColumns = columns;
this.referencedColumns = refColumns;
}
Map> process(ReverseEngineeringStrategy revengStrategy) {
Map> oneToManyCandidates = new HashMap>();
Iterator> iterator = dependentTables.entrySet().iterator();
while (iterator.hasNext() ) {
Entry entry = iterator.next();
String fkName = entry.getKey();
Table fkTable = entry.getValue();
List columns = dependentColumns.get(fkName);
List refColumns = referencedColumns.get(fkName);
String className = revengStrategy.tableToClassName(TableIdentifier.create(referencedTable) );
ForeignKey key = fkTable.createForeignKey(fkName, columns, className, null, refColumns);
key.setReferencedTable(referencedTable);
addToMultiMap(oneToManyCandidates, className, key);
}
return oneToManyCandidates;
}
private void addToMultiMap(Map> multimap, String key, ForeignKey item) {
List existing = multimap.get(key);
if(existing == null) {
existing = new ArrayList();
multimap.put(key, existing);
}
existing.add(item);
}
}