下面列出了怎么用org.hibernate.mapping.Constraint的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
}
}
@Override
public String[] getSqlCreateStrings(Constraint constraint, Metadata metadata) {
return new String[] {
dialect.getUniqueDelegate().getAlterTableToAddUniqueKeyCommand(
(UniqueKey) constraint,
metadata
)
};
}
@Override
public String[] getSqlDropStrings(Constraint constraint, Metadata metadata) {
return new String[] {
dialect.getUniqueDelegate().getAlterTableToDropUniqueKeyCommand(
(UniqueKey) constraint,
metadata
)
};
}
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
);
}
}
}
}
public Exporter<Constraint> getUniqueKeyExporter() {
return uniqueKeyExporter;
}
@Override
public Exporter<Constraint> getUniqueKeyExporter() {
return getInstance().getUniqueKeyExporter();
}