org.hibernate.mapping.UniqueKey Java Examples

The following examples show how to use org.hibernate.mapping.UniqueKey. 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: DefaultUniqueDelegate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected String uniqueConstraintSql(UniqueKey uniqueKey) {
	final StringBuilder sb = new StringBuilder();
	sb.append( "unique (" );
	final Iterator<org.hibernate.mapping.Column> columnIterator = uniqueKey.columnIterator();
	while ( columnIterator.hasNext() ) {
		final org.hibernate.mapping.Column column = columnIterator.next();
		sb.append( column.getQuotedName( dialect ) );
		if ( uniqueKey.getColumnOrderMap().containsKey( column ) ) {
			sb.append( " " ).append( uniqueKey.getColumnOrderMap().get( column ) );
		}
		if ( columnIterator.hasNext() ) {
			sb.append( ", " );
		}
	}

	return sb.append( ')' ).toString();
}
 
Example #2
Source File: DefaultUniqueDelegate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
	final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();

	final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			uniqueKey.getTable().getQualifiedTableName(),
			dialect
	);

	final StringBuilder buf = new StringBuilder( dialect.getAlterTableString(tableName) );
	buf.append( getDropUnique() );
	if ( dialect.supportsIfExistsBeforeConstraintName() ) {
		buf.append( "if exists " );
	}
	buf.append( dialect.quote( uniqueKey.getName() ) );
	if ( dialect.supportsIfExistsAfterConstraintName() ) {
		buf.append( " if exists" );
	}
	return buf.toString();
}
 
Example #3
Source File: DB2UniqueDelegate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
	if ( hasNullable( uniqueKey ) ) {
		return org.hibernate.mapping.Index.buildSqlCreateIndexString(
				dialect,
				uniqueKey.getName(),
				uniqueKey.getTable(),
				uniqueKey.columnIterator(),
				uniqueKey.getColumnOrderMap(),
				true,
				metadata
		);
	}
	else {
		return super.getAlterTableToAddUniqueKeyCommand( uniqueKey, metadata );
	}
}
 
Example #4
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 #5
Source File: SpannerTableStatements.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Set<String> getTableIndices(Table table) {
  Set<String> tableIndices = new HashSet<>();

  Iterator<Index> indexIterator = table.getIndexIterator();
  while (indexIterator.hasNext()) {
    tableIndices.add(indexIterator.next().getName());
  }

  Iterator<UniqueKey> keyIterator = table.getUniqueKeyIterator();
  while (keyIterator.hasNext()) {
    tableIndices.add(keyIterator.next().getName());
  }

  return tableIndices;
}
 
Example #6
Source File: StandardUniqueKeyExporter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String[] getSqlCreateStrings(Constraint constraint, Metadata metadata) {
	return new String[] {
			dialect.getUniqueDelegate().getAlterTableToAddUniqueKeyCommand(
					(UniqueKey) constraint,
					metadata
			)
	};
}
 
Example #7
Source File: StandardUniqueKeyExporter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String[] getSqlDropStrings(Constraint constraint, Metadata metadata) {
	return new String[] {
			dialect.getUniqueDelegate().getAlterTableToDropUniqueKeyCommand(
					(UniqueKey) constraint,
					metadata
			)
	};
}
 
Example #8
Source File: DB2UniqueDelegate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean hasNullable(org.hibernate.mapping.UniqueKey uniqueKey) {
	final Iterator<org.hibernate.mapping.Column> iter = uniqueKey.columnIterator();
	while ( iter.hasNext() ) {
		if ( iter.next().isNullable() ) {
			return true;
		}
	}
	return false;
}
 
Example #9
Source File: DefaultUniqueDelegate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
	final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();

	final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			uniqueKey.getTable().getQualifiedTableName(),
			dialect
	);

	final String constraintName = dialect.quote( uniqueKey.getName() );
	return dialect.getAlterTableString( tableName )
			+ " add constraint " + constraintName + " " + uniqueConstraintSql( uniqueKey );
}
 
Example #10
Source File: InformixUniqueDelegate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
	// Do this here, rather than allowing UniqueKey/Constraint to do it.
	// We need full, simplified control over whether or not it happens.
	final String tableName = metadata.getDatabase().getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
			uniqueKey.getTable().getQualifiedTableName(),
			metadata.getDatabase().getJdbcEnvironment().getDialect()
	);
	final String constraintName = dialect.quote( uniqueKey.getName() );
	return dialect.getAlterTableString( tableName )
			+ " add constraint " + uniqueConstraintSql( uniqueKey ) + " constraint " + constraintName;
}
 
Example #11
Source File: DB2UniqueDelegate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
	if ( hasNullable( uniqueKey ) ) {
		return org.hibernate.mapping.Index.buildSqlDropIndexString(
				uniqueKey.getName(),
				metadata.getDatabase().getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
						uniqueKey.getTable().getQualifiedTableName(),
						metadata.getDatabase().getJdbcEnvironment().getDialect()
				)
		);
	}
	else {
		return super.getAlterTableToDropUniqueKeyCommand( uniqueKey, metadata );
	}
}
 
Example #12
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void process() {
	log.debugf( "Binding natural-id UniqueKey for entity : " + entityBinding.getEntityName() );

	final List<Identifier> columnNames = new ArrayList<Identifier>();

	final UniqueKey uk = new UniqueKey();
	uk.setTable( entityBinding.getTable() );
	for ( Property attributeBinding : attributeBindings ) {
		final Iterator itr = attributeBinding.getColumnIterator();
		while ( itr.hasNext() ) {
			final Object selectable = itr.next();
			if ( Column.class.isInstance( selectable ) ) {
				final Column column = (Column) selectable;
				uk.addColumn( column );
				columnNames.add(
						mappingDocument.getMetadataCollector().getDatabase().toIdentifier( column.getQuotedName() )
				);
			}
		}
		uk.addColumns( attributeBinding.getColumnIterator() );
	}

	final Identifier ukName = mappingDocument.getBuildingOptions().getImplicitNamingStrategy().determineUniqueKeyName(
			new ImplicitUniqueKeyNameSource() {
				@Override
				public Identifier getTableName() {
					return entityBinding.getTable().getNameIdentifier();
				}

				@Override
				public List<Identifier> getColumnNames() {
					return columnNames;
				}

				@Override
				public MetadataBuildingContext getBuildingContext() {
					return mappingDocument;
				}

				@Override
				public Identifier getUserProvidedIdentifier() {
					return uk.getName() != null ? Identifier.toIdentifier( uk.getName() ) : null;
				}
			}
	);
	uk.setName( ukName.render( mappingDocument.getMetadataCollector().getDatabase().getDialect() ) );

	entityBinding.getTable().addUniqueKey( uk );
}
 
Example #13
Source File: SpannerUniqueDelegate.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
  return Index.buildSqlCreateIndexString(
      dialect, uniqueKey.getName(), uniqueKey.getTable(), uniqueKey.columnIterator(),
      uniqueKey.getColumnOrderMap(), true, metadata);
}
 
Example #14
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected void applyUniqueKeys(
		Table table,
		TableInformation tableInfo,
		Dialect dialect,
		Metadata metadata,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	if ( uniqueConstraintStrategy == null ) {
		uniqueConstraintStrategy = determineUniqueConstraintSchemaUpdateStrategy( metadata );
	}

	if ( uniqueConstraintStrategy != UniqueConstraintSchemaUpdateStrategy.SKIP ) {
		final Exporter<Constraint> exporter = dialect.getUniqueKeyExporter();

		final Iterator ukItr = table.getUniqueKeyIterator();
		while ( ukItr.hasNext() ) {
			final UniqueKey uniqueKey = (UniqueKey) ukItr.next();
			// Skip if index already exists. Most of the time, this
			// won't work since most Dialects use Constraints. However,
			// keep it for the few that do use Indexes.
			IndexInformation indexInfo = null;
			if ( tableInfo != null && StringHelper.isNotEmpty( uniqueKey.getName() ) ) {
				indexInfo = tableInfo.getIndex( Identifier.toIdentifier( uniqueKey.getName() ) );
			}
			if ( indexInfo == null ) {
				if ( uniqueConstraintStrategy == UniqueConstraintSchemaUpdateStrategy.DROP_RECREATE_QUIETLY ) {
					applySqlStrings(
							true,
							exporter.getSqlDropStrings( uniqueKey, metadata ),
							formatter,
							options,
							targets
					);
				}

				applySqlStrings(
						true,
						exporter.getSqlCreateStrings( uniqueKey, metadata ),
						formatter,
						options,
						targets
				);
			}
		}
	}
}
 
Example #15
Source File: SpannerUniqueDelegate.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
  StringBuilder buf = new StringBuilder("DROP INDEX ");
  buf.append(dialect.quote(uniqueKey.getName()));
  return buf.toString();
}
 
Example #16
Source File: UniqueDelegate.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get the SQL ALTER TABLE command to be used to create the given UniqueKey.
 *
 * @param uniqueKey The UniqueKey instance.  Contains all information about the columns
 * @param metadata Access to the bootstrap mapping information
 *
 * @return The ALTER TABLE command
 */
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata);
 
Example #17
Source File: UniqueDelegate.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get the SQL ALTER TABLE command to be used to drop the given UniqueKey.
 *
 * @param uniqueKey The UniqueKey instance.  Contains all information about the columns
 * @param metadata Access to the bootstrap mapping information
 *
 * @return The ALTER TABLE command
 */
public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata);