From 4d78d0f0dfd910dbd3c64cdded3321c4d832ae76 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 25 Aug 2025 19:36:30 +1000 Subject: [PATCH 1/2] some nice minor refactorings to binders --- .../boot/model/internal/BinderHelper.java | 29 ++++++- .../boot/model/internal/EntityBinder.java | 78 +++++++------------ .../boot/model/internal/ToOneBinder.java | 53 +++++-------- 3 files changed, 76 insertions(+), 84 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java index 182f84bac504..0b980091f048 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java @@ -35,6 +35,7 @@ import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Property; import org.hibernate.mapping.Selectable; +import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.SyntheticProperty; import org.hibernate.mapping.ToOne; import org.hibernate.mapping.Value; @@ -65,6 +66,7 @@ import static org.hibernate.boot.model.internal.ForeignKeyType.NON_PRIMARY_KEY_REFERENCE; import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER; import static org.hibernate.internal.util.StringHelper.isEmpty; +import static org.hibernate.internal.util.StringHelper.nullIfEmpty; import static org.hibernate.internal.util.StringHelper.qualifier; import static org.hibernate.internal.util.StringHelper.qualify; import static org.hibernate.internal.util.collections.ArrayHelper.isEmpty; @@ -1003,7 +1005,7 @@ private static boolean checkReferencedClass(PersistentClass ownerClass, Persiste return false; } - public static boolean noConstraint(ForeignKey foreignKey, boolean noConstraintByDefault) { + static boolean noConstraint(ForeignKey foreignKey, boolean noConstraintByDefault) { if ( foreignKey == null ) { return false; } @@ -1014,6 +1016,31 @@ public static boolean noConstraint(ForeignKey foreignKey, boolean noConstraintBy } } + static void handleForeignKeyConstraint( + SimpleValue key, + ForeignKey foreignKey, + ForeignKey nestedForeignKey, + MetadataBuildingContext context) { + final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault(); + if ( noConstraint( foreignKey, noConstraintByDefault ) + || noConstraint( nestedForeignKey, noConstraintByDefault ) ) { + key.disableForeignKey(); + } + else if ( foreignKey != null ) { + key.setForeignKeyName( nullIfEmpty( foreignKey.name() ) ); + key.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) ); + key.setForeignKeyOptions( foreignKey.options() ); + } + else if ( noConstraintByDefault ) { + key.disableForeignKey(); + } + else if ( nestedForeignKey != null ) { + key.setForeignKeyName( nullIfEmpty( nestedForeignKey.name() ) ); + key.setForeignKeyDefinition( nullIfEmpty( nestedForeignKey.foreignKeyDefinition() ) ); + key.setForeignKeyOptions( nestedForeignKey.options() ); + } + } + /** * Extract an annotation from the package-info for the package the given class is defined in * diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java index 507a697f9441..557a6e8e6bd0 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java @@ -16,6 +16,7 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.IdClass; import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; import jakarta.persistence.MappedSuperclass; @@ -93,8 +94,8 @@ import static org.hibernate.boot.model.internal.BinderHelper.extractFromPackage; import static org.hibernate.boot.model.internal.BinderHelper.getMappedSuperclassOrNull; import static org.hibernate.boot.model.internal.BinderHelper.getPath; +import static org.hibernate.boot.model.internal.BinderHelper.handleForeignKeyConstraint; import static org.hibernate.boot.model.internal.BinderHelper.hasToOneAnnotation; -import static org.hibernate.boot.model.internal.BinderHelper.noConstraint; import static org.hibernate.boot.model.internal.BinderHelper.toAliasEntityMap; import static org.hibernate.boot.model.internal.BinderHelper.toAliasTableMap; import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverridableAnnotation; @@ -505,8 +506,7 @@ private boolean mapAsIdClass( classWithIdClass, compositeType, baseInferredData, - propertyAccessor, - true + propertyAccessor ); if ( idClassComponent.isSimpleRecord() ) { mapper.setSimpleRecord( true ); @@ -552,8 +552,7 @@ private Component createMapperProperty( ClassDetails classWithIdClass, TypeDetails compositeClass, PropertyData baseInferredData, - AccessType propertyAccessor, - boolean isIdClass) { + AccessType propertyAccessor) { final var mapper = createMapper( inheritanceStates, persistentClass, @@ -562,8 +561,7 @@ private Component createMapperProperty( classWithIdClass, compositeClass, baseInferredData, - propertyAccessor, - isIdClass + propertyAccessor ); final var mapperProperty = new SyntheticProperty(); mapperProperty.setName( NavigablePath.IDENTIFIER_MAPPER_PROPERTY ); @@ -583,8 +581,7 @@ private Component createMapper( ClassDetails classWithIdClass, TypeDetails compositeClass, PropertyData baseInferredData, - AccessType propertyAccessor, - boolean isIdClass) { + AccessType propertyAccessor) { final var mapper = fillEmbeddable( propertyHolder, new PropertyPreloadedData( @@ -605,7 +602,7 @@ private Component createMapper( null, context, inheritanceStates, - isIdClass + true ); persistentClass.setIdentifierMapper( mapper ); @@ -824,18 +821,14 @@ private void handleInheritance( InheritanceState inheritanceState, PersistentClass superEntity, PropertyHolder propertyHolder) { - final boolean isJoinedSubclass; switch ( inheritanceState.getType() ) { case JOINED: joinedInheritance( inheritanceState, superEntity, propertyHolder ); - isJoinedSubclass = inheritanceState.hasParents(); break; case SINGLE_TABLE: singleTableInheritance( inheritanceState, propertyHolder ); - isJoinedSubclass = false; break; case TABLE_PER_CLASS: - isJoinedSubclass = false; break; default: throw new AssertionFailure( "Unrecognized InheritanceType" ); @@ -843,19 +836,24 @@ private void handleInheritance( bindDiscriminatorValue(); - if ( !isJoinedSubclass ) { + if ( !isJoinedSubclass( inheritanceState ) ) { checkNoJoinColumns( annotatedClass ); checkNoOnDelete( annotatedClass ); } } + private static boolean isJoinedSubclass(InheritanceState inheritanceState) { + return inheritanceState.getType() == InheritanceType.JOINED + && inheritanceState.hasParents(); + } + private void singleTableInheritance(InheritanceState inheritanceState, PropertyHolder holder) { final var discriminatorColumn = processSingleTableDiscriminatorProperties( inheritanceState ); // todo : sucks that this is separate from RootClass distinction if ( !inheritanceState.hasParents() ) { - final var rootClass = (RootClass) persistentClass; if ( inheritanceState.hasSiblings() || discriminatorColumn != null && !discriminatorColumn.isImplicit() ) { + final var rootClass = (RootClass) persistentClass; bindDiscriminatorColumnToRootPersistentClass( rootClass, discriminatorColumn, holder ); if ( context.getBuildingOptions().shouldImplicitlyForceDiscriminatorInSelect() ) { rootClass.setForceDiscriminator( true ); @@ -870,7 +868,7 @@ private void joinedInheritance(InheritanceState state, PersistentClass superEnti final var joinedSubclass = (JoinedSubclass) persistentClass; final var key = new DependantValue( context, joinedSubclass.getTable(), joinedSubclass.getIdentifier() ); joinedSubclass.setKey( key ); - handleForeignKeys( annotatedClass, context, key ); + handleForeignKey( annotatedClass, context, key ); final var onDelete = annotatedClass.getAnnotationUsage( OnDelete.class, modelsContext() ); key.setOnDeleteAction( onDelete == null ? null : onDelete.action() ); //we are never in a second pass at that stage, so queue it @@ -880,14 +878,15 @@ private void joinedInheritance(InheritanceState state, PersistentClass superEnti } final var discriminatorColumn = processJoinedDiscriminatorProperties( state ); - if ( !state.hasParents() ) { // todo : sucks that this is separate from RootClass distinction - final var rootClass = (RootClass) persistentClass; + // todo : sucks that this is separate from RootClass distinction + if ( !state.hasParents() ) { // the class we're processing is the root of the hierarchy, so // let's see if we had a discriminator column (it's perfectly // valid for joined inheritance to not have a discriminator) if ( discriminatorColumn != null ) { // we do have a discriminator column if ( state.hasSiblings() || !discriminatorColumn.isImplicit() ) { + final var rootClass = (RootClass) persistentClass; bindDiscriminatorColumnToRootPersistentClass( rootClass, discriminatorColumn, holder ); if ( context.getBuildingOptions().shouldImplicitlyForceDiscriminatorInSelect() ) { rootClass.setForceDiscriminator( true ); @@ -912,39 +911,22 @@ private void checkNoOnDelete(ClassDetails annotatedClass) { } } - private void handleForeignKeys(ClassDetails clazzToProcess, MetadataBuildingContext context, DependantValue key) { + private static void handleForeignKey(ClassDetails clazzToProcess, MetadataBuildingContext context, DependantValue key) { + final var foreignKey = clazzToProcess.getDirectAnnotationUsage( ForeignKey.class ); + handleForeignKeyConstraint( key, foreignKey, nestedForeignKey( clazzToProcess ), context ); + } + + private static ForeignKey nestedForeignKey(ClassDetails clazzToProcess) { final var pkJoinColumn = clazzToProcess.getDirectAnnotationUsage( PrimaryKeyJoinColumn.class ); final var pkJoinColumns = clazzToProcess.getDirectAnnotationUsage( PrimaryKeyJoinColumns.class ); - final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault(); - if ( pkJoinColumn != null && noConstraint( pkJoinColumn.foreignKey(), noConstraintByDefault ) - || pkJoinColumns != null && noConstraint( pkJoinColumns.foreignKey(), noConstraintByDefault ) ) { - key.disableForeignKey(); + if ( pkJoinColumn != null ) { + return pkJoinColumn.foreignKey(); + } + else if ( pkJoinColumns != null ) { + return pkJoinColumns.foreignKey(); } else { - final var foreignKey = clazzToProcess.getDirectAnnotationUsage( ForeignKey.class ); - if ( noConstraint( foreignKey, noConstraintByDefault ) ) { - key.disableForeignKey(); - } - else if ( foreignKey != null ) { - key.setForeignKeyName( nullIfEmpty( foreignKey.name() ) ); - key.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) ); - key.setForeignKeyOptions( foreignKey.options() ); - } - else if ( noConstraintByDefault ) { - key.disableForeignKey(); - } - else if ( pkJoinColumns != null ) { - final var nestedFk = pkJoinColumns.foreignKey(); - key.setForeignKeyName( nullIfEmpty( nestedFk.name() ) ); - key.setForeignKeyDefinition( nullIfEmpty( nestedFk.foreignKeyDefinition() ) ); - key.setForeignKeyOptions( nestedFk.options() ); - } - else if ( pkJoinColumn != null ) { - final var nestedFk = pkJoinColumn.foreignKey(); - key.setForeignKeyName( nullIfEmpty( nestedFk.name() ) ); - key.setForeignKeyDefinition( nullIfEmpty( nestedFk.foreignKeyDefinition() ) ); - key.setForeignKeyOptions( nestedFk.options() ); - } + return null; } } 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 ae411f2f5005..ebd759d4c3bc 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 @@ -48,7 +48,7 @@ import static org.hibernate.boot.model.internal.BinderHelper.getFetchMode; import static org.hibernate.boot.model.internal.BinderHelper.getPath; import static org.hibernate.boot.model.internal.BinderHelper.isDefault; -import static org.hibernate.boot.model.internal.BinderHelper.noConstraint; +import static org.hibernate.boot.model.internal.BinderHelper.handleForeignKeyConstraint; import static org.hibernate.internal.CoreLogging.messageLogger; import static org.hibernate.internal.util.StringHelper.isBlank; import static org.hibernate.internal.util.StringHelper.nullIfEmpty; @@ -580,46 +580,29 @@ private static boolean isMappedToPrimaryKey( public static void bindForeignKeyNameAndDefinition( SimpleValue value, - MemberDetails property, + MemberDetails memberDetails, ForeignKey foreignKey, MetadataBuildingContext context) { - if ( property.hasDirectAnnotationUsage( NotFound.class ) ) { + if ( memberDetails.hasDirectAnnotationUsage( NotFound.class ) ) { // supersedes all others value.disableForeignKey(); } else { - final var joinColumn = property.getDirectAnnotationUsage( JoinColumn.class ); - final var joinColumns = property.getDirectAnnotationUsage( JoinColumns.class ); - final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault(); - if ( joinColumn != null && noConstraint( joinColumn.foreignKey(), noConstraintByDefault ) - || joinColumns != null && noConstraint( joinColumns.foreignKey(), noConstraintByDefault ) ) { - value.disableForeignKey(); - } - else { - if ( noConstraint( foreignKey, noConstraintByDefault ) ) { - value.disableForeignKey(); - } - else if ( foreignKey != null ) { - value.setForeignKeyName( nullIfEmpty( foreignKey.name() ) ); - value.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) ); - value.setForeignKeyOptions( foreignKey.options() ); - } - else if ( noConstraintByDefault ) { - value.disableForeignKey(); - } - else if ( joinColumns != null ) { - final var joinColumnsForeignKey = joinColumns.foreignKey(); - value.setForeignKeyName( nullIfEmpty( joinColumnsForeignKey.name() ) ); - value.setForeignKeyDefinition( nullIfEmpty( joinColumnsForeignKey.foreignKeyDefinition() ) ); - value.setForeignKeyOptions( joinColumnsForeignKey.options() ); - } - else if ( joinColumn != null ) { - final var joinColumnForeignKey = joinColumn.foreignKey(); - value.setForeignKeyName( nullIfEmpty( joinColumnForeignKey.name() ) ); - value.setForeignKeyDefinition( nullIfEmpty( joinColumnForeignKey.foreignKeyDefinition() ) ); - value.setForeignKeyOptions( joinColumnForeignKey.options() ); - } - } + handleForeignKeyConstraint( value, foreignKey, nestedForeignKey( memberDetails ), context ); + } + } + + private static ForeignKey nestedForeignKey(MemberDetails memberDetails) { + final var joinColumn = memberDetails.getDirectAnnotationUsage( JoinColumn.class ); + final var joinColumns = memberDetails.getDirectAnnotationUsage( JoinColumns.class ); + if ( joinColumn != null ) { + return joinColumn.foreignKey(); + } + else if ( joinColumns != null ) { + return joinColumns.foreignKey(); + } + else { + return null; } } From 73cafffe54e05b5dd3eeb4863308a5c48b21b2c2 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 25 Aug 2025 19:44:29 +1000 Subject: [PATCH 2/2] minor cleanups to EmbeddableBinder --- .../boot/model/internal/EmbeddableBinder.java | 219 +++++++++--------- 1 file changed, 107 insertions(+), 112 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java index ed990864526f..29dabfb04fd0 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EmbeddableBinder.java @@ -216,22 +216,22 @@ private static Component bindOverriddenEmbeddable( ); } - static boolean isEmbedded(MemberDetails property, ClassDetails returnedClass) { - return property.hasDirectAnnotationUsage( Embedded.class ) - || property.hasDirectAnnotationUsage( EmbeddedId.class ) + static boolean isEmbedded(MemberDetails memberDetails, ClassDetails returnedClass) { + return memberDetails.hasDirectAnnotationUsage( Embedded.class ) + || memberDetails.hasDirectAnnotationUsage( EmbeddedId.class ) || returnedClass.hasDirectAnnotationUsage( Embeddable.class ) - && !property.hasDirectAnnotationUsage( Convert.class ); + && !memberDetails.hasDirectAnnotationUsage( Convert.class ); } - static boolean isEmbedded(MemberDetails property, TypeDetails returnedClass) { - if ( property.hasDirectAnnotationUsage( Embedded.class ) - || property.hasDirectAnnotationUsage( EmbeddedId.class ) ) { + static boolean isEmbedded(MemberDetails memberDetails, TypeDetails returnedClass) { + if ( memberDetails.hasDirectAnnotationUsage( Embedded.class ) + || memberDetails.hasDirectAnnotationUsage( EmbeddedId.class ) ) { return true; } else { final var returnClassDetails = returnedClass.determineRawClass(); return returnClassDetails.hasDirectAnnotationUsage( Embeddable.class ) - && !property.hasDirectAnnotationUsage( Convert.class ); + && !memberDetails.hasDirectAnnotationUsage( Convert.class ); } } @@ -243,11 +243,11 @@ private static Component bindOverriddenEmbeddable( boolean isComponentEmbedded, boolean isId, // is an identifier Map inheritanceStatePerClass, - String referencedEntityName, // is a component which is overridden by a @MapsId + String referencedEntityName, // is a embeddable which is overridden by a @MapsId String propertyName, Class customInstantiatorImpl, AnnotatedJoinColumns annotatedJoinColumns) { - final Component component = createEmbeddable( + final var embeddable = createEmbeddable( propertyHolder, inferredData, isComponentEmbedded, @@ -257,7 +257,7 @@ private static Component bindOverriddenEmbeddable( ); context.getMetadataCollector() .addSecondPass( new CopyIdentifierComponentSecondPass( - component, + embeddable, referencedEntityName, propertyName, annotatedJoinColumns, @@ -265,11 +265,11 @@ private static Component bindOverriddenEmbeddable( ) ); if ( isId ) { - component.setKey( true ); - checkEmbeddedId( inferredData, propertyHolder, referencedEntityName, component ); + embeddable.setKey( true ); + checkEmbeddedId( inferredData, propertyHolder, referencedEntityName, embeddable ); } - callTypeBinders( component, context, inferredData.getPropertyType() ); - return component; + callTypeBinders( embeddable, context, inferredData.getPropertyType() ); + return embeddable; } static Component bindEmbeddable( @@ -285,7 +285,7 @@ static Component bindEmbeddable( Class customInstantiatorImpl, Class> compositeUserTypeClass, AnnotatedColumns annotatedColumns) { - final Component component = fillEmbeddable( + final var embeddable = fillEmbeddable( propertyHolder, inferredData, propertyAccessor, @@ -301,14 +301,14 @@ static Component bindEmbeddable( inheritanceStatePerClass ); if ( isId ) { - component.setKey( true ); - checkEmbeddedId( inferredData, propertyHolder, null, component ); + embeddable.setKey( true ); + checkEmbeddedId( inferredData, propertyHolder, null, embeddable ); } - callTypeBinders( component, context, inferredData.getPropertyType() ); - return component; + callTypeBinders( embeddable, context, inferredData.getPropertyType() ); + return embeddable; } - private static void callTypeBinders(Component component, MetadataBuildingContext context, TypeDetails annotatedClass ) { + private static void callTypeBinders(Component embeddable, MetadataBuildingContext context, TypeDetails annotatedClass ) { final var metaAnnotatedAnnotations = annotatedClass.determineRawClass() .getMetaAnnotated( TypeBinderType.class, @@ -319,7 +319,7 @@ private static void callTypeBinders(Component component, MetadataBuildingContext //noinspection rawtypes final TypeBinder binder = binderType.binder().getDeclaredConstructor().newInstance(); //noinspection unchecked - binder.bind( metaAnnotated, context, component ); + binder.bind( metaAnnotated, context, embeddable ); } catch (Exception e) { throw new AnnotationException( @@ -336,11 +336,11 @@ private static PropertyBinder createEmbeddedProperty( boolean isComponentEmbedded, boolean isId, Map inheritanceStatePerClass, - Component component) { + Component embeddable) { final var binder = new PropertyBinder(); binder.setDeclaringClass( inferredData.getDeclaringClass() ); binder.setName( inferredData.getPropertyName() ); - binder.setValue( component ); + binder.setValue( embeddable ); binder.setMemberDetails( inferredData.getAttributeMember() ); binder.setAccessType( inferredData.getDefaultAccess() ); binder.setEmbedded( isComponentEmbedded ); @@ -357,18 +357,18 @@ private static void checkEmbeddedId( PropertyData inferredData, PropertyHolder propertyHolder, String referencedEntityName, - Component component) { + Component embeddable) { if ( propertyHolder.getPersistentClass().getIdentifier() != null ) { throw new AnnotationException( - "Embeddable class '" + component.getComponentClassName() + "Embeddable class '" + embeddable.getComponentClassName() + "' may not have a property annotated '@Id' since it is used by '" + getPath(propertyHolder, inferredData) + "' as an '@EmbeddedId'" ); } - if ( referencedEntityName == null && component.getPropertySpan() == 0 ) { + if ( referencedEntityName == null && embeddable.getPropertySpan() == 0 ) { throw new AnnotationException( - "Embeddable class '" + component.getComponentClassName() + "Embeddable class '" + embeddable.getComponentClassName() + "' may not be used as an '@EmbeddedId' by '" + getPath(propertyHolder, inferredData) + "' because it has no properties" @@ -430,7 +430,7 @@ static Component fillEmbeddable( // inSecondPass can only be used to apply right away the second pass of a composite-element // Because it's a value type, there is no bidirectional association, hence second pass // ordering does not matter - final Component component = createEmbeddable( + final var embeddable = createEmbeddable( propertyHolder, inferredData, isComponentEmbedded, @@ -443,8 +443,8 @@ static Component fillEmbeddable( if ( LOG.isTraceEnabled() ) { LOG.trace( "Binding embeddable with path: " + subpath ); } - final PropertyHolder subholder = buildPropertyHolder( - component, + final var subholder = buildPropertyHolder( + embeddable, subpath, inferredData, propertyHolder, @@ -452,8 +452,8 @@ static Component fillEmbeddable( inheritanceStatePerClass ); - // propertyHolder here is the owner of the component property. - // Tell it we are about to start the component... + // propertyHolder here is the owner of the embeddable property. + // Tell it we are about to start the embeddable... propertyHolder.startingProperty( inferredData.getAttributeMember() ); final CompositeUserType compositeUserType; @@ -464,11 +464,11 @@ static Component fillEmbeddable( } else { compositeUserType = compositeUserType( compositeUserTypeClass, context ); - component.setTypeName( compositeUserTypeClass.getName() ); + embeddable.setTypeName( compositeUserTypeClass.getName() ); returnedClassOrElement = context.getBootstrapContext().getModelsContext().getClassDetailsRegistry().resolveClassDetails( compositeUserType.embeddable().getName() ); } AggregateComponentBinder.processAggregate( - component, + embeddable, propertyHolder, inferredData, returnedClassOrElement, @@ -476,12 +476,12 @@ static Component fillEmbeddable( context ); - final InheritanceState inheritanceState = inheritanceStatePerClass.get( returnedClassOrElement ); + final var inheritanceState = inheritanceStatePerClass.get( returnedClassOrElement ); if ( inheritanceState != null ) { - inheritanceState.postProcess( component ); + inheritanceState.postProcess( embeddable ); // Main entry point for binding embeddable inheritance bindDiscriminator( - component, + embeddable, returnedClassOrElement, propertyHolder, subholder, @@ -493,8 +493,9 @@ static Component fillEmbeddable( final var annotatedTypeDetails = inferredData.getPropertyType(); - final Map subclassToSuperclass = component.isPolymorphic() ? new HashMap<>() : null; - final List classElements = collectClassElements( + final Map subclassToSuperclass = + embeddable.isPolymorphic() ? new HashMap<>() : null; + final var classElements = collectClassElements( propertyAccessor, context, returnedClassOrElement, @@ -503,9 +504,9 @@ static Component fillEmbeddable( subclassToSuperclass ); - if ( component.isPolymorphic() ) { + if ( embeddable.isPolymorphic() ) { validateInheritanceIsSupported( subholder, compositeUserType ); - final var discriminatorType = (BasicType) component.getDiscriminator().getType(); + final var discriminatorType = (BasicType) embeddable.getDiscriminator().getType(); // Discriminator values are used to construct the embeddable domain // type hierarchy so order of processing is important final Map discriminatorValues = new LinkedHashMap<>(); @@ -519,11 +520,11 @@ static Component fillEmbeddable( discriminatorValues, subclassToSuperclass ); - component.setDiscriminatorValues( discriminatorValues ); - component.setSubclassToSuperclass( subclassToSuperclass ); + embeddable.setDiscriminatorValues( discriminatorValues ); + embeddable.setSubclassToSuperclass( subclassToSuperclass ); } - final List baseClassElements = + final var baseClassElements = collectBaseClassElements( baseInferredData, propertyAccessor, context, entityAtStake ); if ( baseClassElements != null //useful to avoid breaking pre JPA 2 mappings @@ -548,7 +549,7 @@ static Component fillEmbeddable( final var memberDetails = propertyAnnotatedElement.getAttributeMember(); if ( isIdClass || subholder.isOrWithinEmbeddedId() ) { - final var property = findProperty( component, memberDetails.getName() ); + final var property = findProperty( embeddable, memberDetails.getName() ); if ( property != null ) { // Identifier properties are always simple values final var value = (SimpleValue) property.getValue(); @@ -569,14 +570,14 @@ else if ( memberDetails.hasDirectAnnotationUsage( GeneratedValue.class ) ) { } if ( compositeUserType != null ) { - processCompositeUserType( component, compositeUserType ); + processCompositeUserType( embeddable, compositeUserType ); } - return component; + return embeddable; } - private static Property findProperty(Component component, String name) { - for ( var property : component.getProperties() ) { + private static Property findProperty(Component embeddable, String name) { + for ( var property : embeddable.getProperties() ) { if ( property.getName().equals( name ) ) { return property; } @@ -597,39 +598,37 @@ private static CompositeUserType compositeUserType( } private static void bindDiscriminator( - Component component, + Component embeddable, ClassDetails componentClass, PropertyHolder parentHolder, PropertyHolder holder, PropertyData propertyData, InheritanceState inheritanceState, MetadataBuildingContext context) { - if ( inheritanceState == null ) { - return; - } - final var discriminatorColumn = processEmbeddableDiscriminatorProperties( - componentClass, - propertyData, - parentHolder, - holder, - inheritanceState, - context - ); - if ( discriminatorColumn != null ) { - bindDiscriminatorColumnToComponent( component, discriminatorColumn, holder, context ); + if ( inheritanceState != null ) { + final var discriminatorColumn = processEmbeddableDiscriminatorProperties( + componentClass, + propertyData, + parentHolder, + holder, + inheritanceState, + context + ); + if ( discriminatorColumn != null ) { + bindDiscriminatorColumnToComponent( embeddable, discriminatorColumn, holder, context ); + } } } private static AnnotatedDiscriminatorColumn processEmbeddableDiscriminatorProperties( - ClassDetails annotatedClass, + ClassDetails classDetails, PropertyData propertyData, PropertyHolder parentHolder, PropertyHolder holder, InheritanceState inheritanceState, MetadataBuildingContext context) { - final var discriminatorColumn = annotatedClass.getDirectAnnotationUsage( DiscriminatorColumn.class ); - final var discriminatorFormula = - getOverridableAnnotation( annotatedClass, DiscriminatorFormula.class, context ); + final var discriminatorColumn = classDetails.getDirectAnnotationUsage( DiscriminatorColumn.class ); + final var discriminatorFormula = getOverridableAnnotation( classDetails, DiscriminatorFormula.class, context ); if ( !inheritanceState.hasParents() ) { if ( inheritanceState.hasSiblings() ) { final String path = qualify( holder.getPath(), EntityDiscriminatorMapping.DISCRIMINATOR_ROLE_NAME ); @@ -657,13 +656,13 @@ private static AnnotatedDiscriminatorColumn processEmbeddableDiscriminatorProper if ( discriminatorColumn != null ) { throw new AnnotationException( String.format( "Embeddable class '%s' is annotated '@DiscriminatorColumn' but it is not the root of the inheritance hierarchy", - annotatedClass.getName() + classDetails.getName() ) ); } if ( discriminatorFormula != null ) { throw new AnnotationException( String.format( "Embeddable class '%s' is annotated '@DiscriminatorFormula' but it is not the root of the inheritance hierarchy", - annotatedClass.getName() + classDetails.getName() ) ); } } @@ -671,18 +670,18 @@ private static AnnotatedDiscriminatorColumn processEmbeddableDiscriminatorProper } private static void bindDiscriminatorColumnToComponent( - Component component, + Component embeddable, AnnotatedDiscriminatorColumn discriminatorColumn, PropertyHolder holder, MetadataBuildingContext context) { - assert component.getDiscriminator() == null; + assert embeddable.getDiscriminator() == null; final var columns = new AnnotatedColumns(); columns.setPropertyHolder( holder ); columns.setBuildingContext( context ); discriminatorColumn.setParent( columns ); - final BasicValue discriminatorColumnBinding = new BasicValue( context, component.getTable() ); - discriminatorColumnBinding.setAggregateColumn( component.getAggregateColumn() ); - component.setDiscriminator( discriminatorColumnBinding ); + final var discriminatorColumnBinding = new BasicValue( context, embeddable.getTable() ); + discriminatorColumnBinding.setAggregateColumn( embeddable.getAggregateColumn() ); + embeddable.setDiscriminator( discriminatorColumnBinding ); discriminatorColumn.linkWithValue( discriminatorColumnBinding ); discriminatorColumnBinding.setTypeName( discriminatorColumn.getDiscriminatorTypeName() ); } @@ -726,11 +725,7 @@ private static List collectClassElements( ClassDetails superClass; while ( isValidSuperclass( superClass = subclass.getSuperClass(), isIdClass ) ) { //FIXME: proper support of type variables incl var resolved at upper levels - final PropertyContainer superContainer = new PropertyContainer( - superClass, - annotatedClass, - propertyAccessor - ); + final var superContainer = new PropertyContainer( superClass, annotatedClass, propertyAccessor ); addElementsOfClass( classElements, superContainer, context, 0 ); if ( subclassToSuperclass != null ) { subclassToSuperclass.put( subclass.getName(), superClass.getName() ); @@ -845,16 +840,16 @@ private static List collectBaseClassElements( } } - private static void processCompositeUserType(Component component, CompositeUserType compositeUserType) { - component.sortProperties(); - final List sortedPropertyNames = new ArrayList<>( component.getPropertySpan() ); - final List sortedPropertyTypes = new ArrayList<>( component.getPropertySpan() ); + private static void processCompositeUserType(Component embeddable, CompositeUserType compositeUserType) { + embeddable.sortProperties(); + final List sortedPropertyNames = new ArrayList<>( embeddable.getPropertySpan() ); + final List sortedPropertyTypes = new ArrayList<>( embeddable.getPropertySpan() ); final var strategy = new PropertyAccessStrategyCompositeUserTypeImpl( compositeUserType, sortedPropertyNames, sortedPropertyTypes ); - for ( var property : component.getProperties() ) { + for ( var property : embeddable.getProperties() ) { sortedPropertyNames.add( property.getName() ); sortedPropertyTypes.add( PropertyAccessStrategyGetterImpl.INSTANCE.buildPropertyAccess( @@ -962,32 +957,32 @@ static Component createEmbeddable( boolean isIdentifierMapper, Class customInstantiatorImpl, MetadataBuildingContext context) { - final var component = new Component( context, propertyHolder.getPersistentClass() ); - component.setEmbedded( isComponentEmbedded ); + final var embeddable = new Component( context, propertyHolder.getPersistentClass() ); + embeddable.setEmbedded( isComponentEmbedded ); //yuk - component.setTable( propertyHolder.getTable() ); + embeddable.setTable( propertyHolder.getTable() ); if ( isIdentifierMapper || isComponentEmbedded && inferredData.getPropertyName() == null ) { - component.setComponentClassName( component.getOwner().getClassName() ); + embeddable.setComponentClassName( embeddable.getOwner().getClassName() ); } else { - component.setComponentClassName( inferredData.getClassOrElementType().getName() ); + embeddable.setComponentClassName( inferredData.getClassOrElementType().getName() ); } - component.setCustomInstantiator( customInstantiatorImpl ); + embeddable.setCustomInstantiator( customInstantiatorImpl ); final var constructor = resolveInstantiator( inferredData.getClassOrElementType() ); if ( constructor != null ) { - component.setInstantiator( constructor, constructor.getAnnotation( Instantiator.class ).value() ); + embeddable.setInstantiator( constructor, constructor.getAnnotation( Instantiator.class ).value() ); } if ( propertyHolder.isComponent() ) { final var componentPropertyHolder = (ComponentPropertyHolder) propertyHolder; - component.setParentAggregateColumn( componentPropertyHolder.getAggregateColumn() ); + embeddable.setParentAggregateColumn( componentPropertyHolder.getAggregateColumn() ); } - applyColumnNamingPattern( component, inferredData ); - return component; + applyColumnNamingPattern( embeddable, inferredData ); + return embeddable; } - private static void applyColumnNamingPattern(Component component, PropertyData inferredData) { - final Class componentClass = component.getComponentClass(); + private static void applyColumnNamingPattern(Component embeddable, PropertyData inferredData) { + final var componentClass = embeddable.getComponentClass(); if ( componentClass == null || Map.class.equals( componentClass ) ) { // dynamic models return; @@ -1021,7 +1016,7 @@ private static void applyColumnNamingPattern(Component component, PropertyData i ) ); } - component.setColumnNamingPattern( columnNamingPattern ); + embeddable.setColumnNamingPattern( columnNamingPattern ); } private static Constructor resolveInstantiator(TypeDetails embeddableClass) { @@ -1077,16 +1072,16 @@ public static Class determineCustomInstantiato private static class CopyIdentifierComponentSecondPass implements FkSecondPass { private final String referencedEntityName; private final String propertyName; - private final Component component; + private final Component embeddable; private final MetadataBuildingContext buildingContext; private final AnnotatedJoinColumns joinColumns; private CopyIdentifierComponentSecondPass( - Component component, + Component embeddable, String referencedEntityName, String propertyName, AnnotatedJoinColumns joinColumns, MetadataBuildingContext buildingContext) { - this.component = component; + this.embeddable = embeddable; this.referencedEntityName = referencedEntityName; this.propertyName = propertyName; this.buildingContext = buildingContext; @@ -1095,7 +1090,7 @@ private CopyIdentifierComponentSecondPass( @Override public Value getValue() { - return component; + return embeddable; } @Override @@ -1152,7 +1147,7 @@ public void doSecondPass(Map persistentClasses) throws referencedProperty ); } - component.addProperty( property ); + embeddable.addProperty( property ); } } @@ -1171,10 +1166,10 @@ private Component getReferencedComponent(PersistentClass referencedPersistentCla // a property of the composite id that has the foreign key throw new AnnotationException( "Missing 'value' in '@MapsId' annotation of association '" + propertyName - + "' of entity '" + component.getOwner().getEntityName() - + "' with composite identifier type" - + " ('@MapsId' must specify a property of the '@EmbeddedId' class which has the foreign key of '" - + referencedEntityName + "')" + + "' of entity '" + embeddable.getOwner().getEntityName() + + "' with composite identifier type" + + " ('@MapsId' must specify a property of the '@EmbeddedId' class which has the foreign key of '" + + referencedEntityName + "')" ); } } @@ -1188,9 +1183,9 @@ private Property createComponentProperty( property.setName( referencedProperty.getName() ); //FIXME set optional? //property.setOptional( property.isOptional() ); - property.setPersistentClass( component.getOwner() ); + property.setPersistentClass( embeddable.getOwner() ); property.setPropertyAccessorName( referencedProperty.getPropertyAccessorName() ); - var value = new Component( buildingContext, component.getOwner() ); + var value = new Component( buildingContext, embeddable.getOwner() ); property.setValue( value ); final var referencedValue = (Component) referencedProperty.getValue(); @@ -1233,9 +1228,9 @@ private Property createSimpleProperty( property.setName( referencedProperty.getName() ); //FIXME set optional? //property.setOptional( property.isOptional() ); - property.setPersistentClass( component.getOwner() ); + property.setPersistentClass( embeddable.getOwner() ); property.setPropertyAccessorName( referencedProperty.getPropertyAccessorName() ); - final var value = new BasicValue( buildingContext, component.getTable() ); + final var value = new BasicValue( buildingContext, embeddable.getTable() ); property.setValue( value ); final var referencedValue = (SimpleValue) referencedProperty.getValue(); value.copyTypeFrom( referencedValue ); @@ -1267,7 +1262,7 @@ private Property createSimpleProperty( if ( joinColumn == null && !firstColumn.isNameDeferred() ) { throw new AnnotationException( "Property '" + propertyName - + "' of entity '" + component.getOwner().getEntityName() + + "' of entity '" + embeddable.getOwner().getEntityName() + "' must have a '@JoinColumn' which references the foreign key column '" + logicalColumnName + "'" );