package org.hibernate.boot.model.naming;
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.boot.model.source.spi.AttributePath;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.internal.util.StringHelper;
public class ImplicitNamingStrategyJpaCompliantImpl implements ImplicitNamingStrategy, Serializable {
public static final ImplicitNamingStrategy INSTANCE = new ImplicitNamingStrategyJpaCompliantImpl();
public ImplicitNamingStrategyJpaCompliantImpl() {
}
@Override
public Identifier determinePrimaryTableName(ImplicitEntityNameSource source) {
if ( source == null ) {
throw new HibernateException( "Entity naming information was not provided." );
}
String tableName = transformEntityName( source.getEntityNaming() );
if ( tableName == null ) {
throw new HibernateException( "Could not determine primary table name for entity" );
}
return toIdentifier( tableName, source.getBuildingContext() );
}
protected String transformEntityName(EntityNaming entityNaming) {
if ( StringHelper.isNotEmpty( entityNaming.getJpaEntityName() ) ) {
return entityNaming.getJpaEntityName();
}
else {
return StringHelper.unqualify( entityNaming.getEntityName() );
}
}
@Override
public Identifier determineJoinTableName(ImplicitJoinTableNameSource source) {
final String name = source.getOwningPhysicalTableName()
+ '_'
+ source.getNonOwningPhysicalTableName();
return toIdentifier( name, source.getBuildingContext() );
}
@Override
public Identifier determineCollectionTableName(ImplicitCollectionTableNameSource source) {
final String entityName = transformEntityName( source.getOwningEntityNaming() );
final String name = entityName
+ '_'
+ transformAttributePath( source.getOwningAttributePath() );
return toIdentifier( name, source.getBuildingContext() );
}
@Override
public Identifier determineIdentifierColumnName(ImplicitIdentifierColumnNameSource source) {
return toIdentifier(
transformAttributePath( source.getIdentifierAttributePath() ),
source.getBuildingContext()
);
}
@Override
public Identifier determineDiscriminatorColumnName(ImplicitDiscriminatorColumnNameSource source) {
return toIdentifier(
source.getBuildingContext().getMappingDefaults().getImplicitDiscriminatorColumnName(),
source.getBuildingContext()
);
}
@Override
public Identifier determineTenantIdColumnName(ImplicitTenantIdColumnNameSource source) {
return toIdentifier(
source.getBuildingContext().getMappingDefaults().getImplicitTenantIdColumnName(),
source.getBuildingContext()
);
}
@Override
public Identifier determineBasicColumnName(ImplicitBasicColumnNameSource source) {
return toIdentifier( transformAttributePath( source.getAttributePath() ), source.getBuildingContext() );
}
@Override
public Identifier determineJoinColumnName(ImplicitJoinColumnNameSource source) {
final String name;
if ( source.getNature() == ImplicitJoinColumnNameSource.Nature.ELEMENT_COLLECTION
|| source.getAttributePath() == null ) {
name = transformEntityName( source.getEntityNaming() )
+ '_'
+ source.getReferencedColumnName().getText();
}
else {
name = transformAttributePath( source.getAttributePath() )
+ '_'
+ source.getReferencedColumnName().getText();
}
return toIdentifier( name, source.getBuildingContext() );
}
@Override
public Identifier determinePrimaryKeyJoinColumnName(ImplicitPrimaryKeyJoinColumnNameSource source) {
return source.getReferencedPrimaryKeyColumnName();
}
@Override
public Identifier determineAnyDiscriminatorColumnName(ImplicitAnyDiscriminatorColumnNameSource source) {
return toIdentifier(
transformAttributePath( source.getAttributePath() ) + "_" + source.getBuildingContext().getMappingDefaults().getImplicitDiscriminatorColumnName(),
source.getBuildingContext()
);
}
@Override
public Identifier determineAnyKeyColumnName(ImplicitAnyKeyColumnNameSource source) {
return toIdentifier(
transformAttributePath( source.getAttributePath() ) + "_" + source.getBuildingContext().getMappingDefaults().getImplicitIdColumnName(),
source.getBuildingContext()
);
}
@Override
public Identifier determineMapKeyColumnName(ImplicitMapKeyColumnNameSource source) {
return toIdentifier(
transformAttributePath( source.getPluralAttributePath() ) + "_KEY",
source.getBuildingContext()
);
}
@Override
public Identifier determineListIndexColumnName(ImplicitIndexColumnNameSource source) {
return toIdentifier(
transformAttributePath( source.getPluralAttributePath() ) + "_ORDER",
source.getBuildingContext()
);
}
@Override
public Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source) {
Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();
source.getBuildingContext().getBuildingOptions().getSchemaCharset();
return userProvidedIdentifier != null ? userProvidedIdentifier : toIdentifier(
NamingHelper.withCharset( source.getBuildingContext().getBuildingOptions().getSchemaCharset() ).generateHashedFkName(
"FK",
source.getTableName(),
source.getReferencedTableName(),
source.getColumnNames()
),
source.getBuildingContext()
);
}
@Override
public Identifier determineUniqueKeyName(ImplicitUniqueKeyNameSource source) {
Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();
return userProvidedIdentifier != null ? userProvidedIdentifier : toIdentifier(
NamingHelper.withCharset( source.getBuildingContext().getBuildingOptions().getSchemaCharset() ).generateHashedConstraintName(
"UK",
source.getTableName(),
source.getColumnNames()
),
source.getBuildingContext()
);
}
@Override
public Identifier determineIndexName(ImplicitIndexNameSource source) {
Identifier userProvidedIdentifier = source.getUserProvidedIdentifier();
return userProvidedIdentifier != null ? userProvidedIdentifier : toIdentifier(
NamingHelper.withCharset( source.getBuildingContext().getBuildingOptions().getSchemaCharset() ).generateHashedConstraintName(
"IDX",
source.getTableName(),
source.getColumnNames()
),
source.getBuildingContext()
);
}
protected String transformAttributePath(AttributePath attributePath) {
return attributePath.getProperty();
}
protected Identifier toIdentifier(String stringForm, MetadataBuildingContext buildingContext) {
return buildingContext.getMetadataCollector()
.getDatabase()
.getJdbcEnvironment()
.getIdentifierHelper()
.toIdentifier( stringForm );
}
}