diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/FilterDef.java b/hibernate-core/src/main/java/org/hibernate/annotations/FilterDef.java index 0d615527c8d6..b8e4eb63b61d 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/FilterDef.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/FilterDef.java @@ -97,7 +97,7 @@ ParamDef[] parameters() default {}; /** - * Specifies that the filter auto-enabled, so that it is + * Specifies that the filter is auto-enabled, so that it is * not necessary to call * {@link org.hibernate.Session#enableFilter(String)}. *

diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnyBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnyBinder.java index c9ffc5bb8f9a..e450604b0031 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnyBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnyBinder.java @@ -44,8 +44,8 @@ static void bindAny( EntityBinder entityBinder, boolean isIdentifierMapper, MetadataBuildingContext context, - MemberDetails property, AnnotatedJoinColumns joinColumns) { + final MemberDetails property = inferredData.getAttributeMember(); //check validity if ( property.hasDirectAnnotationUsage( Columns.class ) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java index c2e626cd2b2b..88b4c7e76f66 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java @@ -214,9 +214,9 @@ public static void bindCollection( boolean isIdentifierMapper, MetadataBuildingContext context, Map inheritanceStatePerClass, - MemberDetails property, AnnotatedJoinColumns joinColumns) { final ModelsContext modelsContext = context.getBootstrapContext().getModelsContext(); + final MemberDetails property = inferredData.getAttributeMember(); final OneToMany oneToManyAnn = property.getAnnotationUsage( OneToMany.class, modelsContext ); final ManyToMany manyToManyAnn = property.getAnnotationUsage( ManyToMany.class, modelsContext ); @@ -224,7 +224,7 @@ public static void bindCollection( checkAnnotations( propertyHolder, inferredData, property, oneToManyAnn, manyToManyAnn, elementCollectionAnn ); final CollectionBinder collectionBinder = getCollectionBinder( property, hasMapKeyAnnotation( property ), context ); - collectionBinder.setIndexColumn( getIndexColumn( propertyHolder, inferredData, entityBinder, context, property ) ); + collectionBinder.setIndexColumn( getIndexColumn( propertyHolder, inferredData, entityBinder, context ) ); collectionBinder.setMapKey( property.getAnnotationUsage( MapKey.class, modelsContext ) ); collectionBinder.setPropertyName( inferredData.getPropertyName() ); collectionBinder.setJpaOrderBy( property.getAnnotationUsage( OrderBy.class, modelsContext ) ); @@ -423,8 +423,8 @@ private static IndexColumn getIndexColumn( PropertyHolder propertyHolder, PropertyData inferredData, EntityBinder entityBinder, - MetadataBuildingContext context, - MemberDetails property) { + MetadataBuildingContext context) { + final MemberDetails property = inferredData.getAttributeMember(); return IndexColumn.fromAnnotations( property.getDirectAnnotationUsage( OrderColumn.class ), property.getDirectAnnotationUsage( ListIndexBase.class ), diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/OneToOneSecondPass.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/OneToOneSecondPass.java index be58e5c25db2..371a59eb2f9f 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/OneToOneSecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/OneToOneSecondPass.java @@ -232,7 +232,7 @@ private ManyToOne createManyToOne(OneToOne oneToOne, Join mappedByJoin) { private Property targetProperty(OneToOne oneToOne, PersistentClass targetEntity) { try { - Property targetProperty = findPropertyByName( targetEntity, mappedBy ); + final Property targetProperty = findPropertyByName( targetEntity, mappedBy ); if ( targetProperty != null ) { return targetProperty; } @@ -272,13 +272,14 @@ private void bindOwned( * the owning side. */ private Join buildJoinFromMappedBySide(PersistentClass persistentClass, Property otherSideProperty, Join originalJoin) { - Join join = new Join(); + final Join join = new Join(); join.setPersistentClass( persistentClass ); //no check constraints available on joins join.setTable( originalJoin.getTable() ); join.setInverse( true ); - DependantValue key = new DependantValue( buildingContext, join.getTable(), persistentClass.getIdentifier() ); + final DependantValue key = + new DependantValue( buildingContext, join.getTable(), persistentClass.getIdentifier() ); if ( notFoundAction != null ) { join.disableForeignKeyCreation(); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java index 664f75df6408..ada7ae1230d9 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java @@ -725,8 +725,7 @@ public static void processElementAnnotations( isComponentEmbedded, inSecondPass, context, - inheritanceStatePerClass, - property + inheritanceStatePerClass ); } } @@ -756,8 +755,9 @@ private static void buildProperty( boolean isComponentEmbedded, boolean inSecondPass, MetadataBuildingContext context, - Map inheritanceStatePerClass, - MemberDetails property) { + Map inheritanceStatePerClass) { + + final MemberDetails property = inferredData.getAttributeMember(); if ( isPropertyOfRegularEmbeddable( propertyHolder, isComponentEmbedded ) && property.hasDirectAnnotationUsage(Id.class)) { @@ -777,7 +777,6 @@ private static void buildProperty( isIdentifierMapper, context, inheritanceStatePerClass, - property, attributeTypeDetails ); @@ -798,7 +797,6 @@ private static void buildProperty( isIdentifierMapper, isComponentEmbedded, inSecondPass, - property, attributeTypeDetails.determineRawClass(), columnsBuilder ); @@ -812,8 +810,8 @@ private static PropertyBinder propertyBinder( boolean isIdentifierMapper, MetadataBuildingContext context, Map inheritanceStatePerClass, - MemberDetails property, TypeDetails attributeTypeDetails) { + final MemberDetails property = inferredData.getAttributeMember(); final PropertyBinder propertyBinder = new PropertyBinder(); propertyBinder.setName( inferredData.getPropertyName() ); propertyBinder.setReturnedClassName( inferredData.getTypeName() ); @@ -847,9 +845,9 @@ private AnnotatedColumns bindProperty( boolean isIdentifierMapper, boolean isComponentEmbedded, boolean inSecondPass, - MemberDetails property, ClassDetails returnedClass, ColumnsBuilder columnsBuilder) { + final MemberDetails property = inferredData.getAttributeMember(); if ( isVersion( property ) ) { bindVersionProperty( propertyHolder, @@ -865,7 +863,6 @@ else if ( isManyToOne( property ) ) { isIdentifierMapper, inSecondPass, buildingContext, - property, columnsBuilder.getJoinColumns(), this ); @@ -877,7 +874,6 @@ else if ( isOneToOne( property ) ) { isIdentifierMapper, inSecondPass, buildingContext, - property, columnsBuilder.getJoinColumns(), this ); @@ -890,7 +886,6 @@ else if ( isAny( property ) ) { entityBinder, isIdentifierMapper, buildingContext, - property, columnsBuilder.getJoinColumns() ); } @@ -903,7 +898,6 @@ else if ( isCollection( property ) ) { isIdentifierMapper, buildingContext, inheritanceStatePerClass, - property, columnsBuilder.getJoinColumns() ); } @@ -917,7 +911,6 @@ else if ( !isId() || !entityBinder.isIgnoreIdAnnotations() ) { entityBinder, isIdentifierMapper, isComponentEmbedded, - property, columnsBuilder, columnsBuilder.getColumns(), returnedClass @@ -1002,10 +995,10 @@ private AnnotatedColumns bindBasicOrComposite( EntityBinder entityBinder, boolean isIdentifierMapper, boolean isComponentEmbedded, - MemberDetails property, ColumnsBuilder columnsBuilder, AnnotatedColumns columns, ClassDetails returnedClass) { + final MemberDetails property = inferredData.getAttributeMember(); // overrides from @MapsId or @IdClass if needed final PropertyData overridingProperty = @@ -1030,7 +1023,6 @@ private AnnotatedColumns bindBasicOrComposite( isComposite, isIdentifierMapper, isComponentEmbedded, - property, columns, returnedClass, actualColumns, @@ -1075,7 +1067,6 @@ private PropertyBinder propertyBinder( boolean isComposite, boolean isIdentifierMapper, boolean isComponentEmbedded, - MemberDetails property, AnnotatedColumns columns, ClassDetails returnedClass, AnnotatedColumns actualColumns, @@ -1084,6 +1075,7 @@ private PropertyBinder propertyBinder( final Class> compositeUserType = resolveCompositeUserType( inferredData, buildingContext ); + final MemberDetails property = inferredData.getAttributeMember(); if ( isComposite || compositeUserType != null ) { if ( property.isArray() && property.getElementType() != null && isEmbedded( property, property.getElementType() ) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ToOneBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ToOneBinder.java index 43f8db02a824..4ca09f8bfeb2 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ToOneBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/ToOneBinder.java @@ -4,9 +4,7 @@ */ package org.hibernate.boot.model.internal; -import java.util.ArrayList; import java.util.EnumSet; -import java.util.List; import org.hibernate.AnnotationException; import org.hibernate.AssertionFailure; @@ -29,6 +27,7 @@ import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.ToOne; import org.hibernate.models.spi.ClassDetails; +import org.hibernate.models.spi.ClassDetailsRegistry; import org.hibernate.models.spi.MemberDetails; import org.hibernate.models.spi.ModelsContext; @@ -76,9 +75,9 @@ static void bindManyToOne( boolean isIdentifierMapper, boolean inSecondPass, MetadataBuildingContext context, - MemberDetails property, AnnotatedJoinColumns joinColumns, PropertyBinder propertyBinder) { + final MemberDetails property = inferredData.getAttributeMember(); final ManyToOne manyToOne = property.getDirectAnnotationUsage( ManyToOne.class ); //check validity @@ -99,21 +98,13 @@ && isIdentifier( propertyHolder, propertyBinder, isIdentifierMapper ) ) { } final Cascade hibernateCascade = property.getDirectAnnotationUsage( Cascade.class ); - final NotFound notFound = property.getDirectAnnotationUsage( NotFound.class ); - final NotFoundAction notFoundAction = notFound == null ? null : notFound.action(); - matchIgnoreNotFoundWithFetchType( propertyHolder.getEntityName(), property.getName(), notFoundAction, manyToOne.fetch() ); - final OnDelete onDelete = property.getDirectAnnotationUsage( OnDelete.class ); - final JoinTable joinTable = propertyHolder.getJoinTable( property ); bindManyToOne( aggregateCascadeTypes( manyToOne.cascade(), hibernateCascade, false, context ), joinColumns, - joinTable, - !isMandatory( manyToOne.optional(), property, notFoundAction ), - notFoundAction, - onDelete == null ? null : onDelete.action(), - getTargetEntity( inferredData, context ), propertyHolder, inferredData, + manyToOne.fetch(), + manyToOne.optional(), false, isIdentifierMapper, inSecondPass, @@ -148,55 +139,38 @@ private static boolean isMandatory(boolean optional, MemberDetails property, Not private static void bindManyToOne( EnumSet cascadeStrategy, AnnotatedJoinColumns joinColumns, - JoinTable joinTable, - boolean optional, - NotFoundAction notFoundAction, - OnDeleteAction onDeleteAction, - ClassDetails targetEntity, PropertyHolder propertyHolder, PropertyData inferredData, + FetchType fetchType, + boolean explicitlyOptional, boolean unique, // identifies a "logical" @OneToOne boolean isIdentifierMapper, boolean inSecondPass, PropertyBinder propertyBinder, MetadataBuildingContext context) { - if ( joinTable != null && !isBlank( joinTable.name() ) ) { - final Join join = propertyHolder.addJoin( joinTable, false ); - // TODO: if notFoundAction!=null should we call join.disableForeignKeyCreation() ? - for ( AnnotatedJoinColumn joinColumn : joinColumns.getJoinColumns() ) { - joinColumn.setExplicitTableName( join.getTable().getName() ); - } - if ( notFoundAction != null ) { - join.disableForeignKeyCreation(); - } - } + final MemberDetails property = inferredData.getAttributeMember(); - // All FK columns should be in the same table - final org.hibernate.mapping.ManyToOne value = - new org.hibernate.mapping.ManyToOne( context, joinColumns.getTable() ); + final NotFoundAction notFoundAction = notFoundAction( propertyHolder, property, fetchType ); + final OnDeleteAction onDeleteAction = onDeleteAction( property ); - if ( joinTable != null && isBlank( joinTable.name() ) ) { - context.getMetadataCollector().addSecondPass( new ImplicitToOneJoinTableSecondPass( - propertyHolder, - inferredData, - context, - joinColumns, - joinTable, - notFoundAction, - value - ) ); - } + final boolean optional = !isMandatory( explicitlyOptional, property, notFoundAction ); + + final JoinTable joinTable = propertyHolder.getJoinTable( property ); + final org.hibernate.mapping.ManyToOne manyToOne = + joinTable == null + ? new org.hibernate.mapping.ManyToOne( context, joinColumns.getTable() ) + : handleJoinTable( joinColumns, joinTable, notFoundAction, propertyHolder, inferredData, context ); if ( unique ) { // This is a @OneToOne mapped to a physical o.h.mapping.ManyToOne - value.markAsLogicalOneToOne(); + manyToOne.markAsLogicalOneToOne(); } - value.setReferencedEntityName( getReferenceEntityName( inferredData, targetEntity ) ); - final MemberDetails property = inferredData.getAttributeMember(); - defineFetchingStrategy( value, property, inferredData, propertyHolder ); + final ClassDetails targetEntity = getTargetEntity( inferredData, context ); + manyToOne.setReferencedEntityName( getReferenceEntityName( inferredData, targetEntity ) ); + defineFetchingStrategy( manyToOne, property, inferredData, propertyHolder ); //value.setFetchMode( fetchMode ); - value.setNotFoundAction( notFoundAction ); - value.setOnDeleteAction( onDeleteAction ); + manyToOne.setNotFoundAction( notFoundAction ); + manyToOne.setOnDeleteAction( onDeleteAction ); //value.setLazy( fetchMode != FetchMode.JOIN ); if ( !optional ) { for ( AnnotatedJoinColumn column : joinColumns.getJoinColumns() ) { @@ -205,26 +179,24 @@ private static void bindManyToOne( } if ( property.hasDirectAnnotationUsage( MapsId.class ) ) { - final MapsId mapsId = property.getDirectAnnotationUsage( MapsId.class ); - final List joinColumnList = joinColumns.getJoinColumns(); - //read only - for ( AnnotatedJoinColumn column : joinColumnList ) { + // read-only + for ( AnnotatedJoinColumn column : joinColumns.getJoinColumns() ) { column.setInsertable( false ); column.setUpdatable( false ); } - joinColumns.setMapsId( mapsId.value() ); + joinColumns.setMapsId( property.getDirectAnnotationUsage( MapsId.class ).value() ); } - value.setTypeName( inferredData.getClassOrElementName() ); + manyToOne.setTypeName( inferredData.getClassOrElementName() ); final String propertyName = inferredData.getPropertyName(); - value.setTypeUsingReflection( propertyHolder.getClassName(), propertyName ); + manyToOne.setTypeUsingReflection( propertyHolder.getClassName(), propertyName ); final String fullPath = qualify( propertyHolder.getPath(), propertyName ); - bindForeignKeyNameAndDefinition( value, property, propertyHolder.getOverriddenForeignKey( fullPath ), context ); + bindForeignKeyNameAndDefinition( manyToOne, property, propertyHolder.getOverriddenForeignKey( fullPath ), context ); final FkSecondPass secondPass = new ToOneFkSecondPass( - value, + manyToOne, joinColumns, unique, isTargetAnnotatedEntity( targetEntity, property ), @@ -246,12 +218,62 @@ private static void bindManyToOne( inferredData, isIdentifierMapper, propertyBinder, - value, + manyToOne, property, propertyName ); } + private static OnDeleteAction onDeleteAction(MemberDetails property) { + final OnDelete onDelete = property.getDirectAnnotationUsage( OnDelete.class ); + return onDelete == null ? null : onDelete.action(); + } + + private static NotFoundAction notFoundAction(PropertyHolder propertyHolder, MemberDetails property, FetchType fetchType) { + final NotFound notFound = property.getDirectAnnotationUsage( NotFound.class ); + final NotFoundAction notFoundAction = notFound == null ? null : notFound.action(); + if ( notFoundAction != null && fetchType == LAZY ) { + LOG.ignoreNotFoundWithFetchTypeLazy( propertyHolder.getEntityName(), property.getName() ); + } + return notFoundAction; + } + + private static org.hibernate.mapping.ManyToOne handleJoinTable( + AnnotatedJoinColumns joinColumns, + JoinTable joinTable, + NotFoundAction notFoundAction, + PropertyHolder propertyHolder, + PropertyData inferredData, + MetadataBuildingContext context) { + if ( isBlank( joinTable.name() ) ) { + // We don't yet know the name of the join table this will map to, + // so, for now, just treat it as if it maps to the main table + var value = new org.hibernate.mapping.ManyToOne( context, joinColumns.getTable() ); + context.getMetadataCollector() + .addSecondPass( new ImplicitToOneJoinTableSecondPass( + propertyHolder, + inferredData, + context, + joinColumns, + joinTable, + notFoundAction, + value + ) ); + return value; + } + else { + final Join join = propertyHolder.addJoin( joinTable, false ); + for ( AnnotatedJoinColumn joinColumn : joinColumns.getJoinColumns() ) { + joinColumn.setExplicitTableName( join.getTable().getName() ); + } + if ( notFoundAction != null ) { + join.disableForeignKeyCreation(); + } + // All FK columns should be in the same table + return new org.hibernate.mapping.ManyToOne( context, joinColumns.getTable() ); + } + } + static boolean isTargetAnnotatedEntity(ClassDetails targetEntity, MemberDetails property) { final ClassDetails target = isDefault( targetEntity ) ? property.getType().determineRawClass() : targetEntity; return target.hasDirectAnnotationUsage( Entity.class ); @@ -395,9 +417,9 @@ static void bindOneToOne( boolean isIdentifierMapper, boolean inSecondPass, MetadataBuildingContext context, - MemberDetails property, AnnotatedJoinColumns joinColumns, PropertyBinder propertyBinder) { + final MemberDetails property = inferredData.getAttributeMember(); final OneToOne oneToOne = property.getDirectAnnotationUsage( OneToOne.class ); //check validity @@ -421,22 +443,11 @@ static void bindOneToOne( final boolean trueOneToOne = property.hasDirectAnnotationUsage( PrimaryKeyJoinColumn.class ) || property.hasDirectAnnotationUsage( PrimaryKeyJoinColumns.class ); final Cascade hibernateCascade = property.getDirectAnnotationUsage( Cascade.class ); - final NotFound notFound = property.getDirectAnnotationUsage( NotFound.class ); - final NotFoundAction notFoundAction = notFound == null ? null : notFound.action(); - - matchIgnoreNotFoundWithFetchType( propertyHolder.getEntityName(), property.getName(), notFoundAction, oneToOne.fetch() ); - final OnDelete onDelete = property.getDirectAnnotationUsage( OnDelete.class ); - final JoinTable joinTable = propertyHolder.getJoinTable( property ); bindOneToOne( aggregateCascadeTypes( oneToOne.cascade(), hibernateCascade, oneToOne.orphanRemoval(), context ), joinColumns, - joinTable, - !isMandatory( oneToOne.optional(), property, notFoundAction ), - getFetchMode( oneToOne.fetch() ), - notFoundAction, - onDelete == null ? null : onDelete.action(), - getTargetEntity( inferredData, context ), - property, + oneToOne.optional(), + oneToOne.fetch(), propertyHolder, inferredData, nullIfEmpty( oneToOne.mappedBy() ), @@ -451,13 +462,8 @@ static void bindOneToOne( private static void bindOneToOne( EnumSet cascadeStrategy, AnnotatedJoinColumns joinColumns, - JoinTable joinTable, - boolean optional, - FetchMode fetchMode, - NotFoundAction notFoundAction, - OnDeleteAction cascadeOnDelete, - ClassDetails targetEntity, - MemberDetails annotatedProperty, + boolean explicitlyOptional, + FetchType fetchMode, PropertyHolder propertyHolder, PropertyData inferredData, String mappedBy, @@ -467,44 +473,28 @@ private static void bindOneToOne( PropertyBinder propertyBinder, MetadataBuildingContext context) { //column.getTable() => persistentClass.getTable() - final String propertyName = inferredData.getPropertyName(); - LOG.tracev( "Fetching {0} with {1}", propertyName, fetchMode ); - if ( isMapToPK( joinColumns, propertyHolder, trueOneToOne ) - || mappedBy != null ) { - //is a true one-to-one - //FIXME referencedColumnName ignored => ordering may fail. - final OneToOneSecondPass secondPass = new OneToOneSecondPass( - mappedBy, - propertyHolder.getEntityName(), - propertyHolder, - inferredData, - getReferenceEntityName( inferredData, targetEntity ), - isTargetAnnotatedEntity( targetEntity, annotatedProperty ), - notFoundAction, - cascadeOnDelete, - optional, + if ( mappedBy != null || isMappedToPrimaryKey( joinColumns, propertyHolder, trueOneToOne ) ) { + bindTrueOneToOne( cascadeStrategy, joinColumns, + explicitlyOptional, + fetchMode, + propertyHolder, + inferredData, + mappedBy, + inSecondPass, context ); - if ( inSecondPass ) { - secondPass.doSecondPass( context.getMetadataCollector().getEntityBindingMap() ); - } - else { - context.getMetadataCollector().addSecondPass( secondPass, mappedBy == null ); - } } else { //has a FK on the table bindManyToOne( cascadeStrategy, joinColumns, - joinTable, optional, - notFoundAction, - cascadeOnDelete, - targetEntity, propertyHolder, inferredData, + fetchMode, + explicitlyOptional, true, isIdentifierMapper, inSecondPass, @@ -514,34 +504,71 @@ private static void bindOneToOne( } } - private static boolean isMapToPK(AnnotatedJoinColumns joinColumns, PropertyHolder propertyHolder, boolean trueOneToOne) { + private static void bindTrueOneToOne( + EnumSet cascadeStrategy, + AnnotatedJoinColumns joinColumns, + boolean explicitlyOptional, + FetchType fetchMode, + PropertyHolder propertyHolder, + PropertyData inferredData, + String mappedBy, + boolean inSecondPass, + MetadataBuildingContext context) { + //is a true one-to-one + //FIXME referencedColumnName ignored => ordering may fail. + final MemberDetails property = inferredData.getAttributeMember(); + final ClassDetails targetEntity = getTargetEntity( inferredData, context ); + final NotFoundAction notFoundAction = notFoundAction( propertyHolder, property, fetchMode ); + final OneToOneSecondPass secondPass = new OneToOneSecondPass( + mappedBy, + propertyHolder.getEntityName(), + propertyHolder, + inferredData, + getReferenceEntityName( inferredData, targetEntity ), + isTargetAnnotatedEntity( targetEntity, property ), + notFoundAction, + onDeleteAction( property ), + !isMandatory( explicitlyOptional, property, notFoundAction ), + cascadeStrategy, + joinColumns, + context + ); + if ( inSecondPass ) { + secondPass.doSecondPass( context.getMetadataCollector().getEntityBindingMap() ); + } + else { + context.getMetadataCollector().addSecondPass( secondPass, mappedBy == null ); + } + } + + private static boolean isMappedToPrimaryKey( + AnnotatedJoinColumns joinColumns, + PropertyHolder propertyHolder, + boolean trueOneToOne) { if ( trueOneToOne ) { return true; } else { - //try to find a hidden true one to one (FK == PK columns) + // Try to find a hidden true one-to-one (FK == PK columns) final KeyValue identifier = propertyHolder.getIdentifier(); if ( identifier == null ) { - //this is a @OneToOne in an @EmbeddedId (the persistentClass.identifier is not set yet, it's being built) - //by definition the PK cannot refer to itself so it cannot map to itself + // This is a @OneToOne in an @EmbeddedId + // (the persistentClass.identifier is not yet set, it's being built) + // By definition, the PK cannot refer to itself, so it cannot map to itself return false; } else { - final List idColumnNames = new ArrayList<>(); - final List columns = joinColumns.getJoinColumns(); + final var columns = joinColumns.getJoinColumns(); if ( identifier.getColumnSpan() != columns.size() ) { return false; } else { - for ( org.hibernate.mapping.Column currentColumn: identifier.getColumns() ) { - idColumnNames.add( currentColumn.getName() ); - } - for ( AnnotatedJoinColumn column: columns ) { - if ( !idColumnNames.contains( column.getMappingColumn().getName() ) ) { - return false; - } - } - return true; + final var identifierColumns = identifier.getColumns(); + return columns.stream().noneMatch( column -> { + final String name = column.getMappingColumn().getName(); + return identifierColumns.stream() + .noneMatch( idColumn -> idColumn.getName().equals( name ) ); + } ); } } } @@ -607,25 +634,16 @@ public static ClassDetails getTargetEntity(PropertyData propertyData, MetadataBu } private static ClassDetails getTargetEntityClass(MemberDetails property, MetadataBuildingContext context) { - final ModelsContext modelsContext = context.getBootstrapContext().getModelsContext(); + final ClassDetailsRegistry classDetailsRegistry = + context.getBootstrapContext().getModelsContext().getClassDetailsRegistry(); final ManyToOne manyToOne = property.getDirectAnnotationUsage( ManyToOne.class ); if ( manyToOne != null ) { - return modelsContext.getClassDetailsRegistry().resolveClassDetails( manyToOne.targetEntity().getName() ); + return classDetailsRegistry.resolveClassDetails( manyToOne.targetEntity().getName() ); } final OneToOne oneToOne = property.getDirectAnnotationUsage( OneToOne.class ); if ( oneToOne != null ) { - return modelsContext.getClassDetailsRegistry().resolveClassDetails( oneToOne.targetEntity().getName() ); + return classDetailsRegistry.resolveClassDetails( oneToOne.targetEntity().getName() ); } throw new AssertionFailure( "Unexpected discovery of a targetEntity: " + property.getName() ); } - - private static void matchIgnoreNotFoundWithFetchType( - String entity, - String association, - NotFoundAction notFoundAction, - FetchType fetchType) { - if ( notFoundAction != null && fetchType == LAZY ) { - LOG.ignoreNotFoundWithFetchTypeLazy( entity, association ); - } - } }