All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.mapping.PrimaryKey Maven / Gradle / Ivy

There is a newer version: 6.6.2.Final
Show newest version
/*
 * 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.mapping;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Internal;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;

import org.jboss.logging.Logger;

/**
 * A mapping model object representing a primary key constraint.
 *
 * @author Gavin King
 * @author Steve Ebersole
 */
public class PrimaryKey extends Constraint {
	private static final Logger log = Logger.getLogger( PrimaryKey.class );

	private int[] originalOrder;

	public PrimaryKey(Table table){
		setTable( table );
	}

	@Override
	public void addColumn(Column column) {
		for ( Column next : getTable().getColumns() ) {
			if ( next.getCanonicalName().equals( column.getCanonicalName() ) ) {
				next.setNullable( false );
				if ( log.isDebugEnabled() ) {
					log.debugf(
							"Forcing column [%s] to be non-null as it is part of the primary key for table [%s]",
							column.getCanonicalName(),
							getTableNameForLogging( column )
					);
				}
			}
		}
		super.addColumn( column );
	}

	protected String getTableNameForLogging(Column column) {
		if ( getTable() != null ) {
			if ( getTable().getNameIdentifier() != null ) {
				return getTable().getNameIdentifier().getCanonicalName();
			}
			else {
				return "";
			}
		}
		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 (");
		boolean first = true;
		for ( Column column : getColumns() ) {
			if ( first ) {
				first = false;
			}
			else {
				buf.append(", ");
			}
			buf.append( column.getQuotedName( dialect ) );
		}
		return buf.append(')').toString();
	}

	@Override @Deprecated(since="6.2", forRemoval = true)
	public String sqlConstraintString(SqlStringGenerationContext context, String constraintName, String defaultCatalog, String defaultSchema) {
		Dialect dialect = context.getDialect();
		StringBuilder buf = new StringBuilder();
		buf.append( dialect.getAddPrimaryKeyConstraintString( constraintName ) ).append('(');
		boolean first = true;
		for ( Column column : getColumns() ) {
			if ( first ) {
				first = false;
			}
			else {
				buf.append(", ");
			}
			buf.append( column.getQuotedName( dialect ) );
		}
		return buf.append(')').toString();
	}
	
	public String generatedConstraintNamePrefix() {
		return "PK_";
	}

	@Override
	public String getExportIdentifier() {
		return StringHelper.qualify( getTable().getExportIdentifier(), "PK-" + getName() );
	}

	public List getColumnsInOriginalOrder() {
		if ( originalOrder == null ) {
			return getColumns();
		}
		final List columns = getColumns();
		final Column[] columnsInOriginalOrder = new Column[columns.size()];
		for ( int i = 0; i < columnsInOriginalOrder.length; i++ ) {
			columnsInOriginalOrder[i] = columns.get( originalOrder[i] );
		}
		return Arrays.asList( columnsInOriginalOrder );
	}

	@Internal
	public void reorderColumns(List reorderedColumns) {
		if ( originalOrder != null ) {
			assert getColumns().equals( reorderedColumns );
			return;
		}
		assert getColumns().size() == reorderedColumns.size() && getColumns().containsAll( reorderedColumns );
		final List columns = getColumns();
		originalOrder = new int[columns.size()];
		for ( int i = 0; i < reorderedColumns.size(); i++ ) {
			final Column reorderedColumn = reorderedColumns.get( i );
			originalOrder[i] = columns.indexOf( reorderedColumn );
		}
		columns.clear();
		columns.addAll( reorderedColumns );
	}

	@Internal
	public int[] getOriginalOrder() {
		return originalOrder;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy