FileDocCategorySizeDatePackage
Table.javaAPI DocHibernate 3.2.520578Tue Mar 20 06:06:38 GMT 2007org.hibernate.mapping

Table

public class Table extends Object implements Serializable, RelationalModel
A relational table
author
Gavin King

Fields Summary
private String
name
private String
schema
private String
catalog
private Map
columns
contains all columns, including the primary key
private KeyValue
idValue
private PrimaryKey
primaryKey
private Map
indexes
private Map
foreignKeys
private Map
uniqueKeys
private final int
uniqueInteger
private boolean
quoted
private boolean
schemaQuoted
private static int
tableCounter
private List
checkConstraints
private String
rowId
private String
subselect
private boolean
isAbstract
private boolean
hasDenormalizedTables
private String
comment
Constructors Summary
public Table()

		uniqueInteger = tableCounter++;
	
public Table(String name)

		this();
		setName( name );
	
Methods Summary
public voidaddCheckConstraint(java.lang.String constraint)

		checkConstraints.add( constraint );
	
public voidaddColumn(Column column)

		Column old = (Column) getColumn( column );
		if ( old == null ) {
			columns.put( column.getCanonicalName(), column );
			column.uniqueInteger = columns.size();
		}
		else {
			column.uniqueInteger = old.uniqueInteger;
		}
	
public IndexaddIndex(Index index)

		Index current = (Index) indexes.get( index.getName() );
		if ( current != null ) {
			throw new MappingException( "Index " + index.getName() + " already exists!" );
		}
		indexes.put( index.getName(), index );
		return index;
	
public UniqueKeyaddUniqueKey(UniqueKey uniqueKey)

		UniqueKey current = (UniqueKey) uniqueKeys.get( uniqueKey.getName() );
		if ( current != null ) {
			throw new MappingException( "UniqueKey " + uniqueKey.getName() + " already exists!" );
		}
		uniqueKeys.put( uniqueKey.getName(), uniqueKey );
		return uniqueKey;
	
public booleancontainsColumn(Column column)

		return columns.containsValue( column );
	
public ForeignKeycreateForeignKey(java.lang.String keyName, java.util.List keyColumns, java.lang.String referencedEntityName)

		return createForeignKey( keyName, keyColumns, referencedEntityName, null );
	
public ForeignKeycreateForeignKey(java.lang.String keyName, java.util.List keyColumns, java.lang.String referencedEntityName, java.util.List referencedColumns)

		Object key = new ForeignKeyKey( keyColumns, referencedEntityName, referencedColumns );

		ForeignKey fk = (ForeignKey) foreignKeys.get( key );
		if ( fk == null ) {
			fk = new ForeignKey();
			if ( keyName != null ) {
				fk.setName( keyName );
			}
			else {
				fk.setName( "FK" + uniqueColumnString( keyColumns.iterator(), referencedEntityName ) );
				//TODO: add referencedClass to disambiguate to FKs on the same
				//      columns, pointing to different tables
			}
			fk.setTable( this );
			foreignKeys.put( key, fk );
			fk.setReferencedEntityName( referencedEntityName );
			fk.addColumns( keyColumns.iterator() );
			if ( referencedColumns != null ) {
				fk.addReferencedColumns( referencedColumns.iterator() );
			}
		}

		if ( keyName != null ) {
			fk.setName( keyName );
		}

		return fk;
	
public voidcreateForeignKeys()

	
public UniqueKeycreateUniqueKey(java.util.List keyColumns)

		String keyName = "UK" + uniqueColumnString( keyColumns.iterator() );
		UniqueKey uk = getOrCreateUniqueKey( keyName );
		uk.addColumns( keyColumns.iterator() );
		return uk;
	
public java.lang.StringgetCatalog()

		return catalog;
	
public java.util.IteratorgetCheckConstraintsIterator()

		return checkConstraints.iterator();
	
public ColumngetColumn(Column column)
Return the column which is identified by column provided as argument.

param
column column with atleast a name.
return
the underlying column or null if not inside this table. Note: the instance *can* be different than the input parameter, but the name will be the same.

		if ( column == null ) {
			return null;
		}

		Column myColumn = (Column) columns.get( column.getCanonicalName() );

		return column.equals( myColumn ) ?
				myColumn :
				null;
	
public ColumngetColumn(int n)

		Iterator iter = columns.values().iterator();
		for ( int i = 0; i < n - 1; i++ ) {
			iter.next();
		}
		return (Column) iter.next();
	
public java.util.IteratorgetColumnIterator()

		return columns.values().iterator();
	
public intgetColumnSpan()

		return columns.size();
	
public java.lang.StringgetComment()

		return comment;
	
public java.util.IteratorgetForeignKeyIterator()

		return foreignKeys.values().iterator();
	
public KeyValuegetIdentifierValue()

		return idValue;
	
public IndexgetIndex(java.lang.String indexName)

		return (Index) indexes.get( indexName );
	
public java.util.IteratorgetIndexIterator()

		return indexes.values().iterator();
	
public java.lang.StringgetName()

		return name;
	
public IndexgetOrCreateIndex(java.lang.String indexName)


		Index index = (Index) indexes.get( indexName );

		if ( index == null ) {
			index = new Index();
			index.setName( indexName );
			index.setTable( this );
			indexes.put( indexName, index );
		}

		return index;
	
public UniqueKeygetOrCreateUniqueKey(java.lang.String keyName)

		UniqueKey uk = (UniqueKey) uniqueKeys.get( keyName );

		if ( uk == null ) {
			uk = new UniqueKey();
			uk.setName( keyName );
			uk.setTable( this );
			uniqueKeys.put( keyName, uk );
		}
		return uk;
	
public PrimaryKeygetPrimaryKey()

		return primaryKey;
	
public java.lang.StringgetQualifiedName(org.hibernate.dialect.Dialect dialect, java.lang.String defaultCatalog, java.lang.String defaultSchema)

		if ( subselect != null ) {
			return "( " + subselect + " )";
		}
		String quotedName = getQuotedName( dialect );
		String usedSchema = schema == null ?
				defaultSchema :
				getQuotedSchema( dialect );
		String usedCatalog = catalog == null ?
				defaultCatalog :
				catalog;
		return qualify( usedCatalog, usedSchema, quotedName );
	
public java.lang.StringgetQuotedName()
returns quoted name as it would be in the mapping file.

		return quoted ?
				"`" + name + "`" :
				name;
	
public java.lang.StringgetQuotedName(org.hibernate.dialect.Dialect dialect)

		return quoted ?
				dialect.openQuote() + name + dialect.closeQuote() :
				name;
	
public java.lang.StringgetQuotedSchema()
returns quoted name as it is in the mapping file.

		return schemaQuoted ?
				"`" + schema + "`" :
				schema;
	
public java.lang.StringgetQuotedSchema(org.hibernate.dialect.Dialect dialect)

		return schemaQuoted ?
				dialect.openQuote() + schema + dialect.closeQuote() :
				schema;
	
public java.lang.StringgetRowId()

		return rowId;
	
public java.lang.StringgetSchema()

		return schema;
	
public java.lang.StringgetSubselect()

		return subselect;
	
public intgetUniqueInteger()

		return uniqueInteger;
	
public UniqueKeygetUniqueKey(java.lang.String keyName)

		return (UniqueKey) uniqueKeys.get( keyName );
	
public java.util.IteratorgetUniqueKeyIterator()

		return getUniqueKeys().values().iterator();
	
java.util.MapgetUniqueKeys()

		if ( uniqueKeys.size() > 1 ) {
			//deduplicate unique constraints sharing the same columns
			//this is needed by Hibernate Annotations since it creates automagically
			// unique constraints for the user
			Iterator it = uniqueKeys.entrySet().iterator();
			Map finalUniqueKeys = new HashMap( uniqueKeys.size() );
			while ( it.hasNext() ) {
				Map.Entry entry = (Map.Entry) it.next();
				UniqueKey uk = (UniqueKey) entry.getValue();
				List columns = uk.getColumns();
				int size = finalUniqueKeys.size();
				boolean skip = false;
				Iterator tempUks = finalUniqueKeys.entrySet().iterator();
				while ( tempUks.hasNext() ) {
					final UniqueKey currentUk = (UniqueKey) ( (Map.Entry) tempUks.next() ).getValue();
					if ( currentUk.getColumns().containsAll( columns ) && columns
							.containsAll( currentUk.getColumns() ) ) {
						skip = true;
						break;
					}
				}
				if ( !skip ) finalUniqueKeys.put( entry.getKey(), uk );
			}
			return finalUniqueKeys;
		}
		else {
			return uniqueKeys;
		}
	
public booleanhasDenormalizedTables()

		return hasDenormalizedTables;
	
public booleanhasPrimaryKey()

		return getPrimaryKey() != null;
	
public booleanisAbstract()

		return isAbstract;
	
public booleanisAbstractUnionTable()

		return hasDenormalizedTables() && isAbstract;
	
public booleanisPhysicalTable()

		return !isSubselect() && !isAbstractUnionTable();
	
public booleanisQuoted()

		return quoted;
	
public booleanisSchemaQuoted()

		return schemaQuoted;
	
public booleanisSubselect()

		return subselect != null;
	
public static java.lang.Stringqualify(java.lang.String catalog, java.lang.String schema, java.lang.String table)

		StringBuffer qualifiedName = new StringBuffer();
		if ( catalog != null ) {
			qualifiedName.append( catalog ).append( '." );
		}
		if ( schema != null ) {
			qualifiedName.append( schema ).append( '." );
		}
		return qualifiedName.append( table ).toString();
	
public voidsetAbstract(boolean isAbstract)

		this.isAbstract = isAbstract;
	
public voidsetCatalog(java.lang.String catalog)

		this.catalog = catalog;
	
public voidsetComment(java.lang.String comment)

		this.comment = comment;
	
voidsetHasDenormalizedTables()

		hasDenormalizedTables = true;
	
public voidsetIdentifierValue(KeyValue idValue)

		this.idValue = idValue;
	
public voidsetName(java.lang.String name)

		if ( name.charAt( 0 ) == '`" ) {
			quoted = true;
			this.name = name.substring( 1, name.length() - 1 );
		}
		else {
			this.name = name;
		}
	
public voidsetPrimaryKey(PrimaryKey primaryKey)

		this.primaryKey = primaryKey;
	
public voidsetQuoted(boolean quoted)

		this.quoted = quoted;
	
public voidsetRowId(java.lang.String rowId)

		this.rowId = rowId;
	
public voidsetSchema(java.lang.String schema)

		if ( schema != null && schema.charAt( 0 ) == '`" ) {
			schemaQuoted = true;
			this.schema = schema.substring( 1, schema.length() - 1 );
		}
		else {
			this.schema = schema;
		}
	
public voidsetSubselect(java.lang.String subselect)

		this.subselect = subselect;
	
public java.util.IteratorsqlAlterStrings(org.hibernate.dialect.Dialect dialect, org.hibernate.engine.Mapping p, org.hibernate.tool.hbm2ddl.TableMetadata tableInfo, java.lang.String defaultCatalog, java.lang.String defaultSchema)


		StringBuffer root = new StringBuffer( "alter table " )
				.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
				.append( ' " )
				.append( dialect.getAddColumnString() );

		Iterator iter = getColumnIterator();
		List results = new ArrayList();
		while ( iter.hasNext() ) {
			Column column = (Column) iter.next();

			ColumnMetadata columnInfo = tableInfo.getColumnMetadata( column.getName() );

			if ( columnInfo == null ) {
				// the column doesnt exist at all.
				StringBuffer alter = new StringBuffer( root.toString() )
						.append( ' " )
						.append( column.getQuotedName( dialect ) )
						.append( ' " )
						.append( column.getSqlType( dialect, p ) );

				String defaultValue = column.getDefaultValue();
				if ( defaultValue != null ) {
					alter.append( " default " ).append( defaultValue );

					if ( column.isNullable() ) {
						alter.append( dialect.getNullColumnString() );
					}
					else {
						alter.append( " not null" );
					}

				}

				boolean useUniqueConstraint = column.isUnique() &&
						dialect.supportsUnique() &&
						( !column.isNullable() || dialect.supportsNotNullUnique() );
				if ( useUniqueConstraint ) {
					alter.append( " unique" );
				}

				if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
					alter.append( " check(" )
							.append( column.getCheckConstraint() )
							.append( ")" );
				}

				String columnComment = column.getComment();
				if ( columnComment != null ) {
					alter.append( dialect.getColumnComment( columnComment ) );
				}

				results.add( alter.toString() );
			}

		}

		return results.iterator();
	
public java.util.IteratorsqlCommentStrings(org.hibernate.dialect.Dialect dialect, java.lang.String defaultCatalog, java.lang.String defaultSchema)

		List comments = new ArrayList();
		if ( dialect.supportsCommentOn() ) {
			String tableName = getQualifiedName( dialect, defaultCatalog, defaultSchema );
			if ( comment != null ) {
				StringBuffer buf = new StringBuffer()
						.append( "comment on table " )
						.append( tableName )
						.append( " is '" )
						.append( comment )
						.append( "'" );
				comments.add( buf.toString() );
			}
			Iterator iter = getColumnIterator();
			while ( iter.hasNext() ) {
				Column column = (Column) iter.next();
				String columnComment = column.getComment();
				if ( columnComment != null ) {
					StringBuffer buf = new StringBuffer()
							.append( "comment on column " )
							.append( tableName )
							.append( '." )
							.append( column.getQuotedName( dialect ) )
							.append( " is '" )
							.append( columnComment )
							.append( "'" );
					comments.add( buf.toString() );
				}
			}
		}
		return comments.iterator();
	
public java.lang.StringsqlCreateString(org.hibernate.dialect.Dialect dialect, org.hibernate.engine.Mapping p, java.lang.String defaultCatalog, java.lang.String defaultSchema)

		StringBuffer buf = new StringBuffer( hasPrimaryKey() ? dialect.getCreateTableString() : dialect.getCreateMultisetTableString() )
				.append( ' " )
				.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
				.append( " (" );

		boolean identityColumn = idValue != null && idValue.isIdentityColumn( dialect );

		// Try to find out the name of the primary key to create it as identity if the IdentityGenerator is used
		String pkname = null;
		if ( hasPrimaryKey() && identityColumn ) {
			pkname = ( (Column) getPrimaryKey().getColumnIterator().next() ).getQuotedName( dialect );
		}

		Iterator iter = getColumnIterator();
		while ( iter.hasNext() ) {
			Column col = (Column) iter.next();

			buf.append( col.getQuotedName( dialect ) )
					.append( ' " );

			if ( identityColumn && col.getQuotedName( dialect ).equals( pkname ) ) {
				// to support dialects that have their own identity data type
				if ( dialect.hasDataTypeInIdentityColumn() ) {
					buf.append( col.getSqlType( dialect, p ) );
				}
				buf.append( ' " )
						.append( dialect.getIdentityColumnString( col.getSqlTypeCode( p ) ) );
			}
			else {

				buf.append( col.getSqlType( dialect, p ) );

				String defaultValue = col.getDefaultValue();
				if ( defaultValue != null ) {
					buf.append( " default " ).append( defaultValue );
				}

				if ( col.isNullable() ) {
					buf.append( dialect.getNullColumnString() );
				}
				else {
					buf.append( " not null" );
				}

			}

			boolean useUniqueConstraint = col.isUnique() &&
					( !col.isNullable() || dialect.supportsNotNullUnique() );
			if ( useUniqueConstraint ) {
				if ( dialect.supportsUnique() ) {
					buf.append( " unique" );
				}
				else {
					UniqueKey uk = getOrCreateUniqueKey( col.getQuotedName( dialect ) + '_" );
					uk.addColumn( col );
				}
			}

			if ( col.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
				buf.append( " check (" )
						.append( col.getCheckConstraint() )
						.append( ")" );
			}

			String columnComment = col.getComment();
			if ( columnComment != null ) {
				buf.append( dialect.getColumnComment( columnComment ) );
			}

			if ( iter.hasNext() ) {
				buf.append( ", " );
			}

		}
		if ( hasPrimaryKey() ) {
			buf.append( ", " )
					.append( getPrimaryKey().sqlConstraintString( dialect ) );
		}

		if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
			Iterator ukiter = getUniqueKeyIterator();
			while ( ukiter.hasNext() ) {
				UniqueKey uk = (UniqueKey) ukiter.next();
				String constraint = uk.sqlConstraintString( dialect );
				if ( constraint != null ) {
					buf.append( ", " ).append( constraint );
				}
			}
		}
		/*Iterator idxiter = getIndexIterator();
		while ( idxiter.hasNext() ) {
			Index idx = (Index) idxiter.next();
			buf.append(',').append( idx.sqlConstraintString(dialect) );
		}*/

		if ( dialect.supportsTableCheck() ) {
			Iterator chiter = checkConstraints.iterator();
			while ( chiter.hasNext() ) {
				buf.append( ", check (" )
						.append( chiter.next() )
						.append( ')" );
			}
		}

		buf.append( ')" );

		if ( comment != null ) {
			buf.append( dialect.getTableComment( comment ) );
		}

		return buf.append( dialect.getTableTypeString() ).toString();
	
public java.lang.StringsqlDropString(org.hibernate.dialect.Dialect dialect, java.lang.String defaultCatalog, java.lang.String defaultSchema)

		StringBuffer buf = new StringBuffer( "drop table " );
		if ( dialect.supportsIfExistsBeforeTableName() ) {
			buf.append( "if exists " );
		}
		buf.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
				.append( dialect.getCascadeConstraintsString() );
		if ( dialect.supportsIfExistsAfterTableName() ) {
			buf.append( " if exists" );
		}
		return buf.toString();
	
public java.lang.StringsqlTemporaryTableCreateString(org.hibernate.dialect.Dialect dialect, org.hibernate.engine.Mapping mapping)

		StringBuffer buffer = new StringBuffer( dialect.getCreateTemporaryTableString() )
				.append( ' " )
				.append( name )
				.append( " (" );
		Iterator itr = getColumnIterator();
		while ( itr.hasNext() ) {
			final Column column = (Column) itr.next();
			buffer.append( column.getQuotedName( dialect ) ).append( ' " );
			buffer.append( column.getSqlType( dialect, mapping ) );
			if ( column.isNullable() ) {
				buffer.append( dialect.getNullColumnString() );
			}
			else {
				buffer.append( " not null" );
			}
			if ( itr.hasNext() ) {
				buffer.append( ", " );
			}
		}
		buffer.append( ") " );
		buffer.append( dialect.getCreateTemporaryTablePostfix() );
		return buffer.toString();
	
public java.lang.StringtoString()

		StringBuffer buf = new StringBuffer().append( getClass().getName() )
				.append( '(" );
		if ( getCatalog() != null ) {
			buf.append( getCatalog() + "." );
		}
		if ( getSchema() != null ) {
			buf.append( getSchema() + "." );
		}
		buf.append( getName() ).append( ')" );
		return buf.toString();
	
public java.lang.StringuniqueColumnString(java.util.Iterator iterator)

		return uniqueColumnString( iterator, null );
	
public java.lang.StringuniqueColumnString(java.util.Iterator iterator, java.lang.String referencedEntityName)

		int result = 0;
		if ( referencedEntityName != null ) {
			result += referencedEntityName.hashCode();
		}
		while ( iterator.hasNext() ) {
			result += iterator.next().hashCode();
		}
		return ( Integer.toHexString( name.hashCode() ) + Integer.toHexString( result ) ).toUpperCase();
	
public voidvalidateColumns(org.hibernate.dialect.Dialect dialect, org.hibernate.engine.Mapping mapping, org.hibernate.tool.hbm2ddl.TableMetadata tableInfo)

		Iterator iter = getColumnIterator();
		while ( iter.hasNext() ) {
			Column col = (Column) iter.next();

			ColumnMetadata columnInfo = tableInfo.getColumnMetadata( col.getName() );

			if ( columnInfo == null ) {
				throw new HibernateException( "Missing column: " + col.getName() + " in " + Table.qualify( tableInfo.getCatalog(), tableInfo.getSchema(), tableInfo.getName()));
			}
			else {
				final boolean typesMatch = col.getSqlType( dialect, mapping )
						.startsWith( columnInfo.getTypeName().toLowerCase() )
						|| columnInfo.getTypeCode() == col.getSqlTypeCode( mapping );
				if ( !typesMatch ) {
					throw new HibernateException(
							"Wrong column type: " + col.getName() +
									", expected: " + col.getSqlType( dialect, mapping )
					);
				}
			}
		}