diff --git a/hibernate-core/src/main/java/org/hibernate/boot/beanvalidation/TypeSafeActivator.java b/hibernate-core/src/main/java/org/hibernate/boot/beanvalidation/TypeSafeActivator.java index 13f02edb6182..602c66cfea76 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/beanvalidation/TypeSafeActivator.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/beanvalidation/TypeSafeActivator.java @@ -14,7 +14,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; -import java.util.StringTokenizer; import jakarta.validation.NoProviderFoundException; import jakarta.validation.constraints.Digits; @@ -25,7 +24,6 @@ import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import org.hibernate.AssertionFailure; -import org.hibernate.MappingException; import org.hibernate.boot.internal.ClassLoaderAccessImpl; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; @@ -60,10 +58,10 @@ import static java.util.Collections.disjoint; import static org.hibernate.boot.beanvalidation.BeanValidationIntegrator.APPLY_CONSTRAINTS; import static org.hibernate.boot.beanvalidation.GroupsPerOperation.buildGroupsForOperation; +import static org.hibernate.boot.model.internal.BinderHelper.findPropertyByName; import static org.hibernate.cfg.ValidationSettings.CHECK_NULLABILITY; import static org.hibernate.cfg.ValidationSettings.JAKARTA_VALIDATION_FACTORY; import static org.hibernate.cfg.ValidationSettings.JPA_VALIDATION_FACTORY; -import static org.hibernate.internal.util.StringHelper.isEmpty; import static org.hibernate.internal.util.StringHelper.isNotEmpty; /** @@ -496,72 +494,6 @@ private static boolean isValidatorLengthAnnotation(ConstraintDescriptor descr .equals( descriptor.getAnnotation().annotationType().getName() ); } - /** - * Locate the property by path in a recursive way, including IdentifierProperty in the loop if propertyName is - * {@code null}. If propertyName is {@code null} or empty, the IdentifierProperty is returned - */ - private static Property findPropertyByName(PersistentClass associatedClass, String propertyName) { - Property property = null; - final Property idProperty = associatedClass.getIdentifierProperty(); - final String idName = idProperty != null ? idProperty.getName() : null; - try { - if ( isEmpty( propertyName ) || propertyName.equals( idName ) ) { - //default to id - property = idProperty; - } - else { - if ( propertyName.indexOf( idName + "." ) == 0 ) { - property = idProperty; - propertyName = propertyName.substring( idName.length() + 1 ); - } - final StringTokenizer tokens = new StringTokenizer( propertyName, ".", false ); - while ( tokens.hasMoreTokens() ) { - final String element = tokens.nextToken(); - if ( property == null ) { - property = associatedClass.getProperty( element ); - } - else { - if ( property.isComposite() ) { - property = ( (Component) property.getValue() ).getProperty( element ); - } - else { - return null; - } - } - } - } - } - catch ( MappingException e ) { - try { - //if we do not find it, try to check the identifier mapper - if ( associatedClass.getIdentifierMapper() == null ) { - return null; - } - else { - final StringTokenizer tokens = new StringTokenizer( propertyName, ".", false ); - while ( tokens.hasMoreTokens() ) { - final String element = tokens.nextToken(); - if ( property == null ) { - property = associatedClass.getIdentifierMapper().getProperty( element ); - } - else { - if ( property.isComposite() ) { - property = ( (Component) property.getValue() ).getProperty( element ); - } - else { - return null; - } - } - } - } - } - catch ( MappingException ee ) { - return null; - } - } - return property; - } - private static ValidatorFactory getValidatorFactory(ActivationContext context) { // IMPL NOTE: We can either be provided a ValidatorFactory or make one. We can be provided // a ValidatorFactory in 2 different ways. So here we "get" a ValidatorFactory diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java index a8988e5956d5..f2d0879e43ea 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java @@ -233,14 +233,9 @@ Property resolveMapsId() { final PersistentClass persistentClass = getPropertyHolder().getPersistentClass(); final KeyValue identifier = persistentClass.getIdentifier(); try { - if ( identifier instanceof Component embeddedIdType ) { - // an @EmbeddedId - return embeddedIdType.getProperty( getMapsId() ); - } - else { - // a simple id or an @IdClass - return persistentClass.getProperty( getMapsId() ); - } + return identifier instanceof Component embeddedIdType + ? embeddedIdType.getProperty( getMapsId() ) // an @EmbeddedId + : persistentClass.getProperty( getMapsId() ); // a simple id or an @IdClass } catch (MappingException me) { throw new AnnotationException( "Identifier field '" + getMapsId() @@ -255,10 +250,10 @@ public List getJoinColumns() { @Override public void addColumn(AnnotatedColumn child) { - if ( !( child instanceof AnnotatedJoinColumn ) ) { + if ( !( child instanceof AnnotatedJoinColumn joinColumn ) ) { throw new AssertionFailure( "wrong sort of column" ); } - addColumn( (AnnotatedJoinColumn) child ); + addColumn( joinColumn ); } public void addColumn(AnnotatedJoinColumn child) { 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 20b400f1cda7..d4863418986f 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 @@ -583,114 +583,84 @@ private static void matchColumnsByProperty(Property property, Map createCompositeUserType(Component component) { : FallbackBeanInstanceProducer.INSTANCE.produceBeanInstance( clazz ); } + @Override + public boolean contains(Property property) { + return properties.contains( property ); + } + @Override public CompositeType getType() throws MappingException { // Resolve the type of the value once and for all as this operation generates a proxy class @@ -546,6 +552,7 @@ public Property getProperty(int index) { return properties.get( index ); } + @Override public Property getProperty(String propertyName) throws MappingException { for ( Property prop : properties ) { if ( prop.getName().equals(propertyName) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Join.java b/hibernate-core/src/main/java/org/hibernate/mapping/Join.java index bb9796fdf2b0..39e44b6f3fa9 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Join.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Join.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.function.Supplier; +import org.hibernate.MappingException; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; import org.hibernate.jdbc.Expectation; import org.hibernate.sql.Alias; @@ -64,6 +65,11 @@ public boolean contains(Property property) { return properties.contains( property ); } + @Override + public Property getProperty(String propertyName) throws MappingException { + throw new UnsupportedOperationException(); //TODO + } + public void addMappedSuperclassProperty(Property property ) { properties.add( property ); property.setPersistentClass( persistentClass ); @@ -81,6 +87,7 @@ public boolean containsProperty(Property property) { return properties.contains( property ); } + @Override public Table getTable() { return table; } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/JoinedSubclass.java b/hibernate-core/src/main/java/org/hibernate/mapping/JoinedSubclass.java index 58a69cd0589f..a11a724b9901 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/JoinedSubclass.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/JoinedSubclass.java @@ -25,6 +25,7 @@ public JoinedSubclass(PersistentClass superclass, MetadataBuildingContext metada super( superclass, metadataBuildingContext ); } + @Override public Table getTable() { return table; } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java index acdae60bedb8..51a31fc0bfb3 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java @@ -541,7 +541,8 @@ else if ( identifierProperty == null && getIdentifierMapper() != null ) { } else { //flat recursive algorithm - property = ( (Component) property.getValue() ).getProperty( element ); + final var value = (Component) property.getValue(); + property = value.getProperty( element ); } } } @@ -564,6 +565,7 @@ private Property getProperty(String propertyName, List properties) thr throw new MappingException( "property [" + propertyName + "] not found on entity [" + getEntityName() + "]" ); } + @Override public Property getProperty(String propertyName) throws MappingException { final Property identifierProperty = getIdentifierProperty(); if ( identifierProperty != null diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/UnionSubclass.java b/hibernate-core/src/main/java/org/hibernate/mapping/UnionSubclass.java index c8c1dc222d24..c8986022eb3b 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/UnionSubclass.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/UnionSubclass.java @@ -22,6 +22,7 @@ public UnionSubclass(PersistentClass superclass, MetadataBuildingContext buildin super( superclass, buildingContext ); } + @Override public Table getTable() { return table; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/AttributeFactory.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/AttributeFactory.java index acdaa5f698ee..b02929cdc1c4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/AttributeFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/AttributeFactory.java @@ -12,7 +12,6 @@ import org.hibernate.AssertionFailure; import org.hibernate.PropertyNotFoundException; -import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.mapping.AggregateColumn; @@ -21,9 +20,7 @@ import org.hibernate.mapping.Component; import org.hibernate.mapping.List; import org.hibernate.mapping.Map; -import org.hibernate.mapping.MappedSuperclass; import org.hibernate.mapping.OneToMany; -import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Property; import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.Value; @@ -31,12 +28,9 @@ import org.hibernate.metamodel.RepresentationMode; import org.hibernate.metamodel.UnsupportedMappingException; import org.hibernate.metamodel.ValueClassification; -import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.CompositeIdentifierMapping; -import org.hibernate.metamodel.mapping.EmbeddableMappingType; import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; import org.hibernate.metamodel.mapping.EntityIdentifierMapping; -import org.hibernate.metamodel.mapping.EntityVersionMapping; import org.hibernate.metamodel.model.domain.internal.AbstractIdentifiableType; import org.hibernate.metamodel.model.domain.DomainType; import org.hibernate.metamodel.model.domain.EmbeddableDomainType; @@ -71,7 +65,6 @@ import jakarta.persistence.ManyToMany; import jakarta.persistence.OneToOne; import jakarta.persistence.metamodel.Attribute; -import jakarta.persistence.metamodel.Type; /** * A factory for building {@link Attribute} instances. Exposes 3 main services for building
    @@ -98,15 +91,14 @@ public AttributeFactory(MetadataContext context) { * @param ownerType The descriptor of the attribute owner (aka declarer). * @param property The Hibernate property descriptor for the attribute * @param The type of the owner - * @param The attribute type * * @return The built attribute descriptor or null if the attribute is not part of the JPA 2 model (eg backrefs) */ - public PersistentAttribute buildAttribute(ManagedDomainType ownerType, Property property) { + public PersistentAttribute buildAttribute(ManagedDomainType ownerType, Property property) { return buildAttribute( ownerType, property, context ); } - public static PersistentAttribute buildAttribute( + public static PersistentAttribute buildAttribute( ManagedDomainType ownerType, Property property, MetadataContext metadataContext) { @@ -115,30 +107,28 @@ public static PersistentAttribute buildAttribute( log.tracef( "Skipping synthetic property %s(%s)", ownerType.getTypeName(), property.getName() ); return null; } + log.tracef( "Building attribute [%s.%s]", ownerType.getTypeName(), property.getName() ); - final AttributeContext attributeContext = wrap( ownerType, property ); - final AttributeMetadata attributeMetadata = determineAttributeMetadata( - attributeContext, - normalMemberResolver, - metadataContext - ); + final var attributeMetadata = + determineAttributeMetadata( wrap( ownerType, property ), normalMemberResolver, metadataContext ); if ( attributeMetadata instanceof PluralAttributeMetadata ) { return PluralAttributeBuilder.build( - (PluralAttributeMetadata) attributeMetadata, + (PluralAttributeMetadata) attributeMetadata, property.isGeneric(), metadataContext ); } - final ValueContext valueContext = ( (SingularAttributeMetadata) attributeMetadata ).getValueContext(); - final DomainType domainType = determineSimpleType( valueContext, metadataContext ); - final JavaType relationalJavaType = determineRelationalJavaType( valueContext, domainType, metadataContext ); + final var singularAttributeMetadata = (SingularAttributeMetadata) attributeMetadata; + final var valueContext = singularAttributeMetadata.getValueContext(); + final var domainType = (SqmDomainType) determineSimpleType( valueContext, metadataContext ); + final var relationalJavaType = determineRelationalJavaType( valueContext, domainType, metadataContext ); return new SingularAttributeImpl<>( ownerType, attributeMetadata.getName(), attributeMetadata.getAttributeClassification(), - (SqmDomainType) domainType, + domainType, relationalJavaType, attributeMetadata.getMember(), false, @@ -166,24 +156,22 @@ public Property getPropertyMapping() { * @param ownerType The descriptor of the attribute owner (aka declarer). * @param property The Hibernate property descriptor for the identifier attribute * @param The type of the owner - * @param The attribute type * * @return The built attribute descriptor */ - public SingularPersistentAttribute buildIdAttribute( + public SingularPersistentAttribute buildIdAttribute( IdentifiableDomainType ownerType, Property property) { log.tracef( "Building identifier attribute [%s.%s]", ownerType.getTypeName(), property.getName() ); - final AttributeMetadata attributeMetadata = + final var attributeMetadata = determineAttributeMetadata( wrap( ownerType, property ), identifierMemberResolver ); - final SingularAttributeMetadata singularAttributeMetadata = - (SingularAttributeMetadata) attributeMetadata; - final DomainType domainType = determineSimpleType( singularAttributeMetadata.getValueContext() ); + final var singularAttributeMetadata = (SingularAttributeMetadata) attributeMetadata; + final var domainType = (SqmDomainType) determineSimpleType( singularAttributeMetadata.getValueContext() ); return new SingularAttributeImpl.Identifier<>( ownerType, property.getName(), - (SqmDomainType) domainType, + domainType, attributeMetadata.getMember(), attributeMetadata.getAttributeClassification(), property.isGeneric() @@ -196,34 +184,32 @@ public SingularPersistentAttribute buildIdAttribute( * @param ownerType The descriptor of the attribute owner (aka declarer). * @param property The Hibernate property descriptor for the version attribute * @param The type of the owner - * @param The attribute type * * @return The built attribute descriptor */ - public SingularAttributeImpl buildVersionAttribute( + public SingularAttributeImpl buildVersionAttribute( IdentifiableDomainType ownerType, Property property) { log.tracef( "Building version attribute [%s.%s]", ownerType.getTypeName(), property.getName() ); - final AttributeMetadata attributeMetadata = + final var attributeMetadata = determineAttributeMetadata( wrap( ownerType, property ), versionMemberResolver ); - final SingularAttributeMetadata singularAttributeMetadata = - (SingularAttributeMetadata) attributeMetadata; - final DomainType domainType = determineSimpleType( singularAttributeMetadata.getValueContext() ); + final var singularAttributeMetadata = (SingularAttributeMetadata) attributeMetadata; + final var domainType = (SqmDomainType) determineSimpleType( singularAttributeMetadata.getValueContext() ); return new SingularAttributeImpl.Version<>( ownerType, property.getName(), attributeMetadata.getAttributeClassification(), - (SqmDomainType) domainType, + domainType, attributeMetadata.getMember() ); } - private DomainType determineSimpleType(ValueContext typeContext) { + private DomainType determineSimpleType(ValueContext typeContext) { return determineSimpleType( typeContext, context ); } - public static DomainType determineSimpleType(ValueContext typeContext, MetadataContext context) { + public static DomainType determineSimpleType(ValueContext typeContext, MetadataContext context) { return switch ( typeContext.getValueClassification() ) { case BASIC -> basicDomainType( typeContext, context ); case ENTITY -> entityDomainType (typeContext, context ); @@ -232,37 +218,30 @@ public static DomainType determineSimpleType(ValueContext typeContext, Me }; } - private static EmbeddableDomainType embeddableDomainType(ValueContext typeContext, MetadataContext context) { - final Component component = (Component) typeContext.getHibernateValue(); + private static EmbeddableDomainType embeddableDomainType(ValueContext typeContext, MetadataContext context) { + final var component = (Component) typeContext.getHibernateValue(); return component.isDynamic() ? dynamicEmbeddableType( context, component ) : classEmbeddableType( context, component ); // we should have a non-dynamic embeddable } - private static EmbeddableDomainType classEmbeddableType(MetadataContext context, Component component) { + private static EmbeddableDomainType classEmbeddableType(MetadataContext context, Component component) { assert component.getComponentClassName() != null; - @SuppressWarnings("unchecked") - final Class embeddableClass = (Class) component.getComponentClass(); + final var embeddableClass = component.getComponentClass(); if ( !component.isGeneric() ) { - final EmbeddableDomainType cached = context.locateEmbeddable( embeddableClass, component ); + final var cached = context.locateEmbeddable( embeddableClass, component ); if ( cached != null ) { return cached; } } - final MappedSuperclass mappedSuperclass = component.getMappedSuperclass(); - final MappedSuperclassDomainType superType; - if ( mappedSuperclass != null ) { - //noinspection unchecked - superType = (MappedSuperclassDomainType) context.locateMappedSuperclassType( mappedSuperclass ); - } - else { - superType = null; - } + final var mappedSuperclass = component.getMappedSuperclass(); + final var superType = mappedSuperclass == null ? null : context.locateMappedSuperclassType( mappedSuperclass ); + + final var discriminatorType = component.isPolymorphic() ? component.getDiscriminatorType() : null; - final DomainType discriminatorType = component.isPolymorphic() ? component.getDiscriminatorType() : null; - final EmbeddableTypeImpl embeddableType = new EmbeddableTypeImpl<>( + final var embeddableType = new EmbeddableTypeImpl<>( context.getJavaTypeRegistry().resolveManagedTypeDescriptor( embeddableClass ), superType, discriminatorType, @@ -272,10 +251,10 @@ private static EmbeddableDomainType classEmbeddableType(MetadataContext c context.registerEmbeddableType( embeddableType, component ); if ( component.isPolymorphic() ) { - final java.util.Collection embeddableSubclasses = component.getDiscriminatorValues().values(); + final var embeddableSubclasses = component.getDiscriminatorValues().values(); final java.util.Map> domainTypes = new HashMap<>(); domainTypes.put( embeddableType.getTypeName(), embeddableType ); - final ClassLoaderService classLoaderService = + final var classLoaderService = context.getRuntimeModelCreationContext().getBootstrapContext().getClassLoaderService(); for ( final String subclassName : embeddableSubclasses ) { if ( domainTypes.containsKey( subclassName ) ) { @@ -283,7 +262,7 @@ private static EmbeddableDomainType classEmbeddableType(MetadataContext c continue; } final Class subclass = classLoaderService.classForName( subclassName ); - final EmbeddableTypeImpl subType = new EmbeddableTypeImpl<>( + final var subType = new EmbeddableTypeImpl<>( context.getJavaTypeRegistry().resolveManagedTypeDescriptor( subclass ), domainTypes.get( component.getSuperclass( subclassName ) ), discriminatorType, @@ -298,8 +277,8 @@ private static EmbeddableDomainType classEmbeddableType(MetadataContext c return embeddableType; } - private static EmbeddableTypeImpl dynamicEmbeddableType(MetadataContext context, Component component) { - final EmbeddableTypeImpl embeddableType = new EmbeddableTypeImpl<>( + private static EmbeddableTypeImpl dynamicEmbeddableType(MetadataContext context, Component component) { + final var embeddableType = new EmbeddableTypeImpl<>( context.getJavaTypeRegistry().getDescriptor( java.util.Map.class ), null, null, @@ -309,9 +288,9 @@ private static EmbeddableTypeImpl dynamicEmbeddableType(MetadataContext c context.registerComponentByEmbeddable( embeddableType, component); - final EmbeddableTypeImpl.InFlightAccess inFlightAccess = embeddableType.getInFlightAccess(); + final var inFlightAccess = embeddableType.getInFlightAccess(); for ( Property property : component.getProperties() ) { - final PersistentAttribute attribute = buildAttribute( embeddableType, property, context); + final var attribute = buildAttribute( embeddableType, property, context); if ( attribute != null ) { inFlightAccess.addAttribute( attribute ); } @@ -321,17 +300,17 @@ private static EmbeddableTypeImpl dynamicEmbeddableType(MetadataContext c return embeddableType; } - private static DomainType entityDomainType(ValueContext typeContext, MetadataContext context) { - final org.hibernate.type.Type type = typeContext.getHibernateValue().getType(); + private static DomainType entityDomainType(ValueContext typeContext, MetadataContext context) { + final var type = typeContext.getHibernateValue().getType(); if ( type instanceof EntityType entityType ) { - final IdentifiableDomainType domainType = - context.locateIdentifiableType( entityType.getAssociatedEntityName() ); + final var domainType = context.locateIdentifiableType( entityType.getAssociatedEntityName() ); if ( domainType == null ) { // Due to the use of generics, it can happen that a mapped super class uses a type // for an attribute that is not a managed type. Since this case is not specifically mentioned // in the Jakarta Persistence spec, we handle this by returning a "dummy" entity type - final JavaType domainJavaType = - context.getJavaTypeRegistry().resolveDescriptor( typeContext.getJpaBindableType() ); + final var domainJavaType = + context.getJavaTypeRegistry() + .resolveDescriptor( typeContext.getJpaBindableType() ); return new EntityTypeImpl<>( domainJavaType, context.getJpaMetamodel() ); } else { @@ -339,7 +318,6 @@ private static DomainType entityDomainType(ValueContext typeContext, Meta } } - assert type instanceof AnyType; final AnyType anyType = (AnyType) type; return new AnyMappingDomainTypeImpl<>( (Any) typeContext.getHibernateValue(), @@ -349,27 +327,25 @@ private static DomainType entityDomainType(ValueContext typeContext, Meta ); } - @SuppressWarnings( "unchecked" ) - private static DomainType basicDomainType(ValueContext typeContext, MetadataContext context) { + private static DomainType basicDomainType(ValueContext typeContext, MetadataContext context) { final Value hibernateValue = typeContext.getHibernateValue(); if ( typeContext.getJpaBindableType().isPrimitive() && ( (SimpleValue) hibernateValue ).getJpaAttributeConverterDescriptor() == null ) { // Special BasicDomainType necessary for primitive types in the JPA metamodel. // When a converted is applied to the attribute we already resolve to the correct type - final Class type = (Class) typeContext.getJpaBindableType(); + final var type = typeContext.getJpaBindableType(); return context.resolveBasicType( type ); } else { - final org.hibernate.type.Type type = hibernateValue.getType(); + final var type = hibernateValue.getType(); if ( type instanceof BasicPluralType pluralType ) { if ( pluralType.getElementType().getJavaTypeDescriptor() instanceof EmbeddableAggregateJavaType ) { - final AggregateColumn aggregateColumn = - (AggregateColumn) hibernateValue.getColumns().get( 0 ); + final var aggregateColumn = (AggregateColumn) hibernateValue.getColumns().get( 0 ); classEmbeddableType( context, aggregateColumn.getComponent() ); } } - return (DomainType) type; + return (DomainType) type; } } @@ -378,9 +354,8 @@ private static JavaType determineRelationalJavaType( DomainType metaModelType, MetadataContext context) { if ( typeContext.getValueClassification() == ValueClassification.BASIC ) { - final var descriptor = - ( (SimpleValue) typeContext.getHibernateValue() ) - .getJpaAttributeConverterDescriptor(); + final var value = (SimpleValue) typeContext.getHibernateValue(); + final var descriptor = value.getJpaAttributeConverterDescriptor(); if ( descriptor != null ) { return context.getJavaTypeRegistry().resolveDescriptor( descriptor.getRelationalValueResolvedType().getErasedType() @@ -399,18 +374,17 @@ private static EntityPersister getDeclaringEntity( private static EntityPersister getDeclarerEntityPersister( AbstractIdentifiableType ownerType, MetadataContext metadataContext) { - final Type.PersistenceType persistenceType = ownerType.getPersistenceType(); - if ( persistenceType == Type.PersistenceType.ENTITY ) { - return metadataContext.getMetamodel().getEntityDescriptor( ownerType.getTypeName() ); - } - else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) { - final PersistentClass persistentClass = - metadataContext.getPersistentClassHostingProperties( (MappedSuperclassTypeImpl) ownerType ); - return persistentClass != null ? metadataContext.getMetamodel().findEntityDescriptor( persistentClass.getClassName() ) : null; - } - else { - throw new AssertionFailure( "Cannot get the metamodel for PersistenceType: " + persistenceType ); - } + final var metamodel = metadataContext.getMetamodel(); + final var persistenceType = ownerType.getPersistenceType(); + return switch ( persistenceType ) { + case ENTITY -> metamodel.getEntityDescriptor( ownerType.getTypeName() ); + case MAPPED_SUPERCLASS -> { + final var ownerAsSuper = (MappedSuperclassTypeImpl) ownerType; + final var persistentClass = metadataContext.getPersistentClassHostingProperties( ownerAsSuper ); + yield persistentClass == null ? null : metamodel.findEntityDescriptor( persistentClass.getClassName() ); + } + default -> throw new AssertionFailure( "Cannot get the metamodel for PersistenceType: " + persistenceType ); + }; } /** @@ -420,17 +394,16 @@ else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) { * @param attributeContext The attribute to be described * @param memberResolver Strategy for how to resolve the member defining the attribute. * @param The owner type - * @param The attribute type * * @return The attribute description */ - private AttributeMetadata determineAttributeMetadata( + private AttributeMetadata determineAttributeMetadata( AttributeContext attributeContext, MemberResolver memberResolver) { return determineAttributeMetadata( attributeContext, memberResolver, context ); } - private static AttributeMetadata determineAttributeMetadata( + private static AttributeMetadata determineAttributeMetadata( AttributeContext attributeContext, MemberResolver memberResolver, MetadataContext context) { @@ -439,11 +412,11 @@ private static AttributeMetadata determineAttributeMetadata( log.tracef( "Starting attribute metadata determination [%s]", propertyName ); - final Member member = memberResolver.resolveMember( attributeContext, context ); + final var member = memberResolver.resolveMember( attributeContext, context ); log.tracef( "\tMember: %s", member ); - final Value value = propertyMapping.getValue(); - final org.hibernate.type.Type type = value.getType(); + final var value = propertyMapping.getValue(); + final var type = value.getType(); log.tracef( "\tType: %s [%s]", type.getName(), type.getClass().getSimpleName() ); if ( type instanceof AnyType ) { @@ -466,7 +439,7 @@ else if ( type instanceof EntityType ) { else if ( type instanceof CollectionType ) { // collection if ( value instanceof Collection collection ) { - final org.hibernate.type.Type elementType = collection.getElement().getType(); + final var elementType = collection.getElement().getType(); final boolean isManyToMany = isManyToMany( member ); return new PluralAttributeMetadataImpl<>( propertyMapping, @@ -629,7 +602,7 @@ else if ( member instanceof Method method ) { private static final MemberResolver embeddedMemberResolver = (attributeContext, metadataContext) -> { // the owner is an embeddable - final EmbeddableDomainType ownerType = (EmbeddableDomainType) attributeContext.getOwnerType(); + final var ownerType = (EmbeddableDomainType) attributeContext.getOwnerType(); return resolveEmbeddedMember( attributeContext.getPropertyMapping(), ownerType, metadataContext ); }; @@ -637,62 +610,51 @@ private static Member resolveEmbeddedMember( Property property, EmbeddableDomainType ownerType, MetadataContext metadataContext) { - final Component ownerBootDescriptor = metadataContext.getEmbeddableBootDescriptor( ownerType ); - - final CompositeTypeImplementor ownerComponentType = (CompositeTypeImplementor) ownerBootDescriptor.getType(); - final EmbeddableValuedModelPart ownerMappingModelDescriptor = ownerComponentType.getMappingModelPart(); - - final EmbeddableRepresentationStrategy ownerRepStrategy = - ownerRepresentationStrategy( metadataContext, ownerMappingModelDescriptor, ownerBootDescriptor ); - - if ( ownerRepStrategy.getMode() == RepresentationMode.MAP ) { - return new MapMember( property.getName(), property.getType().getReturnedClass() ); - } - else { - return ownerRepStrategy - .resolvePropertyAccess( property ) - .getGetter() - .getMember(); - } + final Component embeddable = metadataContext.getEmbeddableBootDescriptor( ownerType ); + final var ownerComponentType = (CompositeTypeImplementor) embeddable.getType(); + final var ownerMappingModel = ownerComponentType.getMappingModelPart(); + final var ownerRepStrategy = ownerRepresentationStrategy( metadataContext, ownerMappingModel, embeddable ); + return ownerRepStrategy.getMode() == RepresentationMode.MAP + ? new MapMember( property.getName(), property.getType().getReturnedClass() ) + : ownerRepStrategy.resolvePropertyAccess( property ).getGetter().getMember(); } private static EmbeddableRepresentationStrategy ownerRepresentationStrategy( - MetadataContext metadataContext, EmbeddableValuedModelPart ownerMappingModelDescriptor, Component ownerBootDescriptor) { + MetadataContext metadataContext, + EmbeddableValuedModelPart ownerMappingModelDescriptor, + Component ownerBootDescriptor) { if ( ownerMappingModelDescriptor == null ) { // When an entity uses a type variable, bound by a mapped superclass, for an embedded id, // we will not create a model part for the component, but we still need the representation strategy here, // in order to discover the property members to expose on the JPA metamodel - return ownerBootDescriptor.getBuildingContext() - .getBootstrapContext() + return ownerBootDescriptor.getBuildingContext().getBootstrapContext() .getRepresentationStrategySelector() .resolveStrategy( ownerBootDescriptor, null, metadataContext.getRuntimeModelCreationContext() ); } else { - return ownerMappingModelDescriptor - .getEmbeddableTypeDescriptor() + return ownerMappingModelDescriptor.getEmbeddableTypeDescriptor() .getRepresentationStrategy(); } } private static final MemberResolver virtualIdentifierMemberResolver = (attributeContext, metadataContext) -> { - final AbstractIdentifiableType identifiableType = (AbstractIdentifiableType) attributeContext.getOwnerType(); - final EntityPersister declaringEntity = getDeclaringEntity( identifiableType, metadataContext ); + final var identifiableType = (AbstractIdentifiableType) attributeContext.getOwnerType(); + final var declaringEntity = getDeclaringEntity( identifiableType, metadataContext ); return resolveVirtualIdentifierMember( attributeContext.getPropertyMapping(), declaringEntity ); }; private static Member resolveVirtualIdentifierMember( Property property, EntityPersister entityPersister) { - final EntityIdentifierMapping identifierMapping = entityPersister.getIdentifierMapping(); - + final var identifierMapping = entityPersister.getIdentifierMapping(); if ( identifierMapping.getNature() != EntityIdentifierMapping.Nature.VIRTUAL ) { throw new IllegalArgumentException( "expecting IdClass mapping" ); } - final CompositeIdentifierMapping cid = (CompositeIdentifierMapping) identifierMapping; - final EmbeddableMappingType embeddable = cid.getPartMappingType(); + final var cid = (CompositeIdentifierMapping) identifierMapping; + final var embeddable = cid.getPartMappingType(); final String attributeName = property.getName(); - final AttributeMapping attributeMapping = embeddable.findAttributeMapping( attributeName ); + final var attributeMapping = embeddable.findAttributeMapping( attributeName ); if ( attributeMapping == null ) { throw new PropertyNotFoundException( "Could not resolve attribute '" + attributeName @@ -710,9 +672,9 @@ private static Member resolveVirtualIdentifierMember( Property property, EntityP * A {@link Member} resolver for normal attributes. */ private static final MemberResolver normalMemberResolver = (attributeContext, metadataContext) -> { - final ManagedDomainType ownerType = attributeContext.getOwnerType(); + final var ownerType = attributeContext.getOwnerType(); final Property property = attributeContext.getPropertyMapping(); - final Type.PersistenceType persistenceType = ownerType.getPersistenceType(); + final var persistenceType = ownerType.getPersistenceType(); return switch ( persistenceType ) { case ENTITY -> resolveEntityMember( property, @@ -727,7 +689,7 @@ private static Member resolveVirtualIdentifierMember( Property property, EntityP private static Member resolveEntityMember(Property property, EntityPersister declaringEntity) { final String propertyName = property.getName(); - final AttributeMapping attributeMapping = declaringEntity.findAttributeMapping( propertyName ); + final var attributeMapping = declaringEntity.findAttributeMapping( propertyName ); return attributeMapping == null // just like in #determineIdentifierJavaMember , this *should* indicate we have an IdClass mapping ? resolveVirtualIdentifierMember( property, declaringEntity ) @@ -761,19 +723,18 @@ private static Member resolveMappedSuperclassMember( } private final MemberResolver identifierMemberResolver = (attributeContext, metadataContext) -> { - final AbstractIdentifiableType identifiableType = - (AbstractIdentifiableType) attributeContext.getOwnerType(); + final var identifiableType = (AbstractIdentifiableType) attributeContext.getOwnerType(); if ( identifiableType instanceof SqmMappedSuperclassDomainType ) { return attributeContext.getPropertyMapping() .getGetter( identifiableType.getJavaType() ) .getMember(); } else { - final EntityPersister declaringEntityMapping = getDeclaringEntity( identifiableType, metadataContext ); - final EntityIdentifierMapping identifierMapping = declaringEntityMapping.getIdentifierMapping(); + final var declaringEntityMapping = getDeclaringEntity( identifiableType, metadataContext ); + final var identifierMapping = declaringEntityMapping.getIdentifierMapping(); final Property propertyMapping = attributeContext.getPropertyMapping(); return !propertyMapping.getName().equals( identifierMapping.getAttributeName() ) - // this *should* indicate processing part of an IdClass... + // this *should* indicate processing part of an IdClass ? virtualIdentifierMemberResolver.resolveMember( attributeContext, metadataContext ) : getter( declaringEntityMapping, propertyMapping, identifierMapping.getAttributeName(), identifierMapping.getJavaType().getJavaTypeClass() ); @@ -781,22 +742,21 @@ private static Member resolveMappedSuperclassMember( }; private final MemberResolver versionMemberResolver = (attributeContext, metadataContext) -> { - final AbstractIdentifiableType identifiableType = - (AbstractIdentifiableType) attributeContext.getOwnerType(); + final var identifiableType = (AbstractIdentifiableType) attributeContext.getOwnerType(); if ( identifiableType instanceof SqmMappedSuperclassDomainType ) { return attributeContext.getPropertyMapping() .getGetter( identifiableType.getJavaType() ) .getMember(); } else { - final EntityPersister entityPersister = getDeclaringEntity( identifiableType, metadataContext ); - final EntityVersionMapping versionMapping = entityPersister.getVersionMapping(); + final var entityPersister = getDeclaringEntity( identifiableType, metadataContext ); + final var versionMapping = entityPersister.getVersionMapping(); assert entityPersister.isVersioned(); assert versionMapping != null; final String versionPropertyName = attributeContext.getPropertyMapping().getName(); if ( !versionPropertyName.equals( versionMapping.getVersionAttribute().getAttributeName() ) ) { - // this should never happen, but to be safe... + // this should never happen but check it to be safe throw new IllegalArgumentException( "Given property did not match declared version property" ); } return getter( entityPersister, attributeContext.getPropertyMapping(), diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/JpaStaticMetamodelPopulationSetting.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/JpaStaticMetamodelPopulationSetting.java index 7f829102216c..9190cc51c39c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/JpaStaticMetamodelPopulationSetting.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/JpaStaticMetamodelPopulationSetting.java @@ -7,9 +7,10 @@ import java.util.Locale; import java.util.Map; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.internal.util.config.ConfigurationHelper; +import static org.hibernate.cfg.MappingSettings.STATIC_METAMODEL_POPULATION; + /** * Enumerated setting used to control whether Hibernate looks for and populates * JPA static metamodel models of application's domain model. @@ -41,13 +42,14 @@ public static JpaStaticMetamodelPopulationSetting parse(String setting) { }; } - public static JpaStaticMetamodelPopulationSetting determineJpaStaticMetaModelPopulationSetting(Map configurationValues) { + public static JpaStaticMetamodelPopulationSetting determineJpaStaticMetaModelPopulationSetting( + Map configurationValues) { return parse( determineSetting( configurationValues ) ); } - private static String determineSetting(Map configurationValues) { + private static String determineSetting(Map configurationValues) { return ConfigurationHelper.getString( - AvailableSettings.STATIC_METAMODEL_POPULATION, + STATIC_METAMODEL_POPULATION, configurationValues, "skipUnsupported" ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java index d6c80739eeb1..0afe0f194f1c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java @@ -5,7 +5,6 @@ package org.hibernate.metamodel.internal; import jakarta.persistence.metamodel.Attribute; -import jakarta.persistence.metamodel.SingularAttribute; import jakarta.persistence.metamodel.Type; import org.hibernate.AssertionFailure; import org.hibernate.Internal; @@ -142,6 +141,18 @@ MappingMetamodel getMetamodel() { return metamodel; } + private PersistentAttribute buildAttribute(ManagedDomainType domainType, Property property) { + return attributeFactory.buildAttribute( domainType, property ); + } + + private SingularPersistentAttribute buildIdAttribute(IdentifiableDomainType type, Property property) { + return attributeFactory.buildIdAttribute( type, property ); + } + + private SingularPersistentAttribute buildVersionAttribute(IdentifiableDomainType type, Property property) { + return attributeFactory.buildVersionAttribute( type, property ); + } + /** * Retrieves the {@linkplain Class java type} to {@link EntityTypeImpl} map. * @@ -179,11 +190,10 @@ public void registerEntityType(PersistentClass persistentClass, EntityTypeImpl< public void registerEmbeddableType( EmbeddableDomainType embeddableType, Component bootDescriptor) { - assert embeddableType.getJavaType() != null; - assert ! Map.class.isAssignableFrom( embeddableType.getJavaType() ); - - embeddablesToProcess - .computeIfAbsent( embeddableType.getJavaType(), k -> new ArrayList<>( 1 ) ) + final var javaType = embeddableType.getJavaType(); + assert javaType != null; + assert !Map.class.isAssignableFrom( javaType ); + embeddablesToProcess.computeIfAbsent( javaType, k -> new ArrayList<>( 1 ) ) .add( embeddableType ); registerComponentByEmbeddable( embeddableType, bootDescriptor ); } @@ -243,9 +253,8 @@ public EntityDomainType locateEntityType(Class javaType) { * * @return The corresponding JPA {@link org.hibernate.type.EntityType}, or null. */ - @SuppressWarnings("unchecked") - public IdentifiableDomainType locateIdentifiableType(String entityName) { - return (IdentifiableDomainType) identifiableTypesByName.get( entityName ); + public IdentifiableDomainType locateIdentifiableType(String entityName) { + return identifiableTypesByName.get( entityName ); } public Map> getIdentifiableTypesByName() { @@ -265,12 +274,12 @@ public Map> getIdentifiableTypesByName() { final Property genericProperty = property.copy(); genericProperty.setValue( genericComponent ); genericProperty.setGeneric( true ); - final PersistentAttribute attribute = factoryFunction.apply( entityType, genericProperty ); + final var attribute = factoryFunction.apply( entityType, genericProperty ); if ( !property.isGeneric() ) { - final PersistentAttribute concreteAttribute = factoryFunction.apply( entityType, property ); + final var concreteAttribute = factoryFunction.apply( entityType, property ); if ( concreteAttribute != null ) { - @SuppressWarnings("unchecked") - final AttributeContainer attributeContainer = (AttributeContainer) entityType; + final var managedType = (ManagedDomainType) entityType; + final var attributeContainer = (AttributeContainer) managedType; attributeContainer.getInFlightAccess().addConcreteGenericAttribute( concreteAttribute ); } } @@ -287,31 +296,30 @@ public void wrapUp() { } final boolean staticMetamodelScanEnabled = - this.jpaStaticMetaModelPopulationSetting != JpaStaticMetamodelPopulationSetting.DISABLED; + jpaStaticMetaModelPopulationSetting != JpaStaticMetamodelPopulationSetting.DISABLED; final Set processedMetamodelClasses = new HashSet<>(); //we need to process types from superclasses to subclasses for ( Object mapping : orderedMappings ) { - if ( PersistentClass.class.isAssignableFrom( mapping.getClass() ) ) { - final PersistentClass safeMapping = (PersistentClass) mapping; + if ( mapping instanceof PersistentClass persistentClass ) { if ( log.isTraceEnabled() ) { - log.trace( "Starting entity [" + safeMapping.getEntityName() + ']' ); + log.trace( "Starting entity [" + persistentClass.getEntityName() + ']' ); } try { - final EntityDomainType jpaMapping = entityTypesByPersistentClass.get( safeMapping ); + final var jpaMapping = entityTypesByPersistentClass.get( persistentClass ); - applyIdMetadata( safeMapping, jpaMapping ); - applyVersionAttribute( safeMapping, jpaMapping ); - applyGenericProperties( safeMapping, jpaMapping ); + applyIdMetadata( persistentClass, jpaMapping ); + applyVersionAttribute( persistentClass, jpaMapping ); + applyGenericProperties( persistentClass, jpaMapping ); - for ( Property property : safeMapping.getDeclaredProperties() ) { - if ( property.getValue() == safeMapping.getIdentifierMapper() ) { + for ( Property property : persistentClass.getDeclaredProperties() ) { + if ( property.getValue() == persistentClass.getIdentifierMapper() ) { // property represents special handling for id-class mappings but we have already // accounted for the embedded property mappings in #applyIdMetadata && // #buildIdClassAttributes continue; } - if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) { + if ( persistentClass.isVersioned() && property == persistentClass.getVersion() ) { // skip the version property, it was already handled previously. continue; } @@ -326,30 +334,29 @@ public void wrapUp() { } finally { if ( log.isTraceEnabled() ) { - log.trace( "Completed entity [" + safeMapping.getEntityName() + ']' ); + log.trace( "Completed entity [" + persistentClass.getEntityName() + ']' ); } } } - else if ( MappedSuperclass.class.isAssignableFrom( mapping.getClass() ) ) { - final MappedSuperclass safeMapping = (MappedSuperclass) mapping; + else if ( mapping instanceof MappedSuperclass mappedSuperclass ) { if ( log.isTraceEnabled() ) { - log.trace( "Starting mapped superclass [" + safeMapping.getMappedClass().getName() + ']' ); + log.trace( "Starting mapped superclass [" + mappedSuperclass.getMappedClass().getName() + ']' ); } try { - final var jpaType = mappedSuperclassByMappedSuperclassMapping.get( safeMapping ); + final var jpaType = mappedSuperclassByMappedSuperclassMapping.get( mappedSuperclass ); - applyIdMetadata( safeMapping, jpaType ); - applyVersionAttribute( safeMapping, jpaType ); + applyIdMetadata( mappedSuperclass, jpaType ); + applyVersionAttribute( mappedSuperclass, jpaType ); // applyNaturalIdAttribute( safeMapping, jpaType ); - for ( Property property : safeMapping.getDeclaredProperties() ) { - if ( isIdentifierProperty( property, safeMapping ) ) { + for ( Property property : mappedSuperclass.getDeclaredProperties() ) { + if ( isIdentifierProperty( property, mappedSuperclass ) ) { // property represents special handling for id-class mappings but we have already // accounted for the embedded property mappings in #applyIdMetadata && // #buildIdClassAttributes continue; } - else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) { + else if ( mappedSuperclass.isVersioned() && property == mappedSuperclass.getVersion() ) { // skip the version property, it was already handled previously. continue; } @@ -364,7 +371,7 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) { } finally { if ( log.isTraceEnabled() ) { - log.trace( "Completed mapped superclass [" + safeMapping.getMappedClass().getName() + ']' ); + log.trace( "Completed mapped superclass [" + mappedSuperclass.getMappedClass().getName() + ']' ); } } } @@ -383,7 +390,7 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) { embeddablesToProcess.clear(); - for ( EmbeddableDomainType embeddable : processingEmbeddables ) { + for ( var embeddable : processingEmbeddables ) { final Component component = componentByEmbeddable.get( embeddable ); for ( Property property : component.getProperties() ) { if ( !component.isPolymorphic() @@ -395,7 +402,8 @@ else if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) { ( ( AttributeContainer) embeddable ).getInFlightAccess().finishUp(); // Do not process embeddables for entity types i.e. id-classes or // generic component embeddables used just for concrete type resolution - if ( !component.isGeneric() && !( embeddable.getExpressibleJavaType() instanceof EntityJavaType ) ) { + if ( !component.isGeneric() + && !( embeddable.getExpressibleJavaType() instanceof EntityJavaType ) ) { embeddables.put( embeddable.getJavaType(), embeddable ); if ( staticMetamodelScanEnabled ) { populateStaticMetamodel( embeddable, processedMetamodelClasses ); @@ -412,15 +420,14 @@ private static boolean isIdentifierProperty(Property property, MappedSuperclass } private void addAttribute(EmbeddableDomainType embeddable, Property property, Component component) { - final PersistentAttribute attribute = - attributeFactory.buildAttribute( embeddable, property); + final var attribute = buildAttribute( embeddable, property); if ( attribute != null ) { final Property superclassProperty = getMappedSuperclassProperty( property.getName(), component.getMappedSuperclass() ); if ( superclassProperty != null && superclassProperty.isGeneric() ) { - @SuppressWarnings("unchecked") - final var attributeContainer = (AttributeContainer) embeddable; + final var managedType = (ManagedDomainType) embeddable; + final var attributeContainer = (AttributeContainer) managedType; attributeContainer.getInFlightAccess().addConcreteGenericAttribute( attribute ); } else { @@ -430,33 +437,29 @@ private void addAttribute(EmbeddableDomainType embeddable, Property prope } private void buildAttribute(Property property, IdentifiableDomainType jpaType) { - final PersistentAttribute attribute = - buildAttribute( property, jpaType, attributeFactory::buildAttribute ); + final var attribute = buildAttribute( property, jpaType, this::buildAttribute ); if ( attribute != null ) { addAttribute( jpaType, attribute ); if ( property.isNaturalIdentifier() ) { - @SuppressWarnings("unchecked") - final AttributeContainer attributeContainer = (AttributeContainer) jpaType; + final var managedType = (ManagedDomainType) jpaType; + final var attributeContainer = (AttributeContainer) managedType; attributeContainer.getInFlightAccess().applyNaturalIdAttribute( attribute ); } } } private void addAttribute(ManagedDomainType type, PersistentAttribute attribute) { - @SuppressWarnings("unchecked") - final AttributeContainer container = (AttributeContainer) type; - final AttributeContainer.InFlightAccess inFlightAccess = container.getInFlightAccess(); + final var container = (AttributeContainer) type; + final var inFlightAccess = container.getInFlightAccess(); final boolean virtual = attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED && attribute.getAttributeJavaType() instanceof EntityJavaType; if ( virtual ) { @SuppressWarnings("unchecked") - final EmbeddableDomainType embeddableDomainType = - (EmbeddableDomainType) attribute.getValueGraphType(); + final var embeddableDomainType = (EmbeddableDomainType) attribute.getValueGraphType(); final Component component = componentByEmbeddable.get( embeddableDomainType ); for ( Property property : component.getProperties() ) { - final PersistentAttribute subAttribute = - attributeFactory.buildAttribute( embeddableDomainType, property ); + final var subAttribute = buildAttribute( embeddableDomainType, property ); if ( subAttribute != null ) { inFlightAccess.addAttribute( subAttribute ); } @@ -474,23 +477,22 @@ private void addAttribute(ManagedDomainType type, PersistentAttribute void applyIdMetadata(PersistentClass persistentClass, IdentifiableDomainType identifiableType) { if ( persistentClass.hasIdentifierProperty() ) { + final var managedType = (ManagedDomainType) identifiableType; + final var attributeContainer = (AttributeContainer) managedType; final Property declaredIdentifierProperty = persistentClass.getDeclaredIdentifierProperty(); - @SuppressWarnings("unchecked") - final AttributeContainer attributeContainer = (AttributeContainer) identifiableType; if ( declaredIdentifierProperty != null ) { final var idAttribute = (SingularPersistentAttribute) buildAttribute( declaredIdentifierProperty, identifiableType, - attributeFactory::buildIdAttribute ); + this::buildIdAttribute ); attributeContainer.getInFlightAccess().applyIdAttribute( idAttribute ); } else { final Property superclassIdentifier = getMappedSuperclassIdentifier( persistentClass ); if ( superclassIdentifier != null && superclassIdentifier.isGeneric() ) { - // If the superclass identifier is generic we have to build the attribute to register the concrete type + // If the superclass identifier is generic, we have to build the attribute to register the concrete type final var concreteIdentifier = - attributeFactory.buildIdAttribute( identifiableType, - persistentClass.getIdentifierProperty() ); + buildIdAttribute( identifiableType, persistentClass.getIdentifierProperty() ); attributeContainer.getInFlightAccess().addConcreteGenericAttribute( concreteIdentifier ); } } @@ -501,36 +503,39 @@ private void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo throw new MappingException( "Expecting Component for id mapping with no id-attribute" ); } - // Handle the actual id-attributes - final List cidProperties; - final int propertySpan; - final EmbeddableTypeImpl idClassType; - final Component identifierMapper = persistentClass.getIdentifierMapper(); - if ( identifierMapper != null ) { - cidProperties = identifierMapper.getProperties(); - propertySpan = identifierMapper.getPropertySpan(); - if ( identifierMapper.getComponentClassName() == null ) { - // support for no id-class, especially for dynamic models - idClassType = null; - } - else { - idClassType = applyIdClassMetadata( (Component) persistentClass.getIdentifier() ); - } - } - else { - cidProperties = compositeId.getProperties(); - propertySpan = compositeId.getPropertySpan(); - idClassType = null; - } - - assert compositeId.isEmbedded(); + applyIdAttributes( persistentClass, identifiableType, compositeId ); + } + } - final IdentifiableDomainType idDomainType = - identifiableTypesByName.get( compositeId.getOwner().getEntityName() ); - @SuppressWarnings("unchecked") - final var idType = (AbstractIdentifiableType) idDomainType; - applyIdAttributes( identifiableType, idType, propertySpan, cidProperties, idClassType ); + private void applyIdAttributes( + PersistentClass persistentClass, + IdentifiableDomainType identifiableType, + Component compositeId) { + // Handle the actual id attributes + final List cidProperties; + final int propertySpan; + final EmbeddableTypeImpl idClassType; + final Component identifierMapper = persistentClass.getIdentifierMapper(); + if ( identifierMapper != null ) { + cidProperties = identifierMapper.getProperties(); + propertySpan = identifierMapper.getPropertySpan(); + idClassType = + identifierMapper.getComponentClassName() == null + ? null // support for no id-class, especially for dynamic models + : applyIdClassMetadata( (Component) persistentClass.getIdentifier() ); } + else { + cidProperties = compositeId.getProperties(); + propertySpan = compositeId.getPropertySpan(); + idClassType = null; + } + + assert compositeId.isEmbedded(); + + final var idDomainType = identifiableTypesByName.get( compositeId.getOwner().getEntityName() ); + @SuppressWarnings("unchecked") + final var idType = (AbstractIdentifiableType) idDomainType; + applyIdAttributes( identifiableType, idType, propertySpan, cidProperties, idClassType ); } private void applyIdAttributes( @@ -539,17 +544,27 @@ private void applyIdAttributes( int propertySpan, List cidProperties, EmbeddableTypeImpl idClassType) { - Set> idAttributes = idType.getIdClassAttributesSafely(); - if ( idAttributes == null ) { - idAttributes = new HashSet<>( propertySpan ); + final var idAttributes = idClassAttributes( idType, propertySpan, cidProperties ); + final var managedType = (ManagedDomainType) identifiableType; + final var container = (AttributeContainer) managedType; + container.getInFlightAccess().applyNonAggregatedIdAttributes( idAttributes, idClassType); + } + + private Set> idClassAttributes( + AbstractIdentifiableType idType, + int propertySpan, + List cidProperties) { + final var idAttributes = idType.getIdClassAttributesSafely(); + if ( idAttributes != null ) { + return idAttributes; + } + else { + final Set> result = new HashSet<>( propertySpan ); for ( Property cidSubproperty : cidProperties ) { - idAttributes.add( attributeFactory.buildIdAttribute( idType, cidSubproperty ) ); + result.add( buildIdAttribute( idType, cidSubproperty ) ); } + return result; } - - @SuppressWarnings("unchecked") - final var container = (AttributeContainer) identifiableType; - container.getInFlightAccess().applyNonAggregatedIdAttributes( idAttributes, idClassType); } private Property getMappedSuperclassIdentifier(PersistentClass persistentClass) { @@ -565,10 +580,9 @@ private Property getMappedSuperclassIdentifier(PersistentClass persistentClass) } private EmbeddableTypeImpl applyIdClassMetadata(Component idClassComponent) { - final EmbeddableTypeImpl embeddableType = - new EmbeddableTypeImpl<>( - getTypeConfiguration().getJavaTypeRegistry() - .resolveManagedTypeDescriptor( idClassComponent.getComponentClass() ), + final var embeddableType = + new EmbeddableTypeImpl( + getJavaTypeRegistry().resolveManagedTypeDescriptor( idClassComponent.getComponentClass() ), getMappedSuperclassDomainType( idClassComponent ), null, false, @@ -587,15 +601,15 @@ private MappedSuperclassDomainType getMappedSuperclassDomainType( } private void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType jpaMappingType) { - @SuppressWarnings("unchecked") - final var attributeContainer = (AttributeContainer) jpaMappingType; + final var managedType = (ManagedDomainType) jpaMappingType; + final var attributeContainer = (AttributeContainer) managedType; if ( mappingType.hasIdentifierProperty() ) { final Property declaredIdentifierProperty = mappingType.getDeclaredIdentifierProperty(); if ( declaredIdentifierProperty != null ) { final var attribute = (SingularPersistentAttribute) buildAttribute( declaredIdentifierProperty, jpaMappingType, - attributeFactory::buildIdAttribute ); + this::buildIdAttribute ); attributeContainer.getInFlightAccess().applyIdAttribute( attribute ); } } @@ -611,33 +625,33 @@ else if ( mappingType.getIdentifierMapper() != null ) { private void applyVersionAttribute(PersistentClass persistentClass, EntityDomainType jpaEntityType) { final Property declaredVersion = persistentClass.getDeclaredVersion(); if ( declaredVersion != null ) { - @SuppressWarnings("unchecked") - final var attributeContainer = (AttributeContainer) jpaEntityType; + final var managedType = (ManagedDomainType) jpaEntityType; + final var attributeContainer = (AttributeContainer) managedType; attributeContainer.getInFlightAccess() - .applyVersionAttribute( attributeFactory.buildVersionAttribute( jpaEntityType, declaredVersion ) ); + .applyVersionAttribute( buildVersionAttribute( jpaEntityType, declaredVersion ) ); } } private void applyVersionAttribute(MappedSuperclass mappingType, MappedSuperclassDomainType jpaMappingType) { final Property declaredVersion = mappingType.getDeclaredVersion(); if ( declaredVersion != null ) { - @SuppressWarnings("unchecked") - final var attributeContainer = (AttributeContainer) jpaMappingType; + final var managedType = (ManagedDomainType) jpaMappingType; + final var attributeContainer = (AttributeContainer) managedType; attributeContainer.getInFlightAccess() - .applyVersionAttribute( attributeFactory.buildVersionAttribute( jpaMappingType, declaredVersion ) ); + .applyVersionAttribute( buildVersionAttribute( jpaMappingType, declaredVersion ) ); } } private void applyGenericProperties(PersistentClass persistentClass, EntityDomainType entityType) { - MappedSuperclass mappedSuperclass = getMappedSuperclass( persistentClass ); + var mappedSuperclass = getMappedSuperclass( persistentClass ); while ( mappedSuperclass != null ) { for ( Property superclassProperty : mappedSuperclass.getDeclaredProperties() ) { if ( superclassProperty.isGeneric() ) { final Property property = persistentClass.getProperty( superclassProperty.getName() ); - final PersistentAttribute attribute = attributeFactory.buildAttribute( entityType, property ); - @SuppressWarnings("unchecked") - final var attributeContainer = (AttributeContainer) entityType; - attributeContainer.getInFlightAccess().addConcreteGenericAttribute( attribute ); + final var managedType = (ManagedDomainType) entityType; + final var attributeContainer = (AttributeContainer) managedType; + attributeContainer.getInFlightAccess() + .addConcreteGenericAttribute( buildAttribute( entityType, property ) ); } } mappedSuperclass = getMappedSuperclass( mappedSuperclass ); @@ -646,7 +660,7 @@ private void applyGenericProperties(PersistentClass persistentClass, EntityD private MappedSuperclass getMappedSuperclass(PersistentClass persistentClass) { while ( persistentClass != null ) { - final MappedSuperclass mappedSuperclass = persistentClass.getSuperMappedSuperclass(); + final var mappedSuperclass = persistentClass.getSuperMappedSuperclass(); if ( mappedSuperclass != null ) { return mappedSuperclass; } @@ -656,7 +670,7 @@ private MappedSuperclass getMappedSuperclass(PersistentClass persistentClass) { } private MappedSuperclass getMappedSuperclass(MappedSuperclass mappedSuperclass) { - final MappedSuperclass superMappedSuperclass = mappedSuperclass.getSuperMappedSuperclass(); + final var superMappedSuperclass = mappedSuperclass.getSuperMappedSuperclass(); return superMappedSuperclass == null ? getMappedSuperclass( mappedSuperclass.getSuperPersistentClass() ) : superMappedSuperclass; @@ -679,11 +693,9 @@ private Property getMappedSuperclassProperty(String propertyName, MappedSupercla if ( property != null ) { return property; } - else if ( mappedSuperclass.getSuperPersistentClass() != null ) { - return mappedSuperclass.getSuperPersistentClass().getProperty( propertyName ); - } else { - return null; + final var superclass = mappedSuperclass.getSuperPersistentClass(); + return superclass != null ? superclass.getProperty( propertyName ) : null; } } @@ -695,7 +707,7 @@ else if ( mappedSuperclass.getSuperPersistentClass() != null ) { } final Set> attributes = new HashSet<>(); for ( Property property : properties ) { - attributes.add( attributeFactory.buildIdAttribute( ownerType, property ) ); + attributes.add( buildIdAttribute( ownerType, property ) ); } return attributes; } @@ -712,7 +724,7 @@ private void populateStaticMetamodel(ManagedDomainType managedType, Set superType = managedType.getSuperType(); + final var superType = managedType.getSuperType(); if ( superType != null ) { populateStaticMetamodel( superType, processedMetamodelClassName ); } @@ -755,7 +767,7 @@ public Class metamodelClass(ManagedDomainType managedDomainType) { private void registerAttributes(Class metamodelClass, ManagedDomainType managedType) { // push the attributes on to the metamodel class... - for ( Attribute attribute : managedType.getDeclaredAttributes() ) { + for ( var attribute : managedType.getDeclaredAttributes() ) { registerAttribute( metamodelClass, attribute ); } @@ -769,7 +781,7 @@ private void registerAttributes(Class metamodelClass, ManagedDomainType attribute : attributes ) { + for ( var attribute : attributes ) { registerAttribute( metamodelClass, attribute ); } } @@ -791,13 +803,12 @@ private void registerAttribute(Class metamodelClass, Attribute attr final boolean allowNonDeclaredFieldReference = attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED || attribute.getDeclaringType().getPersistenceType() == Type.PersistenceType.EMBEDDABLE; - injectField( metamodelClass, name, attribute, allowNonDeclaredFieldReference ); } catch (NoSuchFieldException e) { log.unableToLocateStaticMetamodelField( metamodelClass.getName(), name ); // throw new AssertionFailure( -// "Unable to locate static metamodel field : " + metamodelClass.getName() + '#' + name +// "Unable to locate static metamodel field: " + metamodelClass.getName() + '#' + name // ); } } @@ -826,7 +837,7 @@ private PersistentClass getEntityWorkedOn() { ); } - public PersistentClass getPersistentClassHostingProperties(MappedSuperclassTypeImpl mappedSuperclassType) { + PersistentClass getPersistentClassHostingProperties(MappedSuperclassTypeImpl mappedSuperclassType) { return mappedSuperClassTypeToPersistentClass.get( mappedSuperclassType ); } @@ -838,7 +849,7 @@ public Set getUnusedMappedSuperclasses() { public BasicDomainType resolveBasicType(Class javaType) { @SuppressWarnings("unchecked") - final BasicDomainType domainType = (BasicDomainType) basicDomainTypeMap.get( javaType ); + final var domainType = (BasicDomainType) basicDomainTypeMap.get( javaType ); if ( domainType == null ) { // we cannot use getTypeConfiguration().standardBasicTypeForJavaType(javaType) // because that doesn't return the right thing for primitive types @@ -851,8 +862,7 @@ public BasicDomainType resolveBasicType(Class javaType) { } private BasicDomainType basicDomainType(Class javaType) { - final JavaType javaTypeDescriptor = - getTypeConfiguration().getJavaTypeRegistry().resolveDescriptor( javaType ); + final JavaType javaTypeDescriptor = getJavaTypeRegistry().resolveDescriptor( javaType ); final JdbcType jdbcType = javaTypeDescriptor.getRecommendedJdbcType( typeConfiguration.getCurrentBaseSqlTypeIndicators() ); return javaType.isPrimitive() @@ -860,24 +870,23 @@ private BasicDomainType basicDomainType(Class javaType) { : new BasicTypeImpl<>( javaTypeDescriptor, jdbcType ); } - public EmbeddableDomainType locateEmbeddable(Class embeddableClass, Component component) { - //noinspection unchecked - EmbeddableDomainType domainType = (EmbeddableDomainType) embeddables.get( embeddableClass ); - if ( domainType == null ) { - final List> embeddableDomainTypes = embeddablesToProcess.get( embeddableClass ); + public EmbeddableDomainType locateEmbeddable(Class embeddableClass, Component component) { + final var domainType = embeddables.get( embeddableClass ); + if ( domainType != null ) { + return domainType; + } + else { + final var embeddableDomainTypes = embeddablesToProcess.get( embeddableClass ); if ( embeddableDomainTypes != null ) { - for ( EmbeddableDomainType embeddableDomainType : embeddableDomainTypes ) { + for ( var embeddableDomainType : embeddableDomainTypes ) { final Component cachedComponent = componentByEmbeddable.get( embeddableDomainType ); if ( cachedComponent.isSame( component ) ) { - //noinspection unchecked - domainType = (EmbeddableDomainType) embeddableDomainType; - break; + return embeddableDomainType; } else if ( cachedComponent.getComponentClass().equals( component.getComponentClass() ) ) { final int cachedComponentPropertySpan = cachedComponent.getPropertySpan(); if ( cachedComponentPropertySpan != component.getPropertySpan() ) { - throw new MappingException( - "Encountered multiple component mappings for the same java class " + throw new MappingException( "Encountered multiple component mappings for the same java class " + embeddableClass.getName() + " with different property mappings. Every property mapping combination should have its own java class" ); } @@ -885,25 +894,22 @@ else if ( cachedComponent.getComponentClass().equals( component.getComponentClas for ( int i = 0; i < cachedComponentPropertySpan; i++ ) { if ( !cachedComponent.getProperty( i ).getName() .equals( component.getProperty( i ).getName() ) ) { - throw new MappingException( - "Encountered multiple component mappings for the same java class " + throw new MappingException( "Encountered multiple component mappings for the same java class " + embeddableClass.getName() + " with different property mappings. Every property mapping combination should have its own java class" ); } } } - //noinspection unchecked - domainType = (EmbeddableDomainType) embeddableDomainType; - break; + return embeddableDomainType; } else { throw new MappingException( "Encountered multiple component mappings for the same java class " - + embeddableClass.getName() + - " with different property mappings. Every property mapping combination should have its own java class" ); + + embeddableClass.getName() + + " with different property mappings. Every property mapping combination should have its own java class" ); } } } + return null; } - return domainType; } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AttributeContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AttributeContainer.java index 627e74bfce97..741b4fc25a25 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AttributeContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AttributeContainer.java @@ -8,13 +8,14 @@ import org.hibernate.metamodel.UnsupportedMappingException; import org.hibernate.metamodel.model.domain.EmbeddableDomainType; +import org.hibernate.metamodel.model.domain.ManagedDomainType; import org.hibernate.metamodel.model.domain.PersistentAttribute; import org.hibernate.metamodel.model.domain.SingularPersistentAttribute; /** * @author Steve Ebersole */ -public interface AttributeContainer { +public interface AttributeContainer extends ManagedDomainType { InFlightAccess getInFlightAccess(); /**