Interface UniqueDelegate
-
- All Known Implementing Classes:
AlterTableUniqueDelegate,AlterTableUniqueIndexDelegate,CreateTableUniqueDelegate,DefaultUniqueDelegate,SkipNullableUniqueDelegate
public interface UniqueDelegateDialect-level delegate responsible for applying unique constraints in DDL. Uniqueness can be specified in any of three ways:-
For single-column constraints, by adding
uniqueto the column definition. SeegetColumnDefinitionUniquenessFragment(org.hibernate.mapping.Column, org.hibernate.boot.model.relational.SqlStringGenerationContext) -
By inclusion of the unique constraint in the
create tablestatement. SeegetTableCreationUniqueConstraintsFragment(org.hibernate.mapping.Table, org.hibernate.boot.model.relational.SqlStringGenerationContext) -
By creation of a unique constraint using separate
alter tablestatements. SeegetAlterTableToAddUniqueKeyCommand(org.hibernate.mapping.UniqueKey, org.hibernate.boot.Metadata, org.hibernate.boot.model.relational.SqlStringGenerationContext). Also, seegetAlterTableToDropUniqueKeyCommand(org.hibernate.mapping.UniqueKey, org.hibernate.boot.Metadata, org.hibernate.boot.model.relational.SqlStringGenerationContext).
CreateTableUniqueDelegatewhere possible. However, for databases where unique constraints may not contain a nullable column, and unique indexes must be used instead, we useAlterTableUniqueIndexDelegate.Hibernate specifies that a unique constraint on a nullable column considers null values to be distinct. Some databases default to the opposite semantic, where null values are considered equal for the purpose of determining uniqueness. This is almost never useful, and is the opposite of what we want when we use a unique constraint on a foreign key to map an optional
OneToOneassociation. Therefore, ourUniqueDelegates must jump through hoops to emulate the sensible semantics specified by ANSI, Hibernate, and common sense, namely, that two null values are distinct. A particularly egregious offender is Sybase, where we must simply skip creating the unique constraint.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description StringgetAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata, SqlStringGenerationContext context)Get thealter tablecommand used to create the given unique key constraint, or return the empty string if the constraint was already included in thecreate tablestatement bygetTableCreationUniqueConstraintsFragment(org.hibernate.mapping.Table, org.hibernate.boot.model.relational.SqlStringGenerationContext).StringgetAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata, SqlStringGenerationContext context)Get thealter tablecommand used to drop the given unique key which was previously created bygetAlterTableToAddUniqueKeyCommand(org.hibernate.mapping.UniqueKey, org.hibernate.boot.Metadata, org.hibernate.boot.model.relational.SqlStringGenerationContext).StringgetColumnDefinitionUniquenessFragment(Column column, SqlStringGenerationContext context)Get the SQL fragment used to make the given column unique as part of its column definition, usually just" unique", or return an empty string if uniqueness does not belong in the column definition.StringgetTableCreationUniqueConstraintsFragment(Table table, SqlStringGenerationContext context)Get the SQL fragment used to specify the unique constraints on the given table as part of thecreate tablecommand, with a leading comma, usually something like:
-
-
-
Method Detail
-
getColumnDefinitionUniquenessFragment
String getColumnDefinitionUniquenessFragment(Column column, SqlStringGenerationContext context)
Get the SQL fragment used to make the given column unique as part of its column definition, usually just" unique", or return an empty string if uniqueness does not belong in the column definition.This is for handling single columns explicitly marked unique, not for dealing with unique keys.
- Parameters:
column- The column to which to apply the uniquecontext- A context for SQL string generation- Returns:
- The fragment (usually "unique"), empty string indicates the uniqueness will be indicated using a different approach
-
getTableCreationUniqueConstraintsFragment
String getTableCreationUniqueConstraintsFragment(Table table, SqlStringGenerationContext context)
Get the SQL fragment used to specify the unique constraints on the given table as part of thecreate tablecommand, with a leading comma, usually something like:, unique(x,y), constraint abc unique(a,b,c)or return an empty string if there are no unique constraints or if the unique constraints do not belong in the table definition.
The implementation should iterate over the unique keys of the given table by calling
Table.getUniqueKeys()and generate a fragment which includes all the unique key declarations.- Parameters:
table- The table for which to generate the unique constraints fragmentcontext- A context for SQL string generation- Returns:
- The fragment, typically in the form
", unique(col1, col2), unique(col20)". The leading comma is important!
-
getAlterTableToAddUniqueKeyCommand
String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata, SqlStringGenerationContext context)
Get thealter tablecommand used to create the given unique key constraint, or return the empty string if the constraint was already included in thecreate tablestatement bygetTableCreationUniqueConstraintsFragment(org.hibernate.mapping.Table, org.hibernate.boot.model.relational.SqlStringGenerationContext).- Parameters:
uniqueKey- TheUniqueKeyinstance. Contains all information about the columnsmetadata- Access to the bootstrap mapping informationcontext- A context for SQL string generation- Returns:
- The
alter tablecommand
-
getAlterTableToDropUniqueKeyCommand
String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata, SqlStringGenerationContext context)
Get thealter tablecommand used to drop the given unique key which was previously created bygetAlterTableToAddUniqueKeyCommand(org.hibernate.mapping.UniqueKey, org.hibernate.boot.Metadata, org.hibernate.boot.model.relational.SqlStringGenerationContext).- Parameters:
uniqueKey- TheUniqueKeyinstance. Contains all information about the columnsmetadata- Access to the bootstrap mapping informationcontext- A context for SQL string generation- Returns:
- The
alter tablecommand
-
-