Skip to content

Commit 379c46c

Browse files
committed
HHH-8178 Natural IDs generate one UniqueKey for all columns
1 parent 9c0c2d7 commit 379c46c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,12 +2096,12 @@ else if ( !isId || !entityBinder.isIgnoreIdAnnotations() ) {
20962096
if ( naturalIdAnn != null ) {
20972097
if ( joinColumns != null ) {
20982098
for ( Ejb3Column column : joinColumns ) {
2099-
column.addUniqueKey( StringHelper.randomFixedLengthHex("UK_"), inSecondPass );
2099+
column.addUniqueKey( column.getTable().getNaturalIdUniqueKeyName(), inSecondPass );
21002100
}
21012101
}
21022102
else {
21032103
for ( Ejb3Column column : columns ) {
2104-
column.addUniqueKey( StringHelper.randomFixedLengthHex("UK_"), inSecondPass );
2104+
column.addUniqueKey( column.getTable().getNaturalIdUniqueKeyName(), inSecondPass );
21052105
}
21062106
}
21072107
}

hibernate-core/src/main/java/org/hibernate/mapping/Table.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public class Table implements RelationalModel, Serializable {
6969
private boolean isAbstract;
7070
private boolean hasDenormalizedTables = false;
7171
private String comment;
72+
73+
/**
74+
* Natural ID columns must reside in one single UniqueKey within the Table.
75+
* To prevent separate UniqueKeys from being created, this keeps track of
76+
* a sole name used for all of them. It's necessary since
77+
* AnnotationBinder#processElementAnnotations (static) creates the
78+
* UniqueKeys on a second pass using randomly-generated names.
79+
*/
80+
private final String naturalIdUniqueKeyName = StringHelper.randomFixedLengthHex( "UK_" );
7281

7382
static class ForeignKeyKey implements Serializable {
7483
String referencedClassName;
@@ -817,6 +826,10 @@ public void setComment(String comment) {
817826
public Iterator getCheckConstraintsIterator() {
818827
return checkConstraints.iterator();
819828
}
829+
830+
public String getNaturalIdUniqueKeyName() {
831+
return naturalIdUniqueKeyName;
832+
}
820833

821834
public Iterator sqlCommentStrings(Dialect dialect, String defaultCatalog, String defaultSchema) {
822835
List comments = new ArrayList();

0 commit comments

Comments
 (0)