From e8da2d09b3eec8007e882f176bf375630169285a Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 24 Sep 2015 13:00:39 -0500 Subject: [PATCH] HHH-10045 - Teradata requires non null primary keys. --- .../org/hibernate/mapping/PrimaryKey.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/PrimaryKey.java b/hibernate-core/src/main/java/org/hibernate/mapping/PrimaryKey.java index 76daf4d986..f27b7ab975 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/PrimaryKey.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/PrimaryKey.java @@ -10,11 +10,42 @@ import java.util.Iterator; import org.hibernate.dialect.Dialect; import org.hibernate.internal.util.StringHelper; +import org.jboss.logging.Logger; + /** * A primary key constraint + * * @author Gavin King + * @author Steve Ebersole */ public class PrimaryKey extends Constraint { + private static final Logger log = Logger.getLogger( PrimaryKey.class ); + + @Override + public void addColumn(Column column) { + if ( column.isNullable() ) { + if ( log.isDebugEnabled() ) { + final String columnName = column.getCanonicalName(); + log.debugf( + "Forcing column [%s] to be non-null as it is part of the primary key for table [%s]", + columnName, + getTableNameForLogging( column ) + ); + } + column.setNullable( false ); + } + super.addColumn( column ); + } + + protected String getTableNameForLogging(Column column) { + if ( getTable() != null ) { + return getTable().getNameIdentifier().getCanonicalName(); + } + else if ( column.getValue() != null && column.getValue().getTable() != null ) { + return column.getValue().getTable().getNameIdentifier().getCanonicalName(); + } + return ""; + } public String sqlConstraintString(Dialect dialect) { StringBuilder buf = new StringBuilder("primary key (");