Search in sources :

Example 1 with IdentifiableDomainType

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

the class SemanticQueryBuilder method visitEntityNaturalIdReference.

@Override
public SqmPath<?> visitEntityNaturalIdReference(HqlParser.EntityNaturalIdReferenceContext ctx) {
    final SqmPath<Object> sqmPath = consumeDomainPath((HqlParser.PathContext) ctx.getChild(2));
    final DomainType<?> sqmPathType = sqmPath.getReferencedPathSource().getSqmPathType();
    if (sqmPathType instanceof IdentifiableDomainType<?>) {
        @SuppressWarnings("unchecked") final IdentifiableDomainType<Object> identifiableType = (IdentifiableDomainType<? super Object>) sqmPathType;
        final List<? extends PersistentAttribute<Object, ?>> attributes = identifiableType.findNaturalIdAttributes();
        if (attributes == null) {
            throw new SemanticException(String.format("Path '%s' resolved to entity type '%s' which does not define a natural id", sqmPath.getNavigablePath().getFullPath(), identifiableType.getTypeName()));
        } else if (attributes.size() > 1) {
            throw new SemanticException(String.format("Path '%s' resolved to entity type '%s' which defines multiple natural ids", sqmPath.getNavigablePath().getFullPath(), identifiableType.getTypeName()));
        }
        @SuppressWarnings("unchecked") SingularAttribute<Object, ?> naturalIdAttribute = (SingularAttribute<Object, ?>) attributes.get(0);
        return sqmPath.get(naturalIdAttribute);
    }
    throw new SemanticException("Path does not resolve to an entity type '" + sqmPath.getNavigablePath().getFullPath() + "'");
}
Also used : SingularAttribute(jakarta.persistence.metamodel.SingularAttribute) IdentifiableDomainType(org.hibernate.metamodel.model.domain.IdentifiableDomainType) HqlParser(org.hibernate.grammars.hql.HqlParser) SemanticException(org.hibernate.query.SemanticException)

Example 2 with IdentifiableDomainType

use of org.hibernate.metamodel.model.domain.IdentifiableDomainType 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)

Aggregations

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 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)1 ObjectStreamException (java.io.ObjectStreamException)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1