Skip to content

Commit 5c8d576

Browse files
committed
HBX-3187: Improve the implementation of PrimaryKeyProcessor
Signed-off-by: Koen Aers <koen.aers@gmail.com>
1 parent 6ec5140 commit 5c8d576

File tree

4 files changed

+177
-212
lines changed

4 files changed

+177
-212
lines changed

orm/src/main/java/org/hibernate/tool/internal/reveng/reader/PrimaryKeyProcessor.java

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package org.hibernate.tool.internal.reveng.reader;
22

3-
import java.util.ArrayList;
4-
import java.util.Collections;
5-
import java.util.Comparator;
6-
import java.util.Iterator;
7-
import java.util.List;
8-
import java.util.Map;
3+
import java.util.*;
94

105
import org.hibernate.JDBCException;
116
import org.hibernate.mapping.Column;
@@ -40,30 +35,25 @@ public static void processPrimaryKey(
4035
while (primaryKeyIterator.hasNext() ) {
4136
primaryKeyRs = primaryKeyIterator.next();
4237

43-
/*String ownCatalog = primaryKeyRs.getString("TABLE_CAT");
44-
String ownSchema = primaryKeyRs.getString("TABLE_SCHEM");
45-
String ownTable = primaryKeyRs.getString("TABLE_NAME");*/
46-
4738
String columnName = (String) primaryKeyRs.get("COLUMN_NAME");
48-
short seq = ((Short)primaryKeyRs.get("KEY_SEQ")).shortValue();
39+
short seq = (Short) primaryKeyRs.get("KEY_SEQ");
4940
String name = (String) primaryKeyRs.get("PK_NAME");
5041

5142
if(key==null) {
5243
key = new PrimaryKey(table);
5344
key.setName(name);
54-
key.setTable(table);
5545
if(table.getPrimaryKey()!=null) {
5646
throw new RuntimeException(table + " already has a primary key!"); //TODO: ignore ?
5747
}
5848
table.setPrimaryKey(key);
5949
}
6050
else {
61-
if(!(name==key.getName() ) && name!=null && !name.equals(key.getName() ) ) {
51+
if(!(Objects.equals(name, key.getName())) && name!=null && !name.equals(key.getName() ) ) {
6252
throw new RuntimeException("Duplicate names found for primarykey. Existing name: " + key.getName() + " JDBC name: " + name + " on table " + table);
6353
}
6454
}
6555

66-
columns.add(new Object[] { Short.valueOf(seq), columnName});
56+
columns.add(new Object[] {seq, columnName});
6757
}
6858
} finally {
6959
if (primaryKeyIterator!=null) {
@@ -75,28 +65,22 @@ public static void processPrimaryKey(
7565
}
7666
}
7767

78-
// sort the columns accoring to the key_seq.
79-
Collections.sort(columns,new Comparator<Object[]>() {
80-
public int compare(Object[] o1, Object[] o2) {
81-
Short left = (Short)o1[0];
82-
Short right = (Short)o2[0];
83-
return left.compareTo(right);
84-
}
85-
});
68+
columns.sort((o1, o2) -> {
69+
Short left = (Short) o1[0];
70+
Short right = (Short) o2[0];
71+
return left.compareTo(right);
72+
});
8673

8774
List<String> t = new ArrayList<String>(columns.size());
88-
Iterator<?> cols = columns.iterator();
89-
while (cols.hasNext() ) {
90-
Object[] element = (Object[]) cols.next();
91-
t.add((String)element[1]);
92-
}
75+
for (Object[] element : columns) {
76+
t.add((String) element[1]);
77+
}
9378

9479
if(key==null) {
9580
log.warn("The JDBC driver didn't report any primary key columns in " + table.getName() + ". Asking rev.eng. strategy" );
9681
List<String> userPrimaryKey = RevengUtils.getPrimaryKeyInfoInRevengStrategy(revengStrategy, table, defaultCatalog, defaultSchema); if(userPrimaryKey!=null && !userPrimaryKey.isEmpty()) {
9782
key = new PrimaryKey(table);
9883
key.setName(new Alias(15, "PK").toAliasString( table.getName()));
99-
key.setTable(table);
10084
if(table.getPrimaryKey()!=null) {
10185
throw new RuntimeException(table + " already has a primary key!"); //TODO: ignore ?
10286
}
@@ -131,13 +115,11 @@ public int compare(Object[] o1, Object[] o2) {
131115
}
132116

133117
if(key!=null) {
134-
cols = t.iterator();
135-
while (cols.hasNext() ) {
136-
String name = (String) cols.next();
137-
// should get column from table if it already exists!
138-
Column col = getColumn(metaDataDialect, table, name);
139-
key.addColumn(col);
140-
}
118+
for (String name : t) {
119+
// should get column from table if it already exists!
120+
Column col = getColumn(metaDataDialect, table, name);
121+
key.addColumn(col);
122+
}
141123
log.debug("primary key for " + table + " -> " + key);
142124
}
143125

orm/src/main/java/org/hibernate/tool/internal/reveng/strategy/AbstractStrategy.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public abstract class AbstractStrategy implements RevengStrategy {
2929

3030
static final private Logger log = Logger.getLogger(AbstractStrategy.class);
3131

32-
private static Set<String> AUTO_OPTIMISTICLOCK_COLUMNS;
32+
private static final Set<String> AUTO_OPTIMISTICLOCK_COLUMNS;
3333

3434
private RevengSettings settings = new RevengSettings(this);
3535

@@ -145,7 +145,7 @@ public String tableToClassName(TableIdentifier tableIdentifier) {
145145
String pkgName = settings.getDefaultPackageName();
146146
String className = toUpperCamelCase( tableIdentifier.getName() );
147147

148-
if(pkgName.length()>0) {
148+
if(!pkgName.isEmpty()) {
149149
return StringHelper.qualify(pkgName, className);
150150
}
151151
else {
@@ -187,7 +187,7 @@ public String getOptimisticLockColumnName(TableIdentifier identifier) {
187187

188188
public boolean useColumnForOptimisticLock(TableIdentifier identifier, String column) {
189189
if(settings.getDetectOptimsticLock()) {
190-
return AUTO_OPTIMISTICLOCK_COLUMNS.contains(column.toLowerCase())?true:false;
190+
return AUTO_OPTIMISTICLOCK_COLUMNS.contains(column.toLowerCase());
191191
} else {
192192
return false;
193193
}
@@ -221,11 +221,7 @@ public boolean isForeignKeyCollectionInverse(String name, Table foreignKeyTable,
221221
// if the reference column is the first one then we are inverse.
222222
Column column = foreignKeyTable.getColumn(0);
223223
Column fkColumn = (Column) referencedColumns.get(0);
224-
if(fkColumn.equals(column)) {
225-
return true;
226-
} else {
227-
return false;
228-
}
224+
return fkColumn.equals(column);
229225
}
230226
return true;
231227
}
@@ -242,26 +238,23 @@ public boolean isOneToOne(ForeignKey foreignKey) {
242238
if(settings.getDetectOneToOne()) {
243239
// add support for non-PK associations
244240
List<Column> fkColumns = foreignKey.getColumns();
245-
List<Column> pkForeignTableColumns = null;
246-
241+
List<Column> pkForeignTableColumns = Collections.emptyList();
242+
247243
if (foreignKey.getTable().hasPrimaryKey())
248244
pkForeignTableColumns = foreignKey.getTable().getPrimaryKey().getColumns();
249245

250-
boolean equals =
251-
fkColumns != null && pkForeignTableColumns != null
252-
&& fkColumns.size() == pkForeignTableColumns.size();
246+
boolean equals = fkColumns.size() == pkForeignTableColumns.size();
253247

254248
Iterator<Column> columns = foreignKey.getColumns().iterator();
255249
while (equals && columns.hasNext()) {
256-
Column fkColumn = (Column) columns.next();
257-
equals = equals && pkForeignTableColumns.contains(fkColumn);
250+
Column fkColumn = columns.next();
251+
equals = pkForeignTableColumns.contains(fkColumn);
258252
}
259-
260253
return equals;
261254
} else {
262255
return false;
263256
}
264-
}
257+
}
265258

266259
public boolean isManyToManyTable(Table table) {
267260
if(settings.getDetectManyToMany()) {
@@ -287,14 +280,11 @@ public boolean isManyToManyTable(Table table) {
287280
}
288281

289282
// tests that all columns are implied in the fks
290-
Set<Column> columns = new HashSet<Column>();
291-
for (Column column : table.getColumns()) {
292-
columns.add(column);
293-
}
283+
Set<Column> columns = new HashSet<>(table.getColumns());
294284

295285
for (ForeignKey fkey : table.getForeignKeys().values()) {
296286
if (columns.isEmpty()) break;
297-
columns.removeAll(fkey.getColumns());
287+
fkey.getColumns().forEach(columns::remove);
298288
}
299289

300290
return columns.isEmpty();

0 commit comments

Comments
 (0)