org.hibernate.dialect.unique.DB2UniqueDelegate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core-jakarta Show documentation
Show all versions of hibernate-core-jakarta Show documentation
Hibernate O/RM implementation of the Jakarta Persistence specification
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.dialect.unique;
import java.util.Iterator;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.mapping.UniqueKey;
/**
* DB2 does not allow unique constraints on nullable columns. Rather than
* forcing "not null", use unique *indexes* instead.
*
* @author Brett Meyer
*/
public class DB2UniqueDelegate extends DefaultUniqueDelegate {
/**
* Constructs a DB2UniqueDelegate
*
* @param dialect The dialect
*/
public DB2UniqueDelegate( Dialect dialect ) {
super( dialect );
}
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
if ( hasNullable( uniqueKey ) ) {
return org.hibernate.mapping.Index.buildSqlCreateIndexString(
context,
uniqueKey.getName(),
uniqueKey.getTable(),
uniqueKey.columnIterator(),
uniqueKey.getColumnOrderMap(),
true,
metadata
);
}
else {
return super.getAlterTableToAddUniqueKeyCommand( uniqueKey, metadata, context );
}
}
@Override
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata,
SqlStringGenerationContext context) {
if ( hasNullable( uniqueKey ) ) {
return org.hibernate.mapping.Index.buildSqlDropIndexString(
uniqueKey.getName(),
context.format( uniqueKey.getTable().getQualifiedTableName() )
);
}
else {
return super.getAlterTableToDropUniqueKeyCommand( uniqueKey, metadata, context );
}
}
private boolean hasNullable(org.hibernate.mapping.UniqueKey uniqueKey) {
final Iterator iter = uniqueKey.columnIterator();
while ( iter.hasNext() ) {
if ( iter.next().isNullable() ) {
return true;
}
}
return false;
}
}