Skip to content

Commit 09b3bce

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

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
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

0 commit comments

Comments
 (0)