Search in sources :

Example 1 with SelectableMapping

use of org.hibernate.metamodel.mapping.SelectableMapping in project hibernate-orm by hibernate.

the class MappingModelCreationHelper method interpretPluralAttributeMappingKeyDescriptor.

private static void interpretPluralAttributeMappingKeyDescriptor(PluralAttributeMappingImpl attributeMapping, Collection bootValueMapping, CollectionPersister collectionDescriptor, ManagedMappingType declaringType, Dialect dialect, MappingModelCreationProcess creationProcess) {
    ModelPart attributeMappingSubPart = null;
    if (!StringHelper.isEmpty(collectionDescriptor.getMappedByProperty())) {
        attributeMappingSubPart = ((ModelPartContainer) attributeMapping.getElementDescriptor().getPartMappingType()).findSubPart(collectionDescriptor.getMappedByProperty(), null);
    }
    if (attributeMappingSubPart instanceof ToOneAttributeMapping) {
        final ToOneAttributeMapping referencedAttributeMapping = (ToOneAttributeMapping) attributeMappingSubPart;
        setReferencedAttributeForeignKeyDescriptor(attributeMapping, referencedAttributeMapping, referencedAttributeMapping.findContainingEntityMapping().getEntityPersister(), collectionDescriptor.getMappedByProperty(), dialect, creationProcess);
        return;
    }
    final KeyValue bootValueMappingKey = bootValueMapping.getKey();
    final Type keyType = bootValueMappingKey.getType();
    final ModelPart fkTarget;
    final String lhsPropertyName = collectionDescriptor.getCollectionType().getLHSPropertyName();
    final boolean isReferenceToPrimaryKey = lhsPropertyName == null;
    final ManagedMappingType keyDeclaringType;
    if (collectionDescriptor.getElementType().isEntityType()) {
        keyDeclaringType = ((QueryableCollection) collectionDescriptor).getElementPersister();
    } else {
        // This is not "really correct" but it is as good as it gets.
        // The key declaring type serves as declaring type for the inverse model part of a FK.
        // Most of the time, there is a proper managed type, but not for basic collections.
        // Since the declaring type is needed for certain operations, we use the one from the target side of the FK
        keyDeclaringType = declaringType;
    }
    if (isReferenceToPrimaryKey) {
        fkTarget = collectionDescriptor.getOwnerEntityPersister().getIdentifierMapping();
    } else {
        fkTarget = declaringType.findAttributeMapping(lhsPropertyName);
    }
    if (keyType instanceof BasicType) {
        assert bootValueMappingKey.getColumnSpan() == 1;
        assert fkTarget instanceof BasicValuedModelPart;
        final BasicValuedModelPart simpleFkTarget = (BasicValuedModelPart) fkTarget;
        final String tableExpression = getTableIdentifierExpression(bootValueMappingKey.getTable(), creationProcess);
        final SelectableMapping keySelectableMapping = SelectableMappingImpl.from(tableExpression, bootValueMappingKey.getSelectables().get(0), (JdbcMapping) keyType, dialect, creationProcess.getSqmFunctionRegistry());
        attributeMapping.setForeignKeyDescriptor(new SimpleForeignKeyDescriptor(keyDeclaringType, simpleFkTarget, null, keySelectableMapping, simpleFkTarget, isReferenceToPrimaryKey, ((SimpleValue) bootValueMappingKey).isConstrained()));
    } else if (fkTarget instanceof EmbeddableValuedModelPart) {
        final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = buildEmbeddableForeignKeyDescriptor((EmbeddableValuedModelPart) fkTarget, bootValueMapping, keyDeclaringType, collectionDescriptor.getAttributeMapping(), false, dialect, creationProcess);
        attributeMapping.setForeignKeyDescriptor(embeddedForeignKeyDescriptor);
    } else {
        throw new NotYetImplementedFor6Exception("Support for " + fkTarget.getClass() + " foreign keys not yet implemented: " + bootValueMapping.getRole());
    }
}
Also used : ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) KeyValue(org.hibernate.mapping.KeyValue) BasicType(org.hibernate.type.BasicType) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) SimpleValue(org.hibernate.mapping.SimpleValue) BasicType(org.hibernate.type.BasicType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) CollectionMappingType(org.hibernate.metamodel.mapping.CollectionMappingType) JavaType(org.hibernate.type.descriptor.java.JavaType) EntityType(org.hibernate.type.EntityType) ComponentType(org.hibernate.type.ComponentType) CompositeType(org.hibernate.type.CompositeType) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception)

Example 2 with SelectableMapping

use of org.hibernate.metamodel.mapping.SelectableMapping in project hibernate-orm by hibernate.

the class MappingModelCreationHelper method interpretMapKey.

private static CollectionPart interpretMapKey(Collection bootValueMapping, CollectionPersister collectionDescriptor, String tableExpression, String sqlAliasStem, Dialect dialect, MappingModelCreationProcess creationProcess) {
    assert bootValueMapping instanceof IndexedCollection;
    final IndexedCollection indexedCollection = (IndexedCollection) bootValueMapping;
    final Value bootMapKeyDescriptor = indexedCollection.getIndex();
    if (bootMapKeyDescriptor instanceof BasicValue) {
        final BasicValue basicValue = (BasicValue) bootMapKeyDescriptor;
        final SelectableMapping selectableMapping = SelectableMappingImpl.from(tableExpression, basicValue.getSelectables().get(0), basicValue.resolve().getJdbcMapping(), dialect, creationProcess.getSqmFunctionRegistry());
        return new BasicValuedCollectionPart(collectionDescriptor, CollectionPart.Nature.INDEX, basicValue.resolve().getValueConverter(), selectableMapping);
    }
    if (bootMapKeyDescriptor instanceof Component) {
        final Component component = (Component) bootMapKeyDescriptor;
        final CompositeType compositeType = (CompositeType) component.getType();
        final EmbeddableMappingTypeImpl mappingType = EmbeddableMappingTypeImpl.from(component, compositeType, inflightDescriptor -> new EmbeddedCollectionPart(collectionDescriptor, CollectionPart.Nature.INDEX, inflightDescriptor, // parent-injection
        component.getParentProperty(), tableExpression, sqlAliasStem), creationProcess);
        return (CollectionPart) mappingType.getEmbeddedValueMapping();
    }
    if (bootMapKeyDescriptor instanceof OneToMany || bootMapKeyDescriptor instanceof ToOne) {
        final EntityType indexEntityType = (EntityType) collectionDescriptor.getIndexType();
        final EntityPersister associatedEntity = creationProcess.getEntityPersister(indexEntityType.getAssociatedEntityName());
        final EntityCollectionPart indexDescriptor = new EntityCollectionPart(collectionDescriptor, CollectionPart.Nature.INDEX, bootMapKeyDescriptor, associatedEntity, creationProcess);
        creationProcess.registerInitializationCallback("PluralAttributeMapping( " + bootValueMapping.getRole() + ") - index descriptor", () -> {
            indexDescriptor.finishInitialization(collectionDescriptor, bootValueMapping, indexEntityType.getRHSUniqueKeyPropertyName(), creationProcess);
            return true;
        });
        return indexDescriptor;
    }
    throw new NotYetImplementedFor6Exception("Support for plural attributes with index type [" + bootMapKeyDescriptor + "] not yet implemented");
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) OneToMany(org.hibernate.mapping.OneToMany) BasicValue(org.hibernate.mapping.BasicValue) EntityType(org.hibernate.type.EntityType) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) BasicValue(org.hibernate.mapping.BasicValue) SortableValue(org.hibernate.mapping.SortableValue) KeyValue(org.hibernate.mapping.KeyValue) ToOne(org.hibernate.mapping.ToOne) OneToOne(org.hibernate.mapping.OneToOne) ManyToOne(org.hibernate.mapping.ManyToOne) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) Component(org.hibernate.mapping.Component) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) IndexedCollection(org.hibernate.mapping.IndexedCollection) CompositeType(org.hibernate.type.CompositeType)

Example 3 with SelectableMapping

use of org.hibernate.metamodel.mapping.SelectableMapping in project hibernate-orm by hibernate.

the class MappingModelCreationHelper method interpretElement.

private static CollectionPart interpretElement(Collection bootDescriptor, String tableExpression, CollectionPersister collectionDescriptor, String sqlAliasStem, Dialect dialect, MappingModelCreationProcess creationProcess) {
    final Value element = bootDescriptor.getElement();
    if (element instanceof BasicValue) {
        final BasicValue basicElement = (BasicValue) element;
        final SelectableMapping selectableMapping = SelectableMappingImpl.from(tableExpression, basicElement.getSelectables().get(0), basicElement.resolve().getJdbcMapping(), dialect, creationProcess.getSqmFunctionRegistry());
        return new BasicValuedCollectionPart(collectionDescriptor, CollectionPart.Nature.ELEMENT, basicElement.resolve().getValueConverter(), selectableMapping);
    }
    if (element instanceof Component) {
        final Component component = (Component) element;
        final CompositeType compositeType = (CompositeType) collectionDescriptor.getElementType();
        final EmbeddableMappingTypeImpl mappingType = EmbeddableMappingTypeImpl.from(component, compositeType, embeddableMappingType -> new EmbeddedCollectionPart(collectionDescriptor, CollectionPart.Nature.ELEMENT, embeddableMappingType, // parent-injection
        component.getParentProperty(), tableExpression, sqlAliasStem), creationProcess);
        return (CollectionPart) mappingType.getEmbeddedValueMapping();
    }
    if (element instanceof Any) {
        final Any anyBootMapping = (Any) element;
        final SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
        final TypeConfiguration typeConfiguration = sessionFactory.getTypeConfiguration();
        final JavaTypeRegistry jtdRegistry = typeConfiguration.getJavaTypeRegistry();
        final JavaType<Object> baseJtd = jtdRegistry.getDescriptor(Object.class);
        return new DiscriminatedCollectionPart(CollectionPart.Nature.ELEMENT, collectionDescriptor, baseJtd, anyBootMapping, anyBootMapping.getType(), creationProcess);
    }
    if (element instanceof OneToMany || element instanceof ToOne) {
        final EntityType elementEntityType = (EntityType) collectionDescriptor.getElementType();
        final EntityPersister associatedEntity = creationProcess.getEntityPersister(elementEntityType.getAssociatedEntityName());
        final EntityCollectionPart elementDescriptor = new EntityCollectionPart(collectionDescriptor, CollectionPart.Nature.ELEMENT, bootDescriptor.getElement(), associatedEntity, creationProcess);
        creationProcess.registerInitializationCallback("PluralAttributeMapping( " + elementDescriptor.getNavigableRole() + ") - index descriptor", () -> {
            elementDescriptor.finishInitialization(collectionDescriptor, bootDescriptor, elementEntityType.getRHSUniqueKeyPropertyName(), creationProcess);
            return true;
        });
        return elementDescriptor;
    }
    throw new NotYetImplementedFor6Exception("Support for plural attributes with element type [" + element + "] not yet implemented");
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) OneToMany(org.hibernate.mapping.OneToMany) Any(org.hibernate.mapping.Any) BasicValue(org.hibernate.mapping.BasicValue) EntityType(org.hibernate.type.EntityType) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) BasicValue(org.hibernate.mapping.BasicValue) SortableValue(org.hibernate.mapping.SortableValue) KeyValue(org.hibernate.mapping.KeyValue) ToOne(org.hibernate.mapping.ToOne) OneToOne(org.hibernate.mapping.OneToOne) ManyToOne(org.hibernate.mapping.ManyToOne) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) Component(org.hibernate.mapping.Component) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) CompositeType(org.hibernate.type.CompositeType)

Example 4 with SelectableMapping

use of org.hibernate.metamodel.mapping.SelectableMapping in project hibernate-orm by hibernate.

the class MappingModelCreationHelper method interpretToOneKeyDescriptor.

/**
 * Tries to {@link ToOneAttributeMapping#setForeignKeyDescriptor}
 * to the given attribute {@code attributeMapping}.
 *
 * @param attributeMapping The attribute for which we try to set the foreign key
 * @param bootProperty The property
 * @param bootValueMapping The value mapping
 * @param inversePropertyAccess Access to the inverse property
 * @param dialect Current dialect
 * @param creationProcess Current creation process
 * @return true if the foreign key is actually set
 */
public static boolean interpretToOneKeyDescriptor(ToOneAttributeMapping attributeMapping, Property bootProperty, ToOne bootValueMapping, PropertyAccess inversePropertyAccess, Dialect dialect, MappingModelCreationProcess creationProcess) {
    if (attributeMapping.getForeignKeyDescriptor() != null) {
        // already built/known
        return true;
    }
    final String tableName = getTableIdentifierExpression(bootValueMapping.getTable(), creationProcess);
    attributeMapping.setIdentifyingColumnsTableExpression(tableName);
    final EntityPersister referencedEntityDescriptor = creationProcess.getEntityPersister(bootValueMapping.getReferencedEntityName());
    String referencedPropertyName;
    boolean swapDirection = false;
    if (bootValueMapping instanceof OneToOne) {
        OneToOne oneToOne = (OneToOne) bootValueMapping;
        swapDirection = oneToOne.getForeignKeyType() == ForeignKeyDirection.TO_PARENT;
        referencedPropertyName = oneToOne.getMappedByProperty();
        if (referencedPropertyName == null) {
            referencedPropertyName = oneToOne.getReferencedPropertyName();
        }
    } else {
        referencedPropertyName = null;
    }
    if (referencedPropertyName != null) {
        if (referencedPropertyName.indexOf(".") > 0) {
            return interpretNestedToOneKeyDescriptor(referencedEntityDescriptor, referencedPropertyName, attributeMapping);
        }
        final ModelPart modelPart = referencedEntityDescriptor.findByPath(referencedPropertyName);
        if (modelPart instanceof ToOneAttributeMapping) {
            setReferencedAttributeForeignKeyDescriptor(attributeMapping, (ToOneAttributeMapping) modelPart, referencedEntityDescriptor, referencedPropertyName, dialect, creationProcess);
        } else if (modelPart instanceof EmbeddableValuedModelPart) {
            final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = buildEmbeddableForeignKeyDescriptor((EmbeddableValuedModelPart) modelPart, bootValueMapping, attributeMapping.getDeclaringType(), attributeMapping.findContainingEntityMapping(), true, dialect, creationProcess);
            attributeMapping.setForeignKeyDescriptor(embeddedForeignKeyDescriptor);
        } else if (modelPart == null) {
            throw new IllegalArgumentException("Unable to find attribute " + bootProperty.getPersistentClass().getEntityName() + " -> " + bootProperty.getName());
        } else {
            throw new NotYetImplementedFor6Exception("Support for foreign-keys based on `" + modelPart + "` not yet implemented: " + bootProperty.getPersistentClass().getEntityName() + " -> " + bootProperty.getName());
        }
        return true;
    }
    final ModelPart fkTarget;
    if (bootValueMapping.isReferenceToPrimaryKey()) {
        fkTarget = referencedEntityDescriptor.getIdentifierMapping();
    } else {
        fkTarget = referencedEntityDescriptor.findByPath(bootValueMapping.getReferencedPropertyName());
    }
    if (fkTarget instanceof BasicValuedModelPart) {
        final BasicValuedModelPart simpleFkTarget = (BasicValuedModelPart) fkTarget;
        final Iterator<Selectable> columnIterator = bootValueMapping.getColumnIterator();
        final Table table = bootValueMapping.getTable();
        final String tableExpression = getTableIdentifierExpression(table, creationProcess);
        final BasicValuedModelPart declaringKeyPart;
        final PropertyAccess declaringKeyPropertyAccess;
        if (inversePropertyAccess == null) {
            // So far, OneToOne mappings are only supported based on the owner's PK
            if (bootValueMapping instanceof OneToOne) {
                declaringKeyPart = simpleFkTarget;
                final EntityIdentifierMapping identifierMapping = attributeMapping.findContainingEntityMapping().getIdentifierMapping();
                declaringKeyPropertyAccess = ((PropertyBasedMapping) identifierMapping).getPropertyAccess();
            } else {
                declaringKeyPart = simpleFkTarget;
                // declaringKeyPropertyAccess = ( (PropertyBasedMapping) declaringKeyPart ).getPropertyAccess();
                declaringKeyPropertyAccess = new ChainedPropertyAccessImpl(attributeMapping.getPropertyAccess(), ((PropertyBasedMapping) declaringKeyPart).getPropertyAccess());
            }
        } else {
            declaringKeyPart = simpleFkTarget;
            declaringKeyPropertyAccess = new ChainedPropertyAccessImpl(inversePropertyAccess, ((PropertyBasedMapping) simpleFkTarget).getPropertyAccess());
        }
        final SelectableMapping keySelectableMapping;
        if (columnIterator.hasNext()) {
            keySelectableMapping = SelectableMappingImpl.from(tableExpression, columnIterator.next(), simpleFkTarget.getJdbcMapping(), dialect, creationProcess.getSqmFunctionRegistry());
        } else {
            // case of ToOne with @PrimaryKeyJoinColumn
            keySelectableMapping = SelectableMappingImpl.from(tableExpression, table.getColumn(0), simpleFkTarget.getJdbcMapping(), dialect, creationProcess.getSqmFunctionRegistry());
        }
        final ForeignKeyDescriptor foreignKeyDescriptor = new SimpleForeignKeyDescriptor(attributeMapping.getDeclaringType(), declaringKeyPart, declaringKeyPropertyAccess, keySelectableMapping, simpleFkTarget, bootValueMapping.isReferenceToPrimaryKey(), bootValueMapping.isConstrained(), swapDirection);
        attributeMapping.setForeignKeyDescriptor(foreignKeyDescriptor);
    } else if (fkTarget instanceof EmbeddableValuedModelPart) {
        final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = buildEmbeddableForeignKeyDescriptor((EmbeddableValuedModelPart) fkTarget, bootValueMapping, attributeMapping.getDeclaringType(), attributeMapping.findContainingEntityMapping(), swapDirection, dialect, creationProcess);
        attributeMapping.setForeignKeyDescriptor(embeddedForeignKeyDescriptor);
    } else {
        throw new NotYetImplementedFor6Exception("Support for " + fkTarget.getClass() + " foreign-keys not yet implemented: " + bootProperty.getPersistentClass().getEntityName() + " -> " + bootProperty.getName());
    }
    return true;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) Table(org.hibernate.mapping.Table) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) ChainedPropertyAccessImpl(org.hibernate.property.access.internal.ChainedPropertyAccessImpl) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess) PropertyBasedMapping(org.hibernate.metamodel.mapping.PropertyBasedMapping) OneToOne(org.hibernate.mapping.OneToOne) Selectable(org.hibernate.mapping.Selectable) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception)

Example 5 with SelectableMapping

use of org.hibernate.metamodel.mapping.SelectableMapping in project hibernate-orm by hibernate.

the class MappingModelCreationHelper method buildPluralAttributeMapping.

@SuppressWarnings("rawtypes")
public static PluralAttributeMapping buildPluralAttributeMapping(String attrName, int stateArrayPosition, Property bootProperty, ManagedMappingType declaringType, PropertyAccess propertyAccess, CascadeStyle cascadeStyle, FetchMode fetchMode, MappingModelCreationProcess creationProcess) {
    final Collection bootValueMapping = (Collection) bootProperty.getValue();
    final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
    final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
    final SqlStringGenerationContext sqlStringGenerationContext = sessionFactory.getSqlStringGenerationContext();
    final Dialect dialect = sqlStringGenerationContext.getDialect();
    final MappingMetamodel domainModel = creationContext.getDomainModel();
    final CollectionPersister collectionDescriptor = domainModel.findCollectionDescriptor(bootValueMapping.getRole());
    assert collectionDescriptor != null;
    final String tableExpression = ((Joinable) collectionDescriptor).getTableName();
    final String sqlAliasStem = SqlAliasStemHelper.INSTANCE.generateStemFromAttributeName(bootProperty.getName());
    final CollectionMappingType<?> collectionMappingType;
    final JavaTypeRegistry jtdRegistry = creationContext.getJavaTypeRegistry();
    final CollectionPart elementDescriptor = interpretElement(bootValueMapping, tableExpression, collectionDescriptor, sqlAliasStem, dialect, creationProcess);
    final CollectionPart indexDescriptor;
    CollectionIdentifierDescriptor identifierDescriptor = null;
    final CollectionSemantics<?, ?> collectionSemantics = collectionDescriptor.getCollectionSemantics();
    switch(collectionSemantics.getCollectionClassification()) {
        case ARRAY:
            {
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(Object[].class), StandardArraySemantics.INSTANCE);
                final BasicValue index = (BasicValue) ((IndexedCollection) bootValueMapping).getIndex();
                final SelectableMapping selectableMapping = SelectableMappingImpl.from(tableExpression, index.getSelectables().get(0), creationContext.getTypeConfiguration().getBasicTypeForJavaType(Integer.class), dialect, creationProcess.getSqmFunctionRegistry());
                indexDescriptor = new BasicValuedCollectionPart(collectionDescriptor, CollectionPart.Nature.INDEX, // no converter
                null, selectableMapping);
                break;
            }
        case BAG:
            {
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(java.util.Collection.class), StandardBagSemantics.INSTANCE);
                indexDescriptor = null;
                break;
            }
        case ID_BAG:
            {
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(java.util.Collection.class), StandardIdentifierBagSemantics.INSTANCE);
                indexDescriptor = null;
                assert collectionDescriptor instanceof SQLLoadableCollection;
                final SQLLoadableCollection loadableCollection = (SQLLoadableCollection) collectionDescriptor;
                final String identifierColumnName = loadableCollection.getIdentifierColumnName();
                assert identifierColumnName != null;
                identifierDescriptor = new CollectionIdentifierDescriptorImpl(collectionDescriptor, tableExpression, identifierColumnName, (BasicType) loadableCollection.getIdentifierType());
                break;
            }
        case LIST:
            {
                final BasicValue index = (BasicValue) ((IndexedCollection) bootValueMapping).getIndex();
                final SelectableMapping selectableMapping = SelectableMappingImpl.from(tableExpression, index.getSelectables().get(0), creationContext.getTypeConfiguration().getBasicTypeForJavaType(Integer.class), dialect, creationProcess.getSqmFunctionRegistry());
                indexDescriptor = new BasicValuedCollectionPart(collectionDescriptor, CollectionPart.Nature.INDEX, // no converter
                null, selectableMapping);
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(List.class), StandardListSemantics.INSTANCE);
                break;
            }
        case MAP:
        case ORDERED_MAP:
        case SORTED_MAP:
            {
                final Class<? extends java.util.Map> mapJavaType = collectionSemantics.getCollectionClassification() == CollectionClassification.SORTED_MAP ? SortedMap.class : java.util.Map.class;
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(mapJavaType), collectionSemantics);
                final String mapKeyTableExpression;
                if (bootValueMapping instanceof Map && ((Map) bootValueMapping).getMapKeyPropertyName() != null) {
                    mapKeyTableExpression = getTableIdentifierExpression(((Map) bootValueMapping).getIndex().getTable(), creationProcess);
                } else {
                    mapKeyTableExpression = tableExpression;
                }
                indexDescriptor = interpretMapKey(bootValueMapping, collectionDescriptor, mapKeyTableExpression, sqlAliasStem, dialect, creationProcess);
                break;
            }
        case SET:
        case ORDERED_SET:
        case SORTED_SET:
            {
                final Class<? extends java.util.Set> setJavaType = collectionSemantics.getCollectionClassification() == CollectionClassification.SORTED_MAP ? SortedSet.class : java.util.Set.class;
                collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(setJavaType), collectionSemantics);
                indexDescriptor = null;
                break;
            }
        default:
            {
                throw new MappingException("Unexpected CollectionClassification : " + collectionSemantics.getCollectionClassification());
            }
    }
    final StateArrayContributorMetadata contributorMetadata = new StateArrayContributorMetadata() {

        @Override
        public PropertyAccess getPropertyAccess() {
            return propertyAccess;
        }

        @Override
        public MutabilityPlan getMutabilityPlan() {
            return ImmutableMutabilityPlan.instance();
        }

        @Override
        public boolean isNullable() {
            return bootProperty.isOptional();
        }

        @Override
        public boolean isInsertable() {
            return bootProperty.isInsertable();
        }

        @Override
        public boolean isUpdatable() {
            return bootProperty.isUpdateable();
        }

        @Override
        public boolean isIncludedInDirtyChecking() {
            return false;
        }

        @Override
        public boolean isIncludedInOptimisticLocking() {
            return bootProperty.isOptimisticLocked();
        }

        @Override
        public CascadeStyle getCascadeStyle() {
            return cascadeStyle;
        }
    };
    final FetchStyle style = FetchOptionsHelper.determineFetchStyleByMetadata(fetchMode, collectionDescriptor.getCollectionType(), sessionFactory);
    final FetchTiming timing = FetchOptionsHelper.determineFetchTiming(style, collectionDescriptor.getCollectionType(), collectionDescriptor.isLazy(), collectionDescriptor.getRole(), sessionFactory);
    final PluralAttributeMappingImpl pluralAttributeMapping = new PluralAttributeMappingImpl(attrName, bootValueMapping, propertyAccess, entityMappingType -> contributorMetadata, collectionMappingType, stateArrayPosition, elementDescriptor, indexDescriptor, identifierDescriptor, timing, style, cascadeStyle, declaringType, collectionDescriptor);
    creationProcess.registerInitializationCallback("PluralAttributeMapping(" + bootValueMapping.getRole() + ")#finishInitialization", () -> {
        pluralAttributeMapping.finishInitialization(bootProperty, bootValueMapping, creationProcess);
        return true;
    });
    creationProcess.registerInitializationCallback("PluralAttributeMapping(" + bootValueMapping.getRole() + ") - key descriptor", () -> {
        interpretPluralAttributeMappingKeyDescriptor(pluralAttributeMapping, bootValueMapping, collectionDescriptor, declaringType, dialect, creationProcess);
        return true;
    });
    return pluralAttributeMapping;
}
Also used : SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) SortedSet(java.util.SortedSet) SortedSet(java.util.SortedSet) BasicValue(org.hibernate.mapping.BasicValue) MappingException(org.hibernate.MappingException) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) FetchStyle(org.hibernate.engine.FetchStyle) Dialect(org.hibernate.dialect.Dialect) List(java.util.List) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionIdentifierDescriptor(org.hibernate.metamodel.mapping.CollectionIdentifierDescriptor) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SQLLoadableCollection(org.hibernate.persister.collection.SQLLoadableCollection) RuntimeModelCreationContext(org.hibernate.metamodel.spi.RuntimeModelCreationContext) Joinable(org.hibernate.persister.entity.Joinable) SortedMap(java.util.SortedMap) FetchTiming(org.hibernate.engine.FetchTiming) Collection(org.hibernate.mapping.Collection) IndexedCollection(org.hibernate.mapping.IndexedCollection) SQLLoadableCollection(org.hibernate.persister.collection.SQLLoadableCollection) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) PersistentClass(org.hibernate.mapping.PersistentClass) IndexedCollection(org.hibernate.mapping.IndexedCollection) SortedMap(java.util.SortedMap) Map(org.hibernate.mapping.Map)

Aggregations

SelectableMapping (org.hibernate.metamodel.mapping.SelectableMapping)10 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)6 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)6 List (java.util.List)5 CollectionPart (org.hibernate.metamodel.mapping.CollectionPart)5 ModelPart (org.hibernate.metamodel.mapping.ModelPart)5 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 Collections (java.util.Collections)4 FetchTiming (org.hibernate.engine.FetchTiming)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)4 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)4 NavigablePath (org.hibernate.query.spi.NavigablePath)4 SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)4 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)4 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)4 JavaType (org.hibernate.type.descriptor.java.JavaType)4 BiConsumer (java.util.function.BiConsumer)3 FetchStyle (org.hibernate.engine.FetchStyle)3 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)3