Java Code Examples for org.hibernate.mapping.Table#getColumnIterator()

The following examples show how to use org.hibernate.mapping.Table#getColumnIterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpannerTableExporter.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Processes the columns of the table and creates Unique Constraints for columns
 * annotated with @Column(unique = true).
 */
private static void initializeUniqueConstraints(Table table) {
  Iterator<Column> colIterator = table.getColumnIterator();
  while (colIterator.hasNext()) {
    Column col = colIterator.next();
    if (col.isUnique()) {
      String name = Constraint.generateName("UK_", table, col);
      UniqueKey uk = table.getOrCreateUniqueKey(name);
      uk.addColumn(col);
    }
  }
}
 
Example 2
Source File: SpannerTableStatements.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<String> getCreateTableStrings(
    Table table, Metadata metadata, Iterable<Column> keyColumns) {

  // Get the comma separated string of the primary keys of the table.
  String primaryKeyColNames = StreamSupport.stream(keyColumns.spliterator(), false)
      .map(Column::getQuotedName)
      .collect(Collectors.joining(","));

  // Get the comma separated string of all columns of the table.
  Iterable<Column> columnIterable = () -> (Iterator<Column>) table.getColumnIterator();
  String allColumnNames = StreamSupport.stream(columnIterable.spliterator(), false)
      .map(column -> buildColumnTypeString(column, metadata))
      .collect(Collectors.joining(","));

  ArrayList<String> statements = new ArrayList<>();

  // Build the Create Table string.
  String createTableString = MessageFormat.format(
      CREATE_TABLE_TEMPLATE,
      table.getQuotedName(),
      allColumnNames,
      primaryKeyColNames,
      getInterleavedClause(table, metadata));

  statements.add(createTableString);

  if (table.getName().equals(SequenceStyleGenerator.DEF_SEQUENCE_NAME)) {
    // Caches the INSERT statement since DML statements must be run after a DDL batch.
    addStatementAfterDdlBatch(
        metadata,
        "INSERT INTO " + SequenceStyleGenerator.DEF_SEQUENCE_NAME + " ("
            + SequenceStyleGenerator.DEF_VALUE_COLUMN + ") VALUES(1)");
  }

  return statements;
}
 
Example 3
Source File: AbstractSchemaValidator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void validateTable(
		Table table,
		TableInformation tableInformation,
		Metadata metadata,
		ExecutionOptions options,
		Dialect dialect) {
	if ( tableInformation == null ) {
		throw new SchemaManagementException(
				String.format(
						"Schema-validation: missing table [%s]",
						table.getQualifiedTableName().toString()
				)
		);
	}

	final Iterator selectableItr = table.getColumnIterator();
	while ( selectableItr.hasNext() ) {
		final Selectable selectable = (Selectable) selectableItr.next();
		if ( Column.class.isInstance( selectable ) ) {
			final Column column = (Column) selectable;
			final ColumnInformation existingColumn = tableInformation.getColumn( Identifier.toIdentifier( column.getQuotedName() ) );
			if ( existingColumn == null ) {
				throw new SchemaManagementException(
						String.format(
								"Schema-validation: missing column [%s] in table [%s]",
								column.getName(),
								table.getQualifiedTableName()
						)
				);
			}
			validateColumnType( table, column, existingColumn, metadata, options, dialect );
		}
	}
}
 
Example 4
Source File: StandardTableExporter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void applyComments(Table table, QualifiedName tableName, List<String> sqlStrings) {
	if ( dialect.supportsCommentOn() ) {
		if ( table.getComment() != null ) {
			sqlStrings.add( "comment on table " + tableName + " is '" + table.getComment() + "'" );
		}
		final Iterator iter = table.getColumnIterator();
		while ( iter.hasNext() ) {
			Column column = (Column) iter.next();
			String columnComment = column.getComment();
			if ( columnComment != null ) {
				sqlStrings.add( "comment on column " + tableName + '.' + column.getQuotedName( dialect ) + " is '" + columnComment + "'" );
			}
		}
	}
}
 
Example 5
Source File: AbstractMultiTableBulkIdStrategyImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected String buildIdTableCreateStatement(Table idTable, JdbcServices jdbcServices, MetadataImplementor metadata) {
	final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
	final Dialect dialect = jdbcEnvironment.getDialect();

	StringBuilder buffer = new StringBuilder( getIdTableSupport().getCreateIdTableCommand() )
			.append( ' ' )
			.append( jdbcEnvironment.getQualifiedObjectNameFormatter().format( idTable.getQualifiedTableName(), dialect ) )
			.append( " (" );

	Iterator itr = idTable.getColumnIterator();
	while ( itr.hasNext() ) {
		final Column column = (Column) itr.next();
		buffer.append( column.getQuotedName( dialect ) ).append( ' ' );
		buffer.append( column.getSqlType( dialect, metadata ) );
		if ( column.isNullable() ) {
			buffer.append( dialect.getNullColumnString() );
		}
		else {
			buffer.append( " not null" );
		}
		if ( itr.hasNext() ) {
			buffer.append( ", " );
		}
	}

	buffer.append( ") " );
	if ( getIdTableSupport().getCreateIdTableStatementOptions() != null ) {
		buffer.append( getIdTableSupport().getCreateIdTableStatementOptions() );
	}

	return buffer.toString();
}
 
Example 6
Source File: IgniteCacheInitializer.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void addTableInfo(QueryEntity queryEntity, SchemaDefinitionContext context, String tableName) {
	Namespace namespace = context.getDatabase().getDefaultNamespace();
	Optional<Table> tableOptional = namespace.getTables().stream().filter( currentTable -> currentTable.getName().equals( tableName ) ).findFirst();
	if ( tableOptional.isPresent() ) {
		Table table = tableOptional.get();
		for ( Iterator<Column> columnIterator = table.getColumnIterator(); columnIterator.hasNext(); ) {
			Column currentColumn = columnIterator.next();
			String fieldType = fieldType( currentColumn );
			queryEntity.addQueryField( StringHelper.realColumnName( currentColumn.getName() ), fieldType, null );
		}
	}
}