HHH-8026 Duplicate constraint names with unique=true

This commit is contained in:
Brett Meyer 2013-02-27 15:18:16 -05:00
parent 3335710a38
commit 8515ce197a
3 changed files with 71 additions and 7 deletions

View File

@ -1377,7 +1377,7 @@ public class Configuration implements Serializable {
for ( UniqueConstraintHolder holder : uniqueConstraints ) {
uniqueIndexPerTable++;
final String keyName = StringHelper.isEmpty( holder.getName() )
? "key" + uniqueIndexPerTable
? "UK_" + table.getName() + "_" + uniqueIndexPerTable
: holder.getName();
buildUniqueKeyFromColumnNames( table, keyName, holder.getColumns() );
}

View File

@ -393,6 +393,9 @@ public class Table implements RelationalModel, Serializable {
Iterator iter = getColumnIterator();
List results = new ArrayList();
int uniqueIndexInteger = 0;
while ( iter.hasNext() ) {
Column column = (Column) iter.next();
@ -419,8 +422,9 @@ public class Table implements RelationalModel, Serializable {
}
if ( column.isUnique() ) {
uniqueIndexInteger++;
UniqueKey uk = getOrCreateUniqueKey(
column.getQuotedName( dialect ) + '_' );
"UK_" + name + "_" + uniqueIndexInteger);
uk.addColumn( column );
alter.append( dialect.getUniqueDelegate()
.applyUniqueToColumn( column ) );
@ -489,6 +493,7 @@ public class Table implements RelationalModel, Serializable {
}
Iterator iter = getColumnIterator();
int uniqueIndexInteger = 0;
while ( iter.hasNext() ) {
Column col = (Column) iter.next();
@ -522,8 +527,9 @@ public class Table implements RelationalModel, Serializable {
}
if ( col.isUnique() ) {
uniqueIndexInteger++;
UniqueKey uk = getOrCreateUniqueKey(
col.getQuotedName( dialect ) + '_' );
"uc_" + name + "_" + uniqueIndexInteger);
uk.addColumn( col );
buf.append( dialect.getUniqueDelegate()
.applyUniqueToColumn( col ) );
@ -619,7 +625,7 @@ public class Table implements RelationalModel, Serializable {
}
public UniqueKey createUniqueKey(List keyColumns) {
String keyName = "UK" + uniqueColumnString( keyColumns.iterator() );
String keyName = "UK_" + uniqueColumnString( keyColumns.iterator() );
UniqueKey uk = getOrCreateUniqueKey( keyName );
uk.addColumns( keyColumns.iterator() );
return uk;

View File

@ -1,10 +1,20 @@
package org.hibernate.test.annotations.uniqueconstraint;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import java.util.Iterator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
@ -12,12 +22,14 @@ import org.junit.Test;
* @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
*/
public class UniqueConstraintTest extends BaseCoreFunctionalTestCase {
protected Class[] getAnnotatedClasses() {
protected Class[] getAnnotatedClasses() {
return new Class[]{
Room.class,
Building.class,
House.class
House.class,
UniqueNoNameA.class,
UniqueNoNameB.class
};
}
@ -52,5 +64,51 @@ public class UniqueConstraintTest extends BaseCoreFunctionalTestCase {
tx.rollback();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-8026" )
public void testUnNamedConstraints() {
Iterator<org.hibernate.mapping.Table> iterator = configuration().getTableMappings();
org.hibernate.mapping.Table tableA = null;
org.hibernate.mapping.Table tableB = null;
while( iterator.hasNext() ) {
org.hibernate.mapping.Table table = iterator.next();
if ( table.getName().equals( "UniqueNoNameA" ) ) {
tableA = table;
}
else if ( table.getName().equals( "UniqueNoNameB" ) ) {
tableB = table;
}
}
if ( tableA == null || tableB == null ) {
fail( "Could not find the expected tables." );
}
assertFalse( tableA.getUniqueKeyIterator().next().getName().equals(
tableB.getUniqueKeyIterator().next().getName() ) );
}
@Entity
@Table( name = "UniqueNoNameA",
uniqueConstraints = {@UniqueConstraint(columnNames={"name"})})
public static class UniqueNoNameA {
@Id
@GeneratedValue
public long id;
public String name;
}
@Entity
@Table( name = "UniqueNoNameB",
uniqueConstraints = {@UniqueConstraint(columnNames={"name"})})
public static class UniqueNoNameB {
@Id
@GeneratedValue
public long id;
public String name;
}
}