diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Cache71Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Cache71Dialect.java index 702b72153178..c2811e3b423e 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Cache71Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Cache71Dialect.java @@ -401,11 +401,6 @@ public boolean qualifyIndexName() { return false; } - public boolean supportsUnique() { - // Does this dialect support the UNIQUE column syntax? - return true; - } - /** * The syntax used to add a foreign key constraint to a table. * diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java index e4bc9bdfc83c..9adc838f0be1 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java @@ -216,10 +216,6 @@ public String getForUpdateString() { return " for update"; } - public boolean supportsUnique() { - return true; - } - public boolean supportsLimit() { return true; } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/HSQLDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/HSQLDialect.java index d0b176fa9fa1..f74b900dd5fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/HSQLDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/HSQLDialect.java @@ -246,10 +246,6 @@ public String getForUpdateString() { return ""; } - public boolean supportsUnique() { - return false; - } - public boolean supportsLimit() { return true; } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/IngresDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/IngresDialect.java index a7dbaf194c28..e5b9f8a20eea 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/IngresDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/IngresDialect.java @@ -306,13 +306,6 @@ public boolean useMaxForLimit() { return true; } - /** - * Ingres explicitly needs "unique not null", because "with null" is default - */ - public boolean supportsNotNullUnique() { - return false; - } - /** * Does this dialect support temporary tables? */ diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/RDMSOS2200Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/RDMSOS2200Dialect.java index 6d45c1c6d583..ccec37a7c7a1 100755 --- a/hibernate-core/src/main/java/org/hibernate/dialect/RDMSOS2200Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/RDMSOS2200Dialect.java @@ -240,13 +240,6 @@ public String getForUpdateString() { return ""; // Original Dialect.java returns " for update"; } - /** - * RDMS does not support adding Unique constraints via create and alter table. - */ - public boolean supportsUniqueConstraintInCreateAlterTable() { - return true; - } - // Verify the state of this new method in Hibernate 3.0 Dialect.java /** * RDMS does not support Cascade Deletes. diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseASE15Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseASE15Dialect.java index 423ced7cb768..01f8d25776db 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseASE15Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseASE15Dialect.java @@ -422,10 +422,6 @@ public boolean supportsExpectedLobUsagePattern() { return false; } - public boolean supportsUniqueConstraintInCreateAlterTable() { - return false; - } - public String getCrossJoinSeparator() { return ", "; } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/TimesTenDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/TimesTenDialect.java index b2f611aeb278..f76e3099a102 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/TimesTenDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/TimesTenDialect.java @@ -102,14 +102,6 @@ public boolean dropConstraints() { public boolean qualifyIndexName() { return false; } - - public boolean supportsUnique() { - return false; - } - - public boolean supportsUniqueConstraintInCreateAlterTable() { - return false; - } public String getAddColumnString() { return "add"; diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java b/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java index 5d9a34881e26..6c872434b76e 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java @@ -44,20 +44,6 @@ public DefaultUniqueDelegate( Dialect dialect ) { @Override public String applyUniqueToColumn( org.hibernate.mapping.Table table, org.hibernate.mapping.Column column ) { -// if ( column.isUnique() -// && ( column.isNullable() -// || dialect.supportsNotNullUnique() ) ) { -// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { -// // If the constraint is supported, do not add to the column syntax. -// UniqueKey uk = getOrCreateUniqueKey( column.getQuotedName( dialect ) + '_' ); -// uk.addColumn( column ); -// } -// else if ( dialect.supportsUnique() ) { -// // Otherwise, add to the column syntax if supported. -// sb.append( " unique" ); -// } -// } - org.hibernate.mapping.UniqueKey uk = table.getOrCreateUniqueKey( column.getQuotedName( dialect ) + '_' ); uk.addColumn( column ); @@ -66,20 +52,6 @@ public String applyUniqueToColumn( org.hibernate.mapping.Table table, @Override public String applyUniqueToColumn( Table table, Column column ) { -// if ( column.isUnique() -// && ( column.isNullable() -// || dialect.supportsNotNullUnique() ) ) { -// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { -// // If the constraint is supported, do not add to the column syntax. -// UniqueKey uk = getOrCreateUniqueKey( column.getQuotedName( dialect ) + '_' ); -// uk.addColumn( column ); -// } -// else if ( dialect.supportsUnique() ) { -// // Otherwise, add to the column syntax if supported. -// sb.append( " unique" ); -// } -// } - UniqueKey uk = table.getOrCreateUniqueKey( column.getColumnName() .encloseInQuotesIfQuoted( dialect ) + '_' ); uk.addColumn( column ); @@ -88,41 +60,19 @@ public String applyUniqueToColumn( Table table, Column column ) { @Override public String applyUniquesToTable( org.hibernate.mapping.Table table ) { - // TODO: Am I correct that this shouldn't be done unless the constraint - // isn't created in an alter table? -// Iterator uniqueKeyIterator = table.getUniqueKeyIterator(); -// while ( uniqueKeyIterator.hasNext() ) { -// UniqueKey uniqueKey = (UniqueKey) uniqueKeyIterator.next(); -// -// sb.append( ", " ).append( createUniqueConstraint( uniqueKey) ); -// } return ""; } @Override public String applyUniquesToTable( Table table ) { - // TODO: Am I correct that this shouldn't be done unless the constraint - // isn't created in an alter table? -// Iterator uniqueKeyIterator = table.getUniqueKeyIterator(); -// while ( uniqueKeyIterator.hasNext() ) { -// UniqueKey uniqueKey = (UniqueKey) uniqueKeyIterator.next(); -// -// sb.append( ", " ).append( createUniqueConstraint( uniqueKey) ); -// } return ""; } @Override public String applyUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey, String defaultCatalog, String defaultSchema ) { -// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { -// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema ); -// } -// else { -// return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true, -// defaultCatalog, defaultSchema ); -// } - + // Do this here, rather than allowing UniqueKey/Constraint to do it. + // We need full, simplified control over whether or not it happens. return new StringBuilder( "alter table " ) .append( uniqueKey.getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema ) ) @@ -134,15 +84,9 @@ public String applyUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey, @Override public String applyUniquesOnAlter( UniqueKey uniqueKey ) { -// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { -// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema ); -// } -// else { -// return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true, -// defaultCatalog, defaultSchema ); -// } - - return new StringBuilder( "alter table " ) + // Do this here, rather than allowing UniqueKey/Constraint to do it. + // We need full, simplified control over whether or not it happens. + return new StringBuilder( "alter table " ) .append( uniqueKey.getTable().getQualifiedName( dialect ) ) .append( " add constraint " ) .append( uniqueKey.getName() ) @@ -153,13 +97,8 @@ public String applyUniquesOnAlter( UniqueKey uniqueKey ) { @Override public String dropUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey, String defaultCatalog, String defaultSchema ) { -// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { -// return super.sqlDropString( dialect, defaultCatalog, defaultSchema ); -// } -// else { -// return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog, defaultSchema ); -// } - + // Do this here, rather than allowing UniqueKey/Constraint to do it. + // We need full, simplified control over whether or not it happens. return new StringBuilder( "alter table " ) .append( uniqueKey.getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema ) ) @@ -170,13 +109,8 @@ public String dropUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey, @Override public String dropUniquesOnAlter( UniqueKey uniqueKey ) { -// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { -// return super.sqlDropString( dialect, defaultCatalog, defaultSchema ); -// } -// else { -// return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog, defaultSchema ); -// } - + // Do this here, rather than allowing UniqueKey/Constraint to do it. + // We need full, simplified control over whether or not it happens. return new StringBuilder( "alter table " ) .append( uniqueKey.getTable().getQualifiedName( dialect ) ) .append( " drop constraint " ) @@ -186,10 +120,6 @@ public String dropUniquesOnAlter( UniqueKey uniqueKey ) { @Override public String uniqueConstraintSql( org.hibernate.mapping.UniqueKey uniqueKey ) { - // TODO: This may not be necessary, but not all callers currently - // check it on their own. Go through their logic. -// if ( !isGenerated( dialect ) ) return null; - StringBuilder sb = new StringBuilder(); sb.append( " unique (" ); Iterator columnIterator = uniqueKey.getColumnIterator(); @@ -207,10 +137,6 @@ public String uniqueConstraintSql( org.hibernate.mapping.UniqueKey uniqueKey ) { @Override public String uniqueConstraintSql( UniqueKey uniqueKey ) { - // TODO: This may not be necessary, but not all callers currently - // check it on their own. Go through their logic. -// if ( !isGenerated( dialect ) ) return null; - StringBuilder sb = new StringBuilder(); sb.append( " unique (" ); Iterator columnIterator = uniqueKey.getColumns().iterator(); diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/unique/UniqueDelegate.java b/hibernate-core/src/main/java/org/hibernate/dialect/unique/UniqueDelegate.java index 042f0c8c3c7f..d5ff514649e3 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/unique/UniqueDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/unique/UniqueDelegate.java @@ -28,12 +28,16 @@ * Dialect-level delegate in charge of applying "uniqueness" to a column. * Uniqueness can be defined in 1 of 3 ways: * - * 1.) Add a unique constraint via separate create/alter table statements. + * 1.) Add a unique constraint via separate alter table statements. * 2.) Add a unique constraint via dialect-specific syntax in table create statement. * 3.) Add "unique" syntax to the column itself. * * #1 & #2 are preferred, if possible -- #3 should be solely a fall-back. * + * TODO: This could eventually be simplified. With AST, 1 "applyUniqueness" + * method might be possible. But due to .cfg and .mapping still resolving + * around StringBuilders, separate methods were needed. + * * See HHH-7797. * * @author Brett Meyer @@ -43,7 +47,8 @@ public interface UniqueDelegate { /** * If the delegate supports unique constraints, this method should simply * create the UniqueKey on the Table. Otherwise, the constraint isn't - * supported and "unique" should be added to the column definition. + * supported and "unique" should be returned in order to add it + * to the column definition. * * @param table * @param column @@ -55,7 +60,8 @@ public String applyUniqueToColumn( org.hibernate.mapping.Table table, /** * If the delegate supports unique constraints, this method should simply * create the UniqueKey on the Table. Otherwise, the constraint isn't - * supported and "unique" should be added to the column definition. + * supported and "unique" should be returned in order to add it + * to the column definition. * * @param table * @param column @@ -64,9 +70,9 @@ public String applyUniqueToColumn( org.hibernate.mapping.Table table, public String applyUniqueToColumn( Table table, Column column ); /** - * If creating unique constraints in separate alter statements are not - * supported, this method should return the syntax necessary to create - * the constraint on the original create table statement. + * If constraints are supported, but not in seperate alter statements, + * return uniqueConstraintSql in order to add the constraint to the + * original table definition. * * @param table * @return String @@ -74,9 +80,9 @@ public String applyUniqueToColumn( org.hibernate.mapping.Table table, public String applyUniquesToTable( org.hibernate.mapping.Table table ); /** - * If creating unique constraints in separate alter statements are not - * supported, this method should return the syntax necessary to create - * the constraint on the original create table statement. + * If constraints are supported, but not in seperate alter statements, + * return uniqueConstraintSql in order to add the constraint to the + * original table definition. * * @param table * @return String diff --git a/hibernate-core/src/test/java/org/hibernate/test/constraint/ConstraintTest.java b/hibernate-core/src/test/java/org/hibernate/test/constraint/ConstraintTest.java index 936f61c73c1d..5685d986d8b8 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/constraint/ConstraintTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/constraint/ConstraintTest.java @@ -20,18 +20,23 @@ */ package org.hibernate.test.constraint; -import javax.persistence.Column; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.Table; +import org.hibernate.mapping.Column; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; /** - * HHH-7797 re-wrote the way dialects handle unique constraints. Test as - * many variations of unique, not null, and primary key constraints as possible. + * HHH-7797 re-wrote the way dialects handle unique constraints. Test + * variations of unique & not null to ensure the constraints are created + * correctly for each dialect. * * @author Brett Meyer */ @@ -47,18 +52,34 @@ protected Class[] getAnnotatedClasses() { @Test public void testConstraints() { - // nothing yet -- more interested in DDL creation + Column column = (Column) configuration().getClassMapping( Entity1.class.getName() ) + .getProperty( "foo1" ).getColumnIterator().next(); + assertFalse( column.isNullable() ); + assertTrue( column.isUnique() ); + + column = (Column) configuration().getClassMapping( Entity1.class.getName() ) + .getProperty( "foo2" ).getColumnIterator().next(); + assertTrue( column.isNullable() ); + assertTrue( column.isUnique() ); + + column = (Column) configuration().getClassMapping( Entity1.class.getName() ) + .getProperty( "id" ).getColumnIterator().next(); + assertFalse( column.isNullable() ); + assertTrue( column.isUnique() ); } - // Primary key w/ not null and unique @Entity + @Table( name = "Entity1" ) public static class Entity1 { @Id @GeneratedValue -// @Column( nullable = false, unique = true) + @javax.persistence.Column( nullable = false, unique = true) public long id; - @Column( nullable = false, unique = true) - public String foo; + @javax.persistence.Column( nullable = false, unique = true) + public String foo1; + + @javax.persistence.Column( nullable = true, unique = true) + public String foo2; } } \ No newline at end of file