diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java index bd1e08cbbd..0d6bc2f5d4 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java @@ -2141,6 +2141,28 @@ public abstract class Dialect implements ConversionContext { return false; } + /** + * For dropping a constraint with an "alter table", can the phrase "if exists" be applied before the constraint name? + *
+ * NOTE : Only one or the other (or neither) of this and {@link #supportsIfExistsAfterConstraintName} should return true + * + * @return {@code true} if the "if exists" can be applied before the constraint name + */ + public boolean supportsIfExistsBeforeConstraintName() { + return false; + } + + /** + * For dropping a constraint with an "alter table", can the phrase "if exists" be applied after the constraint name? + * + * NOTE : Only one or the other (or neither) of this and {@link #supportsIfExistsBeforeConstraintName} should return true + * + * @return {@code true} if the "if exists" can be applied after the constraint name + */ + public boolean supportsIfExistsAfterConstraintName() { + return false; + } + /** * Generate a DROP TABLE statement * 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 e085a9ba18..15b1c4d6c3 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java @@ -255,6 +255,11 @@ public class H2Dialect extends Dialect { return true; } + @Override + public boolean supportsIfExistsAfterConstraintName() { + return true; + } + @Override public boolean supportsSequences() { return true; 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 4529760fd1..b65fa4e9bd 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 @@ -94,9 +94,17 @@ public class DefaultUniqueDelegate implements UniqueDelegate { String defaultSchema) { // Do this here, rather than allowing UniqueKey/Constraint to do it. // We need full, simplified control over whether or not it happens. - final String tableName = uniqueKey.getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema ); - final String constraintName = dialect.quote( uniqueKey.getName() ); - return "alter table " + tableName + " drop constraint " + constraintName; + final StringBuilder buf = new StringBuilder( "alter table " ); + buf.append( uniqueKey.getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema ) ); + buf.append(" drop constraint " ); + if ( dialect.supportsIfExistsBeforeConstraintName() ) { + buf.append( "if exists " ); + } + buf.append( dialect.quote( uniqueKey.getName() ) ); + if ( dialect.supportsIfExistsAfterConstraintName() ) { + buf.append( " if exists" ); + } + return buf.toString(); } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/ForeignKey.java b/hibernate-core/src/main/java/org/hibernate/mapping/ForeignKey.java index 02caf61783..a36cf87a9d 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/ForeignKey.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/ForeignKey.java @@ -127,10 +127,17 @@ public class ForeignKey extends Constraint { } public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { - return "alter table " + - getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) + - dialect.getDropForeignKeyString() + - getName(); + final StringBuilder buf = new StringBuilder( "alter table " ); + buf.append( getTable().getQualifiedName(dialect, defaultCatalog, defaultSchema) ); + buf.append( dialect.getDropForeignKeyString() ); + if ( dialect.supportsIfExistsBeforeConstraintName() ) { + buf.append( "if exists " ); + } + buf.append( getName() ); + if ( dialect.supportsIfExistsAfterConstraintName() ) { + buf.append( " if exists" ); + } + return buf.toString(); } public boolean isCascadeDeleteEnabled() {