cleanups (mostly formatting) around EntityBinder + friends

This commit is contained in:
Gavin King 2024-03-13 18:08:47 +01:00
parent 31b075a274
commit 2f746f8004
6 changed files with 114 additions and 129 deletions

View File

@ -110,6 +110,8 @@ import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.MapsId;
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
/**
* The implementation of the {@linkplain InFlightMetadataCollector in-flight
* metadata collector contract}.
@ -1505,32 +1507,30 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector,
@Override
public void addSecondaryTable(QualifiedTableName logicalQualifiedTableName, Join secondaryTableJoin) {
Identifier logicalName = logicalQualifiedTableName.getTableName();
if ( Identifier.areEqual(
Identifier.toIdentifier(
toIdentifier(
new QualifiedTableName(
Identifier.toIdentifier( primaryTable.getCatalog() ),
Identifier.toIdentifier( primaryTable.getSchema() ),
toIdentifier( primaryTable.getCatalog() ),
toIdentifier( primaryTable.getSchema() ),
primaryTableLogicalName
).render()
),
Identifier.toIdentifier( logicalQualifiedTableName.render() ) ) ) {
throw new DuplicateSecondaryTableException( logicalName );
toIdentifier( logicalQualifiedTableName.render() ) ) ) {
throw new DuplicateSecondaryTableException( logicalQualifiedTableName.getTableName() );
}
if ( secondaryTableJoinMap == null ) {
//secondaryTableJoinMap = new HashMap<Identifier,Join>();
//secondaryTableJoinMap.put( logicalName, secondaryTableJoin );
secondaryTableJoinMap = new HashMap<>();
secondaryTableJoinMap.put( logicalName.getCanonicalName(), secondaryTableJoin );
secondaryTableJoinMap.put( logicalQualifiedTableName.getTableName().getCanonicalName(), secondaryTableJoin );
}
else {
//final Join existing = secondaryTableJoinMap.put( logicalName, secondaryTableJoin );
final Join existing = secondaryTableJoinMap.put( logicalName.getCanonicalName(), secondaryTableJoin );
final Join existing = secondaryTableJoinMap.put( logicalQualifiedTableName.getTableName().getCanonicalName(), secondaryTableJoin );
if ( existing != null ) {
throw new DuplicateSecondaryTableException( logicalName );
throw new DuplicateSecondaryTableException( logicalQualifiedTableName.getTableName() );
}
}
}

View File

@ -686,12 +686,15 @@ public final class AnnotationBinder {
MetadataBuildingContext buildingContext) {
final Map<XClass, InheritanceState> inheritanceStatePerClass = new HashMap<>( orderedClasses.size() );
for ( XClass clazz : orderedClasses ) {
final InheritanceState superclassState = getSuperclassInheritanceState( clazz, inheritanceStatePerClass );
final InheritanceState state = new InheritanceState( clazz, inheritanceStatePerClass, buildingContext );
final InheritanceState superclassState =
getSuperclassInheritanceState( clazz, inheritanceStatePerClass );
final InheritanceState state =
new InheritanceState( clazz, inheritanceStatePerClass, buildingContext );
if ( superclassState != null ) {
//the classes are ordered thus preventing an NPE
superclassState.setHasSiblings( true );
final InheritanceState superEntityState = getInheritanceStateOfSuperEntity( clazz, inheritanceStatePerClass );
final InheritanceState superEntityState =
getInheritanceStateOfSuperEntity( clazz, inheritanceStatePerClass );
state.setHasParents( superEntityState != null );
logMixedInheritance( clazz, superclassState, state );
if ( superclassState.getType() != null ) {

View File

@ -85,7 +85,6 @@ import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.PropertyData;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.OptimisticLockStyle;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.internal.CoreMessageLogger;
@ -136,6 +135,8 @@ import jakarta.persistence.SharedCacheMode;
import jakarta.persistence.UniqueConstraint;
import static jakarta.persistence.InheritanceType.SINGLE_TABLE;
import static org.hibernate.annotations.PolymorphismType.EXPLICIT;
import static org.hibernate.annotations.PolymorphismType.IMPLICIT;
import static org.hibernate.boot.model.internal.AnnotatedClassType.MAPPED_SUPERCLASS;
import static org.hibernate.boot.model.internal.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn;
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildInheritanceJoinColumn;
@ -155,7 +156,9 @@ import static org.hibernate.boot.model.internal.PropertyBinder.hasIdAnnotation;
import static org.hibernate.boot.model.internal.PropertyBinder.processElementAnnotations;
import static org.hibernate.boot.model.internal.PropertyHolderBuilder.buildPropertyHolder;
import static org.hibernate.boot.model.internal.BinderHelper.extractFromPackage;
import static org.hibernate.boot.model.internal.TableBinder.bindForeignKey;
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
import static org.hibernate.engine.OptimisticLockStyle.fromLockType;
import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.fromResultCheckStyle;
import static org.hibernate.internal.util.StringHelper.isEmpty;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
@ -756,9 +759,8 @@ public class EntityBinder {
uniqueConstraints = new UniqueConstraint[0];
}
final InFlightMetadataCollector collector = context.getMetadataCollector();
if ( inheritanceState.hasTable() ) {
createTable( inheritanceState, superEntity, schema, table, catalog, uniqueConstraints, collector );
createTable( inheritanceState, superEntity, schema, table, catalog, uniqueConstraints );
}
else {
// must be a SINGLE_TABLE mapping for a subclass
@ -772,7 +774,7 @@ public class EntityBinder {
}
}
// we at least need to properly set up the EntityTableXref
bindTableForDiscriminatedSubclass( collector.getEntityTableXref( superEntity.getEntityName() ) );
bindTableForDiscriminatedSubclass( superEntity.getEntityName() );
}
}
@ -782,8 +784,7 @@ public class EntityBinder {
String schema,
String table,
String catalog,
UniqueConstraint[] uniqueConstraints,
InFlightMetadataCollector collector) {
UniqueConstraint[] uniqueConstraints) {
final RowId rowId = annotatedClass.getAnnotation( RowId.class );
final View view = annotatedClass.getAnnotation( View.class );
bindTable(
@ -794,7 +795,7 @@ public class EntityBinder {
rowId == null ? null : rowId.value(),
view == null ? null : view.query(),
inheritanceState.hasDenormalizedTable()
? collector.getEntityTableXref( superEntity.getEntityName() )
? context.getMetadataCollector().getEntityTableXref( superEntity.getEntityName() )
: null
);
}
@ -951,12 +952,10 @@ public class EntityBinder {
discriminatorColumn.linkWithValue( discriminatorColumnBinding );
discriminatorColumnBinding.setTypeName( discriminatorColumn.getDiscriminatorTypeName() );
rootClass.setPolymorphic( true );
if ( LOG.isTraceEnabled() ) {
LOG.tracev( "Setting discriminator for entity {0}", rootClass.getEntityName() );
}
context.getMetadataCollector().addSecondPass(
new NullableDiscriminatorColumnSecondPass( rootClass.getEntityName() )
);
final String rootEntityName = rootClass.getEntityName();
LOG.tracev( "Setting discriminator for entity {0}", rootEntityName);
context.getMetadataCollector()
.addSecondPass( new NullableDiscriminatorColumnSecondPass( rootEntityName ) );
}
}
@ -1274,14 +1273,14 @@ public class EntityBinder {
private void bindOptimisticLocking() {
final OptimisticLocking optimisticLockingAnn = annotatedClass.getAnnotation( OptimisticLocking.class );
persistentClass.setOptimisticLockStyle(
getVersioning( optimisticLockingAnn == null ? OptimisticLockType.VERSION : optimisticLockingAnn.type() )
);
persistentClass.setOptimisticLockStyle( fromLockType( optimisticLockingAnn == null
? OptimisticLockType.VERSION
: optimisticLockingAnn.type() ) );
}
private void bindPolymorphism() {
final Polymorphism polymorphismAnn = annotatedClass.getAnnotation( Polymorphism.class );
polymorphismType = polymorphismAnn == null ? PolymorphismType.IMPLICIT : polymorphismAnn.type();
polymorphismType = polymorphismAnn == null ? IMPLICIT : polymorphismAnn.type();
}
private void bindEntityAnnotation() {
@ -1369,7 +1368,7 @@ public class EntityBinder {
private void bindRootEntity() {
final RootClass rootClass = (RootClass) persistentClass;
rootClass.setMutable( isMutable() );
rootClass.setExplicitPolymorphism( isExplicitPolymorphism( polymorphismType ) );
rootClass.setExplicitPolymorphism( polymorphismType == EXPLICIT );
if ( isNotEmpty( where ) ) {
rootClass.setWhere( where );
}
@ -1484,7 +1483,8 @@ public class EntityBinder {
private void bindSynchronize() {
if ( annotatedClass.isAnnotationPresent( Synchronize.class ) ) {
final JdbcEnvironment jdbcEnvironment = context.getMetadataCollector().getDatabase().getJdbcEnvironment();
final JdbcEnvironment jdbcEnvironment =
context.getMetadataCollector().getDatabase().getJdbcEnvironment();
final Synchronize synchronize = annotatedClass.getAnnotation(Synchronize.class);
for ( String table : synchronize.value() ) {
String physicalName = synchronize.logical() ? toPhysicalName( jdbcEnvironment, table ) : table;
@ -1494,11 +1494,10 @@ public class EntityBinder {
}
private String toPhysicalName(JdbcEnvironment jdbcEnvironment, String logicalName) {
final Identifier identifier =
jdbcEnvironment.getIdentifierHelper().toIdentifier( logicalName );
return context.getBuildingOptions().getPhysicalNamingStrategy()
.toPhysicalTableName(
jdbcEnvironment.getIdentifierHelper().toIdentifier( logicalName ),
jdbcEnvironment
)
.toPhysicalTableName( identifier, jdbcEnvironment )
.render( jdbcEnvironment.getDialect() );
}
@ -1564,32 +1563,6 @@ public class EntityBinder {
}
}
private OptimisticLockStyle getVersioning(OptimisticLockType type) {
switch ( type ) {
case VERSION:
return OptimisticLockStyle.VERSION;
case NONE:
return OptimisticLockStyle.NONE;
case DIRTY:
return OptimisticLockStyle.DIRTY;
case ALL:
return OptimisticLockStyle.ALL;
default:
throw new AssertionFailure( "optimistic locking not supported: " + type );
}
}
private boolean isExplicitPolymorphism(PolymorphismType type) {
switch ( type ) {
case IMPLICIT:
return false;
case EXPLICIT:
return true;
default:
throw new AssertionFailure( "Unknown polymorphism type: " + type );
}
}
public void bindProxy() {
final Proxy proxy = annotatedClass.getAnnotation( Proxy.class );
if ( proxy != null ) {
@ -1823,23 +1796,21 @@ public class EntityBinder {
@Override
public Identifier handleExplicitName(String explicitName, MetadataBuildingContext buildingContext) {
return buildingContext.getMetadataCollector()
.getDatabase()
.getJdbcEnvironment()
.getIdentifierHelper()
.toIdentifier( explicitName );
return jdbcEnvironment( buildingContext ).getIdentifierHelper().toIdentifier( explicitName );
}
@Override
public Identifier toPhysicalName(Identifier logicalName, MetadataBuildingContext buildingContext) {
return buildingContext.getBuildingOptions().getPhysicalNamingStrategy().toPhysicalTableName(
logicalName,
buildingContext.getMetadataCollector().getDatabase().getJdbcEnvironment()
);
return buildingContext.getBuildingOptions().getPhysicalNamingStrategy()
.toPhysicalTableName( logicalName, jdbcEnvironment( buildingContext ) );
}
private static JdbcEnvironment jdbcEnvironment(MetadataBuildingContext buildingContext) {
return buildingContext.getMetadataCollector().getDatabase().getJdbcEnvironment();
}
}
public void bindTableForDiscriminatedSubclass(InFlightMetadataCollector.EntityTableXref superTableXref) {
public void bindTableForDiscriminatedSubclass(String entityName) {
if ( !(persistentClass instanceof SingleTableSubclass) ) {
throw new AssertionFailure(
"Was expecting a discriminated subclass [" + SingleTableSubclass.class.getName() +
@ -1848,12 +1819,14 @@ public class EntityBinder {
);
}
context.getMetadataCollector().addEntityTableXref(
final InFlightMetadataCollector collector = context.getMetadataCollector();
final InFlightMetadataCollector.EntityTableXref superTableXref =
collector.getEntityTableXref( entityName );
final Table primaryTable = superTableXref.getPrimaryTable();
collector.addEntityTableXref(
persistentClass.getEntityName(),
context.getMetadataCollector().getDatabase().toIdentifier(
context.getMetadataCollector().getLogicalTableName( superTableXref.getPrimaryTable() )
),
superTableXref.getPrimaryTable(),
collector.getDatabase().toIdentifier( collector.getLogicalTableName( primaryTable ) ),
primaryTable,
superTableXref
);
}
@ -1867,14 +1840,14 @@ public class EntityBinder {
String viewQuery,
InFlightMetadataCollector.EntityTableXref denormalizedSuperTableXref) {
final EntityTableNamingStrategyHelper namingStrategyHelper = new EntityTableNamingStrategyHelper(
persistentClass.getClassName(),
persistentClass.getEntityName(),
name
);
final Identifier logicalName = isNotEmpty( tableName )
? namingStrategyHelper.handleExplicitName( tableName, context )
: namingStrategyHelper.determineImplicitName( context );
final String entityName = persistentClass.getEntityName();
final EntityTableNamingStrategyHelper namingStrategyHelper =
new EntityTableNamingStrategyHelper( persistentClass.getClassName(), entityName, name );
final Identifier logicalName =
isNotEmpty( tableName )
? namingStrategyHelper.handleExplicitName( tableName, context )
: namingStrategyHelper.determineImplicitName( context );
final Table table = TableBinder.buildAndFillTable(
schema,
@ -1895,15 +1868,11 @@ public class EntityBinder {
// table.setComment( comment.value() );
// }
context.getMetadataCollector().addEntityTableXref(
persistentClass.getEntityName(),
logicalName,
table,
denormalizedSuperTableXref
);
context.getMetadataCollector()
.addEntityTableXref( entityName, logicalName, table, denormalizedSuperTableXref );
if ( persistentClass instanceof TableOwner ) {
LOG.debugf( "Bind entity %s on table %s", persistentClass.getEntityName(), table.getName() );
LOG.debugf( "Bind entity %s on table %s", entityName, table.getName() );
( (TableOwner) persistentClass ).setTable( table );
}
else {
@ -1934,15 +1903,18 @@ public class EntityBinder {
}
private void createPrimaryColumnsToSecondaryTable(Object column, PropertyHolder propertyHolder, Join join) {
final PrimaryKeyJoinColumn[] pkColumnsAnn = column instanceof PrimaryKeyJoinColumn[]
? (PrimaryKeyJoinColumn[]) column
: null;
final JoinColumn[] joinColumnsAnn = column instanceof JoinColumn[]
? (JoinColumn[]) column
: null;
final AnnotatedJoinColumns annotatedJoinColumns = pkColumnsAnn == null && joinColumnsAnn == null
? createDefaultJoinColumn( propertyHolder )
: createJoinColumns( propertyHolder, pkColumnsAnn, joinColumnsAnn );
final PrimaryKeyJoinColumn[] pkColumnsAnn =
column instanceof PrimaryKeyJoinColumn[]
? (PrimaryKeyJoinColumn[]) column
: null;
final JoinColumn[] joinColumnsAnn =
column instanceof JoinColumn[]
? (JoinColumn[]) column
: null;
final AnnotatedJoinColumns annotatedJoinColumns =
pkColumnsAnn == null && joinColumnsAnn == null
? createDefaultJoinColumn( propertyHolder )
: createJoinColumns( propertyHolder, pkColumnsAnn, joinColumnsAnn );
for ( AnnotatedJoinColumn joinColumn : annotatedJoinColumns.getJoinColumns() ) {
joinColumn.forceNotNull();
@ -1979,7 +1951,8 @@ public class EntityBinder {
columns.setJoins( secondaryTables );
columns.setPropertyHolder( propertyHolder );
for ( int colIndex = 0; colIndex < joinColumnCount; colIndex++ ) {
final PrimaryKeyJoinColumn primaryKeyJoinColumn = primaryKeyJoinColumns != null ? primaryKeyJoinColumns[colIndex] : null;
final PrimaryKeyJoinColumn primaryKeyJoinColumn =
primaryKeyJoinColumns != null ? primaryKeyJoinColumns[colIndex] : null;
final JoinColumn joinColumn = joinColumns != null ? joinColumns[colIndex] : null;
buildInheritanceJoinColumn(
primaryKeyJoinColumn,
@ -1998,7 +1971,7 @@ public class EntityBinder {
join.setKey( key );
setForeignKeyNameIfDefined( join );
key.setOnDeleteAction( null );
TableBinder.bindForeignKey( persistentClass, null, joinColumns, key, false, context );
bindForeignKey( persistentClass, null, joinColumns, key, false, context );
key.sortProperties();
join.createPrimaryKey();
join.createForeignKey();
@ -2106,16 +2079,16 @@ public class EntityBinder {
}
private static <T extends Annotation> String tableMember(Class<T> annotationType, T sqlAnnotation) {
if (SQLInsert.class.equals(annotationType)) {
if (SQLInsert.class == annotationType) {
return ((SQLInsert) sqlAnnotation).table();
}
else if (SQLUpdate.class.equals(annotationType)) {
else if (SQLUpdate.class == annotationType) {
return ((SQLUpdate) sqlAnnotation).table();
}
else if (SQLDelete.class.equals(annotationType)) {
else if (SQLDelete.class == annotationType) {
return ((SQLDelete) sqlAnnotation).table();
}
else if (SQLDeleteAll.class.equals(annotationType)) {
else if (SQLDeleteAll.class == annotationType) {
return ((SQLDeleteAll) sqlAnnotation).table();
}
else {
@ -2124,13 +2097,13 @@ public class EntityBinder {
}
private static <T extends Annotation> Annotation[] valueMember(Class<T> repeatableType, T sqlAnnotation) {
if (SQLInserts.class.equals(repeatableType)) {
if (SQLInserts.class == repeatableType) {
return ((SQLInserts) sqlAnnotation).value();
}
else if (SQLUpdates.class.equals(repeatableType)) {
else if (SQLUpdates.class == repeatableType) {
return ((SQLUpdates) sqlAnnotation).value();
}
else if (SQLDeletes.class.equals(repeatableType)) {
else if (SQLDeletes.class == repeatableType) {
return ((SQLDeletes) sqlAnnotation).value();
}
else {
@ -2217,9 +2190,10 @@ public class EntityBinder {
final Join join = new Join();
persistentClass.addJoin( join );
final String entityName = persistentClass.getEntityName();
final InFlightMetadataCollector.EntityTableXref tableXref
= context.getMetadataCollector().getEntityTableXref( persistentClass.getEntityName() );
assert tableXref != null : "Could not locate EntityTableXref for entity [" + persistentClass.getEntityName() + "]";
= context.getMetadataCollector().getEntityTableXref( entityName );
assert tableXref != null : "Could not locate EntityTableXref for entity [" + entityName + "]";
tableXref.addSecondaryTable( logicalName, join );
// No check constraints available on joins
@ -2228,7 +2202,7 @@ public class EntityBinder {
// Somehow keep joins() for later.
// Has to do the work later because it needs PersistentClass id!
LOG.debugf( "Adding secondary table to entity %s -> %s",
persistentClass.getEntityName(), join.getTable().getName() );
entityName, join.getTable().getName() );
handleSecondaryRowManagement( join );
processSecondaryTableCustomSql( join );
@ -2273,8 +2247,10 @@ public class EntityBinder {
private void processSecondaryTableCustomSql(Join join) {
final String tableName = join.getTable().getQuotedName();
final org.hibernate.annotations.Table matchingTable = findMatchingComplementaryTableAnnotation( tableName );
final SQLInsert sqlInsert = findMatchingSqlAnnotation( tableName, SQLInsert.class, SQLInserts.class );
final org.hibernate.annotations.Table matchingTable =
findMatchingComplementaryTableAnnotation( tableName );
final SQLInsert sqlInsert =
findMatchingSqlAnnotation( tableName, SQLInsert.class, SQLInserts.class );
if ( sqlInsert != null ) {
join.setCustomSQLInsert(
sqlInsert.sql().trim(),
@ -2296,7 +2272,8 @@ public class EntityBinder {
}
}
final SQLUpdate sqlUpdate = findMatchingSqlAnnotation( tableName, SQLUpdate.class, SQLUpdates.class );
final SQLUpdate sqlUpdate =
findMatchingSqlAnnotation( tableName, SQLUpdate.class, SQLUpdates.class );
if ( sqlUpdate != null ) {
join.setCustomSQLUpdate(
sqlUpdate.sql().trim(),
@ -2318,7 +2295,8 @@ public class EntityBinder {
}
}
final SQLDelete sqlDelete = findMatchingSqlAnnotation( tableName, SQLDelete.class, SQLDeletes.class );
final SQLDelete sqlDelete =
findMatchingSqlAnnotation( tableName, SQLDelete.class, SQLDeletes.class );
if ( sqlDelete != null ) {
join.setCustomSQLDelete(
sqlDelete.sql().trim(),

View File

@ -13,11 +13,7 @@ import org.hibernate.boot.model.naming.Identifier;
*/
public class QualifiedNameImpl extends QualifiedNameParser.NameParts implements QualifiedName {
public QualifiedNameImpl(Namespace.Name schemaName, Identifier objectName) {
this(
schemaName.getCatalog(),
schemaName.getSchema(),
objectName
);
this( schemaName.getCatalog(), schemaName.getSchema(), objectName );
}
public QualifiedNameImpl(Identifier catalogName, Identifier schemaName, Identifier objectName) {

View File

@ -37,6 +37,21 @@ public enum OptimisticLockStyle {
*/
ALL;
public static OptimisticLockStyle fromLockType(OptimisticLockType type) {
switch ( type ) {
case VERSION:
return VERSION;
case NONE:
return NONE;
case DIRTY:
return DIRTY;
case ALL:
return ALL;
default:
throw new AssertionFailure( "Unrecognized OptimisticLockType" );
}
}
public boolean isAllOrDirty() {
return isAll() || isDirty();
}

View File

@ -7,16 +7,13 @@
package org.hibernate.mapping;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Internal;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.ContributableDatabaseObject;
import org.hibernate.boot.model.relational.Namespace;
@ -24,10 +21,6 @@ import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.boot.model.relational.QualifiedTypeName;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.tool.schema.extract.spi.ColumnInformation;
import org.hibernate.tool.schema.extract.spi.TableInformation;
import org.jboss.logging.Logger;
/**
* A mapping model object representing a relational database {@linkplain org.hibernate.annotations.Struct UDT}.