From 53f7b73adb88f19bd4e48667688b4298d96e2b9a Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Thu, 11 Apr 2013 14:18:58 -0400 Subject: [PATCH] HHH-8178 Natural IDs generate one UniqueKey for all columns --- .../java/org/hibernate/cfg/AnnotationBinder.java | 4 ++-- .../src/main/java/org/hibernate/mapping/Table.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 03751aa81b..3e11ef67bf 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -2106,12 +2106,12 @@ public final class AnnotationBinder { if ( naturalIdAnn != null ) { if ( joinColumns != null ) { for ( Ejb3Column column : joinColumns ) { - column.addUniqueKey( StringHelper.randomFixedLengthHex("UK_"), inSecondPass ); + column.addUniqueKey( column.getTable().getNaturalIdUniqueKeyName(), inSecondPass ); } } else { for ( Ejb3Column column : columns ) { - column.addUniqueKey( StringHelper.randomFixedLengthHex("UK_"), inSecondPass ); + column.addUniqueKey( column.getTable().getNaturalIdUniqueKeyName(), inSecondPass ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java index 05fbdd10aa..15aeee8f87 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java @@ -68,6 +68,15 @@ public class Table implements RelationalModel, Serializable { private boolean isAbstract; private boolean hasDenormalizedTables = false; private String comment; + + /** + * Natural ID columns must reside in one single UniqueKey within the Table. + * To prevent separate UniqueKeys from being created, this keeps track of + * a sole name used for all of them. It's necessary since + * AnnotationBinder#processElementAnnotations (static) creates the + * UniqueKeys on a second pass using randomly-generated names. + */ + private final String naturalIdUniqueKeyName = StringHelper.randomFixedLengthHex( "UK_" ); static class ForeignKeyKey implements Serializable { String referencedClassName; @@ -818,6 +827,10 @@ public class Table implements RelationalModel, Serializable { public Iterator getCheckConstraintsIterator() { return checkConstraints.iterator(); } + + public String getNaturalIdUniqueKeyName() { + return naturalIdUniqueKeyName; + } public Iterator sqlCommentStrings(Dialect dialect, String defaultCatalog, String defaultSchema) { List comments = new ArrayList();