use of org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping in project hibernate-orm by hibernate.
the class DefaultLoadEventListener method checkIdClass.
private void checkIdClass(final EntityPersister persister, final LoadEvent event, final LoadType loadType, final Class<?> idClass) {
// we may have the jpa requirement of allowing find-by-id where id is the "simple pk value" of a
// dependent objects parent. This is part of its generally goofy derived identity "feature"
final EntityIdentifierMapping idMapping = persister.getIdentifierMapping();
if (idMapping instanceof CompositeIdentifierMapping) {
final CompositeIdentifierMapping compositeIdMapping = (CompositeIdentifierMapping) idMapping;
final List<AttributeMapping> attributeMappings = compositeIdMapping.getPartMappingType().getAttributeMappings();
if (attributeMappings.size() == 1) {
final AttributeMapping singleIdAttribute = attributeMappings.get(0);
if (singleIdAttribute.getMappedType() instanceof EntityMappingType) {
final EntityMappingType parentIdTargetMapping = (EntityMappingType) singleIdAttribute.getMappedType();
final EntityIdentifierMapping parentIdTargetIdMapping = parentIdTargetMapping.getIdentifierMapping();
final MappingType parentIdType = parentIdTargetIdMapping instanceof CompositeIdentifierMapping ? ((CompositeIdentifierMapping) parentIdTargetIdMapping).getMappedIdEmbeddableTypeDescriptor() : parentIdTargetIdMapping.getMappedType();
if (parentIdType.getMappedJavaType().getJavaTypeClass().isInstance(event.getEntityId())) {
// yep that's what we have...
loadByDerivedIdentitySimplePkValue(event, loadType, persister, compositeIdMapping, (EntityPersister) parentIdTargetMapping);
return;
}
}
} else if (idMapping instanceof NonAggregatedIdentifierMapping) {
if (idClass.isInstance(event.getEntityId())) {
return;
}
}
}
throw new TypeMismatchException("Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass());
}
use of org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping in project hibernate-orm by hibernate.
the class AbstractEntityPersister method getPropertyValue.
@Override
public Object getPropertyValue(Object object, String propertyName) {
final int dotIndex = propertyName.indexOf('.');
final String basePropertyName = dotIndex == -1 ? propertyName : propertyName.substring(0, dotIndex);
final AttributeMapping attributeMapping = findAttributeMapping(basePropertyName);
ManagedMappingType baseValueType = null;
Object baseValue = null;
if (attributeMapping != null) {
baseValue = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
if (dotIndex != -1) {
baseValueType = (ManagedMappingType) attributeMapping.getMappedType();
}
} else if (identifierMapping instanceof NonAggregatedIdentifierMapping) {
final EmbeddedAttributeMapping embeddedAttributeMapping = (EmbeddedAttributeMapping) findAttributeMapping(NavigableRole.IDENTIFIER_MAPPER_PROPERTY);
final AttributeMapping mapping = embeddedAttributeMapping.getMappedType().findAttributeMapping(basePropertyName);
if (mapping != null) {
baseValue = mapping.getAttributeMetadataAccess().resolveAttributeMetadata(this).getPropertyAccess().getGetter().get(object);
if (dotIndex != -1) {
baseValueType = (ManagedMappingType) mapping.getMappedType();
}
}
}
return getPropertyValue(baseValue, baseValueType, propertyName, dotIndex);
}
use of org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping in project hibernate-orm by hibernate.
the class NonAggregatedCompositeValuedPathInterpretation method from.
public static <T> NonAggregatedCompositeValuedPathInterpretation<T> from(NonAggregatedCompositeSimplePath<T> sqmPath, SqmToSqlAstConverter converter, SqmToSqlAstConverter sqlAstCreationState) {
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().findTableGroup(sqmPath.getLhs().getNavigablePath());
final NonAggregatedIdentifierMapping mapping = (NonAggregatedIdentifierMapping) tableGroup.getModelPart().findSubPart(sqmPath.getReferencedPathSource().getPathName(), null);
return new NonAggregatedCompositeValuedPathInterpretation<>(mapping.toSqlExpression(tableGroup, converter.getCurrentClauseStack().getCurrent(), converter, converter), sqmPath.getNavigablePath(), mapping, tableGroup);
}
use of org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping in project hibernate-orm by hibernate.
the class DomainResultCreationStateImpl method visitFetches.
@Override
public List<Fetch> visitFetches(FetchParent fetchParent) {
final FetchableContainer fetchableContainer = fetchParent.getReferencedMappingContainer();
final List<Fetch> fetches = CollectionHelper.arrayList(fetchableContainer.getNumberOfFetchables());
final Consumer<Fetchable> fetchableConsumer = fetchable -> {
final String fetchableName = fetchable.getFetchableName();
Map.Entry<String, NavigablePath> currentEntry;
if (relativePathStack.isEmpty()) {
currentEntry = new AbstractMap.SimpleEntry<>(getRelativePath("", fetchable, fetchableContainer), new NavigablePath(fetchableName));
} else {
final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
final String key = oldEntry.getKey();
currentEntry = new AbstractMap.SimpleEntry<>(getRelativePath(key, fetchable, fetchableContainer), oldEntry.getValue().append(fetchableName));
}
// todo (6.0): figure out if we can somehow create the navigable paths in a better way
final String fullPath = currentEntry.getKey();
FetchBuilder explicitFetchBuilder = fetchBuilderResolverStack.getCurrent().apply(fullPath);
DynamicFetchBuilderLegacy fetchBuilderLegacy;
if (explicitFetchBuilder == null) {
fetchBuilderLegacy = legacyFetchResolver.resolve(fromClauseAccess.findTableGroup(fetchParent.getNavigablePath()).getPrimaryTableReference().getIdentificationVariable(), fetchableName);
} else {
fetchBuilderLegacy = null;
}
if (fetchable instanceof Association && fetchable.getMappedFetchOptions().getTiming() == FetchTiming.DELAYED) {
final Association association = (Association) fetchable;
final ForeignKeyDescriptor foreignKeyDescriptor = association.getForeignKeyDescriptor();
final String partName = attributeName(foreignKeyDescriptor.getSide(association.getSideNature().inverse()).getModelPart());
// If there are no fetch builders for this association, we only want to fetch the FK
if (explicitFetchBuilder == null && fetchBuilderLegacy == null && partName != null) {
currentEntry = new AbstractMap.SimpleEntry<>(currentEntry.getKey() + "." + partName, currentEntry.getValue().append(partName));
explicitFetchBuilder = fetchBuilderResolverStack.getCurrent().apply(currentEntry.getKey());
if (explicitFetchBuilder == null) {
fetchBuilderLegacy = legacyFetchResolver.resolve(fromClauseAccess.findTableGroup(fetchParent.getNavigablePath()).getPrimaryTableReference().getIdentificationVariable(), fetchableName);
}
}
}
relativePathStack.push(currentEntry);
try {
final NavigablePath fetchPath = fetchParent.resolveNavigablePath(fetchable);
final FetchBuilder fetchBuilder;
if (explicitFetchBuilder != null) {
fetchBuilder = explicitFetchBuilder;
} else {
if (fetchBuilderLegacy == null) {
fetchBuilder = Builders.implicitFetchBuilder(fetchPath, fetchable, this);
} else {
fetchBuilder = fetchBuilderLegacy;
}
}
final Fetch fetch = fetchBuilder.buildFetch(fetchParent, fetchPath, jdbcResultsMetadata, (s, s2) -> {
throw new UnsupportedOperationException();
}, this);
fetches.add(fetch);
} finally {
relativePathStack.pop();
}
};
boolean previous = this.processingKeyFetches;
this.processingKeyFetches = true;
if (fetchableContainer instanceof EntityValuedModelPart) {
final EntityValuedModelPart entityValuedFetchable = (EntityValuedModelPart) fetchableContainer;
final EntityIdentifierMapping identifierMapping = entityValuedFetchable.getEntityMappingType().getIdentifierMapping();
final boolean idClass = identifierMapping instanceof NonAggregatedIdentifierMapping;
final String identifierAttributeName = attributeName(identifierMapping);
if (idClass) {
final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
relativePathStack.push(new AbstractMap.SimpleEntry<>(oldEntry == null ? "" : oldEntry.getKey(), new EntityIdentifierNavigablePath(oldEntry == null ? fetchParent.getNavigablePath() : oldEntry.getValue(), identifierAttributeName)));
} else if (identifierMapping instanceof CompositeIdentifierMapping) {
final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
relativePathStack.push(new AbstractMap.SimpleEntry<>(oldEntry == null ? identifierAttributeName : oldEntry.getKey() + "." + identifierAttributeName, new EntityIdentifierNavigablePath(oldEntry == null ? fetchParent.getNavigablePath() : oldEntry.getValue(), identifierAttributeName)));
}
try {
if (identifierMapping instanceof FetchableContainer) {
// essentially means the entity has a composite id - ask the embeddable to visit its fetchables
((FetchableContainer) identifierMapping).visitFetchables(fetchableConsumer, null);
} else {
fetchableConsumer.accept((Fetchable) identifierMapping);
}
} finally {
this.processingKeyFetches = previous;
if (idClass) {
this.relativePathStack.pop();
}
}
}
fetchableContainer.visitKeyFetchables(fetchableConsumer, null);
fetchableContainer.visitFetchables(fetchableConsumer, null);
return fetches;
}
Aggregations