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() + "'");
}
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);
}
Aggregations