HHH-7797 Finished auditing dialects. Cleanup and javadocs. Completed
uniqueness test.
This commit is contained in:
parent
962d1e580d
commit
49c8a8e4f0
|
@ -401,11 +401,6 @@ public class Cache71Dialect extends Dialect {
|
||||||
return false;
|
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.
|
* The syntax used to add a foreign key constraint to a table.
|
||||||
*
|
*
|
||||||
|
|
|
@ -216,10 +216,6 @@ public class H2Dialect extends Dialect {
|
||||||
return " for update";
|
return " for update";
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supportsUnique() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean supportsLimit() {
|
public boolean supportsLimit() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -251,10 +251,6 @@ public class HSQLDialect extends Dialect {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supportsUnique() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean supportsLimit() {
|
public boolean supportsLimit() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -306,13 +306,6 @@ public class IngresDialect extends Dialect {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Ingres explicitly needs "unique not null", because "with null" is default
|
|
||||||
*/
|
|
||||||
public boolean supportsNotNullUnique() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does this dialect support temporary tables?
|
* Does this dialect support temporary tables?
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -240,13 +240,6 @@ public class RDMSOS2200Dialect extends Dialect {
|
||||||
return ""; // Original Dialect.java returns " for update";
|
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
|
// Verify the state of this new method in Hibernate 3.0 Dialect.java
|
||||||
/**
|
/**
|
||||||
* RDMS does not support Cascade Deletes.
|
* RDMS does not support Cascade Deletes.
|
||||||
|
|
|
@ -422,10 +422,6 @@ public class SybaseASE15Dialect extends SybaseDialect {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supportsUniqueConstraintInCreateAlterTable() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCrossJoinSeparator() {
|
public String getCrossJoinSeparator() {
|
||||||
return ", ";
|
return ", ";
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,14 +102,6 @@ public class TimesTenDialect extends Dialect {
|
||||||
public boolean qualifyIndexName() {
|
public boolean qualifyIndexName() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supportsUnique() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean supportsUniqueConstraintInCreateAlterTable() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAddColumnString() {
|
public String getAddColumnString() {
|
||||||
return "add";
|
return "add";
|
||||||
|
|
|
@ -44,20 +44,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
@Override
|
@Override
|
||||||
public String applyUniqueToColumn( org.hibernate.mapping.Table table,
|
public String applyUniqueToColumn( org.hibernate.mapping.Table table,
|
||||||
org.hibernate.mapping.Column column ) {
|
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(
|
org.hibernate.mapping.UniqueKey uk = table.getOrCreateUniqueKey(
|
||||||
column.getQuotedName( dialect ) + '_' );
|
column.getQuotedName( dialect ) + '_' );
|
||||||
uk.addColumn( column );
|
uk.addColumn( column );
|
||||||
|
@ -66,20 +52,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String applyUniqueToColumn( Table table, Column column ) {
|
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()
|
UniqueKey uk = table.getOrCreateUniqueKey( column.getColumnName()
|
||||||
.encloseInQuotesIfQuoted( dialect ) + '_' );
|
.encloseInQuotesIfQuoted( dialect ) + '_' );
|
||||||
uk.addColumn( column );
|
uk.addColumn( column );
|
||||||
|
@ -88,41 +60,19 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String applyUniquesToTable( org.hibernate.mapping.Table table ) {
|
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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String applyUniquesToTable( Table table ) {
|
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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String applyUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
|
public String applyUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
|
||||||
String defaultCatalog, String defaultSchema ) {
|
String defaultCatalog, String defaultSchema ) {
|
||||||
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
// Do this here, rather than allowing UniqueKey/Constraint to do it.
|
||||||
// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
|
// We need full, simplified control over whether or not it happens.
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
|
|
||||||
// defaultCatalog, defaultSchema );
|
|
||||||
// }
|
|
||||||
|
|
||||||
return new StringBuilder( "alter table " )
|
return new StringBuilder( "alter table " )
|
||||||
.append( uniqueKey.getTable().getQualifiedName(
|
.append( uniqueKey.getTable().getQualifiedName(
|
||||||
dialect, defaultCatalog, defaultSchema ) )
|
dialect, defaultCatalog, defaultSchema ) )
|
||||||
|
@ -134,15 +84,9 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String applyUniquesOnAlter( UniqueKey uniqueKey ) {
|
public String applyUniquesOnAlter( UniqueKey uniqueKey ) {
|
||||||
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
// Do this here, rather than allowing UniqueKey/Constraint to do it.
|
||||||
// return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
|
// We need full, simplified control over whether or not it happens.
|
||||||
// }
|
return new StringBuilder( "alter table " )
|
||||||
// else {
|
|
||||||
// return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
|
|
||||||
// defaultCatalog, defaultSchema );
|
|
||||||
// }
|
|
||||||
|
|
||||||
return new StringBuilder( "alter table " )
|
|
||||||
.append( uniqueKey.getTable().getQualifiedName( dialect ) )
|
.append( uniqueKey.getTable().getQualifiedName( dialect ) )
|
||||||
.append( " add constraint " )
|
.append( " add constraint " )
|
||||||
.append( uniqueKey.getName() )
|
.append( uniqueKey.getName() )
|
||||||
|
@ -153,13 +97,8 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
@Override
|
@Override
|
||||||
public String dropUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
|
public String dropUniquesOnAlter( org.hibernate.mapping.UniqueKey uniqueKey,
|
||||||
String defaultCatalog, String defaultSchema ) {
|
String defaultCatalog, String defaultSchema ) {
|
||||||
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
// Do this here, rather than allowing UniqueKey/Constraint to do it.
|
||||||
// return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
|
// We need full, simplified control over whether or not it happens.
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog, defaultSchema );
|
|
||||||
// }
|
|
||||||
|
|
||||||
return new StringBuilder( "alter table " )
|
return new StringBuilder( "alter table " )
|
||||||
.append( uniqueKey.getTable().getQualifiedName(
|
.append( uniqueKey.getTable().getQualifiedName(
|
||||||
dialect, defaultCatalog, defaultSchema ) )
|
dialect, defaultCatalog, defaultSchema ) )
|
||||||
|
@ -170,13 +109,8 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String dropUniquesOnAlter( UniqueKey uniqueKey ) {
|
public String dropUniquesOnAlter( UniqueKey uniqueKey ) {
|
||||||
// if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
|
// Do this here, rather than allowing UniqueKey/Constraint to do it.
|
||||||
// return super.sqlDropString( dialect, defaultCatalog, defaultSchema );
|
// We need full, simplified control over whether or not it happens.
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// return Index.buildSqlDropIndexString( dialect, getTable(), getName(), defaultCatalog, defaultSchema );
|
|
||||||
// }
|
|
||||||
|
|
||||||
return new StringBuilder( "alter table " )
|
return new StringBuilder( "alter table " )
|
||||||
.append( uniqueKey.getTable().getQualifiedName( dialect ) )
|
.append( uniqueKey.getTable().getQualifiedName( dialect ) )
|
||||||
.append( " drop constraint " )
|
.append( " drop constraint " )
|
||||||
|
@ -186,10 +120,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String uniqueConstraintSql( org.hibernate.mapping.UniqueKey uniqueKey ) {
|
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();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append( " unique (" );
|
sb.append( " unique (" );
|
||||||
Iterator columnIterator = uniqueKey.getColumnIterator();
|
Iterator columnIterator = uniqueKey.getColumnIterator();
|
||||||
|
@ -207,10 +137,6 @@ public class DefaultUniqueDelegate implements UniqueDelegate {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String uniqueConstraintSql( UniqueKey uniqueKey ) {
|
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();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append( " unique (" );
|
sb.append( " unique (" );
|
||||||
Iterator columnIterator = uniqueKey.getColumns().iterator();
|
Iterator columnIterator = uniqueKey.getColumns().iterator();
|
||||||
|
|
|
@ -28,12 +28,16 @@ import org.hibernate.metamodel.relational.UniqueKey;
|
||||||
* Dialect-level delegate in charge of applying "uniqueness" to a column.
|
* Dialect-level delegate in charge of applying "uniqueness" to a column.
|
||||||
* Uniqueness can be defined in 1 of 3 ways:
|
* 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.
|
* 2.) Add a unique constraint via dialect-specific syntax in table create statement.
|
||||||
* 3.) Add "unique" syntax to the column itself.
|
* 3.) Add "unique" syntax to the column itself.
|
||||||
*
|
*
|
||||||
* #1 & #2 are preferred, if possible -- #3 should be solely a fall-back.
|
* #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.
|
* See HHH-7797.
|
||||||
*
|
*
|
||||||
* @author Brett Meyer
|
* @author Brett Meyer
|
||||||
|
@ -43,7 +47,8 @@ public interface UniqueDelegate {
|
||||||
/**
|
/**
|
||||||
* If the delegate supports unique constraints, this method should simply
|
* If the delegate supports unique constraints, this method should simply
|
||||||
* create the UniqueKey on the Table. Otherwise, the constraint isn't
|
* 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 table
|
||||||
* @param column
|
* @param column
|
||||||
|
@ -55,7 +60,8 @@ public interface UniqueDelegate {
|
||||||
/**
|
/**
|
||||||
* If the delegate supports unique constraints, this method should simply
|
* If the delegate supports unique constraints, this method should simply
|
||||||
* create the UniqueKey on the Table. Otherwise, the constraint isn't
|
* 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 table
|
||||||
* @param column
|
* @param column
|
||||||
|
@ -64,9 +70,9 @@ public interface UniqueDelegate {
|
||||||
public String applyUniqueToColumn( Table table, Column column );
|
public String applyUniqueToColumn( Table table, Column column );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If creating unique constraints in separate alter statements are not
|
* If constraints are supported, but not in seperate alter statements,
|
||||||
* supported, this method should return the syntax necessary to create
|
* return uniqueConstraintSql in order to add the constraint to the
|
||||||
* the constraint on the original create table statement.
|
* original table definition.
|
||||||
*
|
*
|
||||||
* @param table
|
* @param table
|
||||||
* @return String
|
* @return String
|
||||||
|
@ -74,9 +80,9 @@ public interface UniqueDelegate {
|
||||||
public String applyUniquesToTable( org.hibernate.mapping.Table table );
|
public String applyUniquesToTable( org.hibernate.mapping.Table table );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If creating unique constraints in separate alter statements are not
|
* If constraints are supported, but not in seperate alter statements,
|
||||||
* supported, this method should return the syntax necessary to create
|
* return uniqueConstraintSql in order to add the constraint to the
|
||||||
* the constraint on the original create table statement.
|
* original table definition.
|
||||||
*
|
*
|
||||||
* @param table
|
* @param table
|
||||||
* @return String
|
* @return String
|
||||||
|
|
|
@ -20,18 +20,23 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.constraint;
|
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.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
import org.hibernate.mapping.Column;
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HHH-7797 re-wrote the way dialects handle unique constraints. Test as
|
* HHH-7797 re-wrote the way dialects handle unique constraints. Test
|
||||||
* many variations of unique, not null, and primary key constraints as possible.
|
* variations of unique & not null to ensure the constraints are created
|
||||||
|
* correctly for each dialect.
|
||||||
*
|
*
|
||||||
* @author Brett Meyer
|
* @author Brett Meyer
|
||||||
*/
|
*/
|
||||||
|
@ -47,18 +52,34 @@ public class ConstraintTest extends BaseCoreFunctionalTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConstraints() {
|
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
|
@Entity
|
||||||
|
@Table( name = "Entity1" )
|
||||||
public static class Entity1 {
|
public static class Entity1 {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
// @Column( nullable = false, unique = true)
|
@javax.persistence.Column( nullable = false, unique = true)
|
||||||
public long id;
|
public long id;
|
||||||
|
|
||||||
@Column( nullable = false, unique = true)
|
@javax.persistence.Column( nullable = false, unique = true)
|
||||||
public String foo;
|
public String foo1;
|
||||||
|
|
||||||
|
@javax.persistence.Column( nullable = true, unique = true)
|
||||||
|
public String foo2;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue