From a3f8addd51fd809b370dcec9fd713782d95fab64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Kuruc?= Date: Wed, 22 May 2024 15:06:25 +0200 Subject: [PATCH] HHH-18153 Fix for Informix unique constraints on nullness columns --- .../unique/InformixUniqueDelegate.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/unique/InformixUniqueDelegate.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/unique/InformixUniqueDelegate.java index 2697a4e0d0..3cddef5467 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/unique/InformixUniqueDelegate.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/unique/InformixUniqueDelegate.java @@ -9,31 +9,42 @@ package org.hibernate.community.dialect.unique; import org.hibernate.boot.Metadata; import org.hibernate.boot.model.relational.SqlStringGenerationContext; import org.hibernate.dialect.Dialect; -import org.hibernate.dialect.unique.AlterTableUniqueDelegate; +import org.hibernate.dialect.unique.SkipNullableUniqueDelegate; import org.hibernate.mapping.UniqueKey; /** - * Informix requires the constraint name to come last on the alter table. + * Informix requires the constraint name to come last. * * @author Brett Meyer */ -public class InformixUniqueDelegate extends AlterTableUniqueDelegate { +public class InformixUniqueDelegate extends SkipNullableUniqueDelegate { public InformixUniqueDelegate( Dialect dialect ) { super( dialect ); } - // legacy model ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + @Override + protected void appendUniqueConstraint(StringBuilder fragment, UniqueKey uniqueKey) { + if ( !uniqueKey.hasNullableColumn() ) { + fragment.append( ", " ); + fragment.append( uniqueConstraintSql(uniqueKey) ); + if ( uniqueKey.isNameExplicit() ) { + fragment.append( " constraint " ).append( uniqueKey.getName() ); + } + } + } @Override public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata, SqlStringGenerationContext context) { - // Do this here, rather than allowing UniqueKey/Constraint to do it. - // We need full, simplified control over whether it happens. - final String tableName = context.format( uniqueKey.getTable().getQualifiedTableName() ); - final String constraintName = dialect.quote( uniqueKey.getName() ); - return dialect.getAlterTableString( tableName ) - + " add constraint " + uniqueConstraintSql( uniqueKey ) + " constraint " + constraintName; + if (uniqueKey.hasNullableColumn() || !context.isMigration()){ + return ""; + } else { + final String tableName = context.format( uniqueKey.getTable().getQualifiedTableName() ); + final String constraintName = dialect.quote( uniqueKey.getName() ); + return dialect.getAlterTableString( tableName ) + + " add constraint " + uniqueConstraintSql( uniqueKey ) + " constraint " + constraintName; + } } }