Search in sources :

Example 1 with PersistentAttribute

use of org.hibernate.metamodel.model.domain.PersistentAttribute in project hibernate-orm by hibernate.

the class JpaMetamodelImpl method processJpa.

public void processJpa(MetadataImplementor bootMetamodel, MappingMetamodel mappingMetamodel, Map<Class<?>, String> entityProxyInterfaceMap, JpaStaticMetaModelPopulationSetting jpaStaticMetaModelPopulationSetting, JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting, Collection<NamedEntityGraphDefinition> namedEntityGraphDefinitions, RuntimeModelCreationContext runtimeModelCreationContext) {
    bootMetamodel.getImports().forEach((k, v) -> this.nameToImportMap.put(k, new ImportInfo<>(v, null)));
    this.entityProxyInterfaceMap.putAll(entityProxyInterfaceMap);
    final MetadataContext context = new MetadataContext(this, mappingMetamodel, bootMetamodel, jpaStaticMetaModelPopulationSetting, jpaMetaModelPopulationSetting, runtimeModelCreationContext);
    for (PersistentClass entityBinding : bootMetamodel.getEntityBindings()) {
        locateOrBuildEntityType(entityBinding, context, typeConfiguration);
    }
    handleUnusedMappedSuperclasses(context, typeConfiguration);
    context.wrapUp();
    for (Map.Entry<String, IdentifiableDomainType<?>> entry : context.getIdentifiableTypesByName().entrySet()) {
        if (entry.getValue() instanceof EntityDomainType<?>) {
            this.jpaEntityTypeMap.put(entry.getKey(), (EntityDomainType<?>) entry.getValue());
        }
    }
    this.jpaManagedTypeMap.putAll(context.getEntityTypeMap());
    this.jpaManagedTypeMap.putAll(context.getMappedSuperclassTypeMap());
    switch(jpaMetaModelPopulationSetting) {
        case IGNORE_UNSUPPORTED:
            this.jpaManagedTypes.addAll(context.getEntityTypeMap().values());
            this.jpaManagedTypes.addAll(context.getMappedSuperclassTypeMap().values());
            break;
        case ENABLED:
            this.jpaManagedTypes.addAll(context.getIdentifiableTypesByName().values());
            break;
    }
    for (EmbeddableDomainType<?> embeddable : context.getEmbeddableTypeSet()) {
        switch(jpaMetaModelPopulationSetting) {
            case IGNORE_UNSUPPORTED:
                if (embeddable.getJavaType() != null && embeddable.getJavaType() != Map.class) {
                    this.jpaEmbeddables.add(embeddable);
                    this.jpaManagedTypes.add(embeddable);
                    if (!(embeddable.getExpressibleJavaType() instanceof EntityJavaType<?>)) {
                        this.jpaManagedTypeMap.put(embeddable.getJavaType(), embeddable);
                    }
                }
                break;
            case ENABLED:
                this.jpaEmbeddables.add(embeddable);
                this.jpaManagedTypes.add(embeddable);
                if (embeddable.getJavaType() != null && !(embeddable.getExpressibleJavaType() instanceof EntityJavaType<?>)) {
                    this.jpaManagedTypeMap.put(embeddable.getJavaType(), embeddable);
                }
                break;
            case DISABLED:
                if (embeddable.getJavaType() == null) {
                    throw new UnsupportedOperationException("ANY not supported");
                }
                if (!(embeddable.getExpressibleJavaType() instanceof EntityJavaType<?>)) {
                    this.jpaManagedTypeMap.put(embeddable.getJavaType(), embeddable);
                }
                break;
        }
    }
    final Consumer<PersistentAttribute<?, ?>> attributeConsumer = persistentAttribute -> {
        if (persistentAttribute.getJavaType() != null && persistentAttribute.getJavaType().isEnum()) {
            @SuppressWarnings("unchecked") final Class<Enum<?>> enumClass = (Class<Enum<?>>) persistentAttribute.getJavaType();
            final Enum<?>[] enumConstants = enumClass.getEnumConstants();
            for (Enum<?> enumConstant : enumConstants) {
                final String qualifiedEnumLiteral = enumConstant.getDeclaringClass().getSimpleName() + "." + enumConstant.name();
                this.allowedEnumLiteralTexts.computeIfAbsent(enumConstant.name(), k -> new HashMap<>()).put(enumClass, enumConstant);
                this.allowedEnumLiteralTexts.computeIfAbsent(qualifiedEnumLiteral, k -> new HashMap<>()).put(enumClass, enumConstant);
            }
        }
    };
    domainTypeStream(context).forEach(managedDomainType -> managedDomainType.visitAttributes(attributeConsumer));
    applyNamedEntityGraphs(namedEntityGraphDefinitions);
}
Also used : JpaStaticMetaModelPopulationSetting(org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting) EntityType(jakarta.persistence.metamodel.EntityType) Attribute(jakarta.persistence.metamodel.Attribute) ManagedDomainType(org.hibernate.metamodel.model.domain.ManagedDomainType) IdentifiableDomainType(org.hibernate.metamodel.model.domain.IdentifiableDomainType) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) PersistentClass(org.hibernate.mapping.PersistentClass) Map(java.util.Map) SqmPolymorphicRootDescriptor(org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) HEMLogging(org.hibernate.internal.HEMLogging) DynamicModelJavaType(org.hibernate.type.descriptor.java.spi.DynamicModelJavaType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) Collection(java.util.Collection) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) JpaMetaModelPopulationSetting(org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NamedAttributeNode(jakarta.persistence.NamedAttributeNode) StringHelper(org.hibernate.internal.util.StringHelper) Set(java.util.Set) MetadataContext(org.hibernate.metamodel.internal.MetadataContext) EntityGraph(jakarta.persistence.EntityGraph) EntityManagerMessageLogger(org.hibernate.internal.EntityManagerMessageLogger) Serializable(java.io.Serializable) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) List(java.util.List) AttributeNodeImplementor(org.hibernate.graph.spi.AttributeNodeImplementor) Stream(java.util.stream.Stream) RuntimeModelCreationContext(org.hibernate.metamodel.spi.RuntimeModelCreationContext) JpaMetamodelImplementor(org.hibernate.metamodel.model.domain.spi.JpaMetamodelImplementor) EmbeddableType(jakarta.persistence.metamodel.EmbeddableType) NamedEntityGraphDefinition(org.hibernate.cfg.annotations.NamedEntityGraphDefinition) Queryable(org.hibernate.persister.entity.Queryable) RootGraphImpl(org.hibernate.graph.internal.RootGraphImpl) JavaType(org.hibernate.type.descriptor.java.JavaType) HashMap(java.util.HashMap) NamedSubgraph(jakarta.persistence.NamedSubgraph) MappedSuperclassDomainType(org.hibernate.metamodel.model.domain.MappedSuperclassDomainType) ArrayList(java.util.ArrayList) RootGraphImplementor(org.hibernate.graph.spi.RootGraphImplementor) HashSet(java.util.HashSet) NamedEntityGraph(jakarta.persistence.NamedEntityGraph) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) PersistentAttribute(org.hibernate.metamodel.model.domain.PersistentAttribute) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) MappedSuperclass(org.hibernate.mapping.MappedSuperclass) SubGraphImplementor(org.hibernate.graph.spi.SubGraphImplementor) ManagedType(jakarta.persistence.metamodel.ManagedType) EmbeddableDomainType(org.hibernate.metamodel.model.domain.EmbeddableDomainType) Consumer(java.util.function.Consumer) ObjectStreamException(java.io.ObjectStreamException) GraphImplementor(org.hibernate.graph.spi.GraphImplementor) TreeMap(java.util.TreeMap) JpaCompliance(org.hibernate.jpa.spi.JpaCompliance) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) EntityJavaType(org.hibernate.type.descriptor.java.spi.EntityJavaType) EntityJavaType(org.hibernate.type.descriptor.java.spi.EntityJavaType) IdentifiableDomainType(org.hibernate.metamodel.model.domain.IdentifiableDomainType) PersistentAttribute(org.hibernate.metamodel.model.domain.PersistentAttribute) PersistentClass(org.hibernate.mapping.PersistentClass) MetadataContext(org.hibernate.metamodel.internal.MetadataContext) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 2 with PersistentAttribute

use of org.hibernate.metamodel.model.domain.PersistentAttribute in project hibernate-orm by hibernate.

the class MetadataContext method wrapUp.

@SuppressWarnings("unchecked")
public void wrapUp() {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Wrapping up metadata context...");
    }
    boolean staticMetamodelScanEnabled = this.jpaStaticMetaModelPopulationSetting != JpaStaticMetaModelPopulationSetting.DISABLED;
    // 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 (LOG.isTraceEnabled()) {
                LOG.trace("Starting entity [" + safeMapping.getEntityName() + ']');
            }
            try {
                final EntityDomainType<Object> jpaMapping = (EntityDomainType<Object>) entityTypesByPersistentClass.get(safeMapping);
                applyIdMetadata(safeMapping, jpaMapping);
                applyVersionAttribute(safeMapping, jpaMapping);
                for (Property property : safeMapping.getDeclaredProperties()) {
                    if (property.getValue() == safeMapping.getIdentifierMapper()) {
                        // #buildIdClassAttributes
                        continue;
                    }
                    if (safeMapping.isVersioned() && property == safeMapping.getVersion()) {
                        // skip the version property, it was already handled previously.
                        continue;
                    }
                    final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute(jpaMapping, property);
                    if (attribute != null) {
                        addAttribute(jpaMapping, attribute);
                        if (property.isNaturalIdentifier()) {
                            ((AttributeContainer<Object>) jpaMapping).getInFlightAccess().applyNaturalIdAttribute(attribute);
                        }
                    }
                }
                ((AttributeContainer<?>) jpaMapping).getInFlightAccess().finishUp();
                if (staticMetamodelScanEnabled) {
                    populateStaticMetamodel(jpaMapping);
                }
            } finally {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Completed entity [" + safeMapping.getEntityName() + ']');
                }
            }
        } else if (MappedSuperclass.class.isAssignableFrom(mapping.getClass())) {
            final MappedSuperclass safeMapping = (MappedSuperclass) mapping;
            if (LOG.isTraceEnabled()) {
                LOG.trace("Starting mapped superclass [" + safeMapping.getMappedClass().getName() + ']');
            }
            try {
                final MappedSuperclassDomainType<Object> jpaType = (MappedSuperclassDomainType<Object>) mappedSuperclassByMappedSuperclassMapping.get(safeMapping);
                applyIdMetadata(safeMapping, jpaType);
                applyVersionAttribute(safeMapping, jpaType);
                for (Property property : safeMapping.getDeclaredProperties()) {
                    if (safeMapping.isVersioned() && property == safeMapping.getVersion()) {
                        // skip the version property, it was already handled previously.
                        continue;
                    }
                    final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute(jpaType, property);
                    if (attribute != null) {
                        addAttribute(jpaType, attribute);
                        if (property.isNaturalIdentifier()) {
                            ((AttributeContainer<Object>) jpaType).getInFlightAccess().applyNaturalIdAttribute(attribute);
                        }
                    }
                }
                ((AttributeContainer<?>) jpaType).getInFlightAccess().finishUp();
                if (staticMetamodelScanEnabled) {
                    populateStaticMetamodel(jpaType);
                }
            } finally {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Completed mapped superclass [" + safeMapping.getMappedClass().getName() + ']');
                }
            }
        } else {
            throw new AssertionFailure("Unexpected mapping type: " + mapping.getClass());
        }
    }
    while (!embeddablesToProcess.isEmpty()) {
        final ArrayList<EmbeddableDomainType<?>> processingEmbeddables = new ArrayList<>(embeddablesToProcess.size());
        for (List<EmbeddableDomainType<?>> embeddableDomainTypes : embeddablesToProcess.values()) {
            processingEmbeddables.addAll(embeddableDomainTypes);
        }
        embeddablesToProcess.clear();
        for (EmbeddableDomainType<?> embeddable : processingEmbeddables) {
            final Component component = componentByEmbeddable.get(embeddable);
            for (Property property : component.getProperties()) {
                final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute((ManagedDomainType<Object>) embeddable, property);
                if (attribute != null) {
                    addAttribute(embeddable, attribute);
                }
            }
            ((AttributeContainer<?>) embeddable).getInFlightAccess().finishUp();
            embeddables.put(embeddable.getJavaType(), embeddable);
            if (staticMetamodelScanEnabled) {
                populateStaticMetamodel(embeddable);
            }
        }
    }
}
Also used : EmbeddableDomainType(org.hibernate.metamodel.model.domain.EmbeddableDomainType) AssertionFailure(org.hibernate.AssertionFailure) ArrayList(java.util.ArrayList) AttributeContainer(org.hibernate.metamodel.model.domain.internal.AttributeContainer) MappedSuperclassDomainType(org.hibernate.metamodel.model.domain.MappedSuperclassDomainType) PersistentAttribute(org.hibernate.metamodel.model.domain.PersistentAttribute) SingularPersistentAttribute(org.hibernate.metamodel.model.domain.SingularPersistentAttribute) MappedSuperclass(org.hibernate.mapping.MappedSuperclass) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

ArrayList (java.util.ArrayList)2 MappedSuperclass (org.hibernate.mapping.MappedSuperclass)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 EmbeddableDomainType (org.hibernate.metamodel.model.domain.EmbeddableDomainType)2 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)2 MappedSuperclassDomainType (org.hibernate.metamodel.model.domain.MappedSuperclassDomainType)2 PersistentAttribute (org.hibernate.metamodel.model.domain.PersistentAttribute)2 EntityGraph (jakarta.persistence.EntityGraph)1 NamedAttributeNode (jakarta.persistence.NamedAttributeNode)1 NamedEntityGraph (jakarta.persistence.NamedEntityGraph)1 NamedSubgraph (jakarta.persistence.NamedSubgraph)1 Attribute (jakarta.persistence.metamodel.Attribute)1 EmbeddableType (jakarta.persistence.metamodel.EmbeddableType)1 EntityType (jakarta.persistence.metamodel.EntityType)1 ManagedType (jakarta.persistence.metamodel.ManagedType)1 ObjectStreamException (java.io.ObjectStreamException)1 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1