Skip to content

make it easier to customize ImplicitNamingStrategyJpaCompliantImpl #10172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ private void createIndexOrUniqueKey(
String[] columnNames,
String[] orderings,
boolean unique,
boolean declaredAsIndex,
String options,
Selectable[] columns) {
final IndexOrUniqueKeyNameSource source =
new IndexOrUniqueKeyNameSource( context, table, columnNames, originalKeyName );
new IndexOrUniqueKeyNameSource( context, table, columnNames, originalKeyName, declaredAsIndex );
boolean hasFormula = false;
for ( Selectable selectable : columns ) {
if ( selectable.isFormula() ) {
Expand Down Expand Up @@ -212,6 +213,7 @@ void bindIndexes(Table table, jakarta.persistence.Index[] indexes) {
columnExpressions,
ordering,
unique,
true,
options,
selectables( table, name, columnExpressions )
);
Expand All @@ -230,6 +232,7 @@ void bindUniqueConstraints(Table table, UniqueConstraint[] constraints) {
columnNames,
null,
true,
false,
options,
columns( table, name, columnNames )
);
Expand All @@ -255,17 +258,32 @@ else if ( tmp.endsWith( " asc" ) ) {
}
}

private class IndexOrUniqueKeyNameSource implements ImplicitIndexNameSource, ImplicitUniqueKeyNameSource {
private class IndexOrUniqueKeyNameSource
implements ImplicitIndexNameSource, ImplicitUniqueKeyNameSource {
private final MetadataBuildingContext buildingContext;
private final Table table;
private final String[] columnNames;
private final String originalKeyName;
private final boolean declaredAsIndex;

public IndexOrUniqueKeyNameSource(MetadataBuildingContext buildingContext, Table table, String[] columnNames, String originalKeyName) {
private IndexOrUniqueKeyNameSource(
MetadataBuildingContext buildingContext,
Table table,
String[] columnNames,
String originalKeyName,
boolean declaredAsIndex) {
this.buildingContext = buildingContext;
this.table = table;
this.columnNames = columnNames;
this.originalKeyName = originalKeyName;
this.declaredAsIndex = declaredAsIndex;
}

@Override
public Kind kind() {
return declaredAsIndex
? ImplicitIndexNameSource.super.kind()
: ImplicitUniqueKeyNameSource.super.kind();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ public interface ImplicitConstraintNameSource extends ImplicitNameSource {
Identifier getTableName();
List<Identifier> getColumnNames();
Identifier getUserProvidedIdentifier();
Kind kind();

enum Kind {
FOREIGN_KEY,
UNIQUE_KEY,
INDEX
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
public interface ImplicitForeignKeyNameSource extends ImplicitConstraintNameSource {
Identifier getReferencedTableName();
List<Identifier> getReferencedColumnNames();

@Override
default Kind kind() {
return Kind.FOREIGN_KEY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
* @author Steve Ebersole
*/
public interface ImplicitIndexNameSource extends ImplicitConstraintNameSource {
@Override
default Kind kind() {
return Kind.INDEX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.hibernate.HibernateException;
import org.hibernate.boot.model.source.spi.AttributePath;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;

import static org.hibernate.boot.model.naming.ImplicitJoinColumnNameSource.Nature.ELEMENT_COLLECTION;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
Expand Down Expand Up @@ -191,34 +192,27 @@
public Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source) {
final Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();
final MetadataBuildingContext buildingContext = source.getBuildingContext();
return userProvidedIdentifier != null ? userProvidedIdentifier : toIdentifier(
NamingHelper.withCharset( buildingContext.getBuildingOptions().getSchemaCharset() )
.generateHashedFkName( "FK", source.getTableName(),
source.getReferencedTableName(), source.getColumnNames() ),
buildingContext
);
return userProvidedIdentifier != null
? userProvidedIdentifier
: generateConstraintName( source, buildingContext );
}

@Override
public Identifier determineUniqueKeyName(ImplicitUniqueKeyNameSource source) {
final Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();
final MetadataBuildingContext buildingContext = source.getBuildingContext();
return userProvidedIdentifier != null ? userProvidedIdentifier : toIdentifier(
NamingHelper.withCharset( buildingContext.getBuildingOptions().getSchemaCharset() )
.generateHashedConstraintName( "UK", source.getTableName(), source.getColumnNames() ),
buildingContext
);
return userProvidedIdentifier != null
? userProvidedIdentifier
: generateConstraintName( source, buildingContext );
}

@Override
public Identifier determineIndexName(ImplicitIndexNameSource source) {
final Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();
final MetadataBuildingContext buildingContext = source.getBuildingContext();
return userProvidedIdentifier != null ? userProvidedIdentifier : toIdentifier(
NamingHelper.withCharset( buildingContext.getBuildingOptions().getSchemaCharset() )
.generateHashedConstraintName( "IDX", source.getTableName(), source.getColumnNames() ),
buildingContext
);
return userProvidedIdentifier != null
? userProvidedIdentifier
: generateConstraintName( source, buildingContext );
}

/**
Expand All @@ -235,18 +229,54 @@
}

/**
* Easy hook to build an Identifier using the keyword safe IdentifierHelper.
* Easy hook to build an {@link Identifier} using the keyword safe
* {@link org.hibernate.engine.jdbc.env.spi.IdentifierHelper}.
*
* @param stringForm The String form of the name
* @param buildingContext Access to the IdentifierHelper
* @param buildingContext Access to the {@code IdentifierHelper}
*
* @return The identifier
*/
protected Identifier toIdentifier(String stringForm, MetadataBuildingContext buildingContext) {
return buildingContext.getMetadataCollector()
.getDatabase()
.getJdbcEnvironment()
.getIdentifierHelper()
.toIdentifier( stringForm );
return toIdentifier( stringForm,
buildingContext.getMetadataCollector()
.getDatabase()
.getJdbcEnvironment()
.getIdentifierHelper() );
}

/**
* Easy hook to build an {@link Identifier} using the keyword safe
* {@link org.hibernate.engine.jdbc.env.spi.IdentifierHelper}.
*
* @param stringForm The String form of the name
* @param identifierHelper The {@code IdentifierHelper}
*
* @return The identifier
*/
protected Identifier toIdentifier(String stringForm, IdentifierHelper identifierHelper) {
return identifierHelper.toIdentifier( stringForm );
}

protected Identifier generateConstraintName(ImplicitForeignKeyNameSource source, MetadataBuildingContext context) {
return toIdentifier(
NamingHelper.withCharset( context.getBuildingOptions().getSchemaCharset() )
.generateHashedFkName( "FK", source.getTableName(),
source.getReferencedTableName(), source.getColumnNames() ),
context
);
}

protected Identifier generateConstraintName(ImplicitConstraintNameSource source, MetadataBuildingContext context) {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note

Method ImplicitNamingStrategyJpaCompliantImpl.generateConstraintName(..) could be confused with overloaded method
generateConstraintName
, since dispatch depends on static types.
final String prefix = switch (source.kind()) {
case INDEX -> "IDX";
case UNIQUE_KEY -> "UK";
case FOREIGN_KEY -> throw new IllegalArgumentException();
};
return toIdentifier(
NamingHelper.withCharset( context.getBuildingOptions().getSchemaCharset() )
.generateHashedConstraintName( prefix, source.getTableName(), source.getColumnNames() ),
context
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
* @author Steve Ebersole
*/
public interface ImplicitUniqueKeyNameSource extends ImplicitConstraintNameSource {

@Override
default Kind kind() {
return Kind.UNIQUE_KEY;
}
}