use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class StandardEntityGraphTraversalStateImpl method traverse.
@Override
public TraversalResult traverse(FetchParent fetchParent, Fetchable fetchable, boolean exploreKeySubgraph) {
assert !(fetchable instanceof CollectionPart);
final GraphImplementor previousContextRoot = currentGraphContext;
AttributeNodeImplementor attributeNode = null;
if (appliesTo(fetchParent)) {
attributeNode = currentGraphContext.findAttributeNode(fetchable.getFetchableName());
}
currentGraphContext = null;
FetchTiming fetchTiming = null;
boolean joined = false;
if (attributeNode != null) {
fetchTiming = FetchTiming.IMMEDIATE;
joined = true;
final Map<Class<?>, SubGraphImplementor> subgraphMap;
final Class<?> subgraphMapKey;
if (fetchable instanceof PluralAttributeMapping) {
PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable;
assert exploreKeySubgraph && isJpaMapCollectionType(pluralAttributeMapping) || !exploreKeySubgraph && !isJpaMapCollectionType(pluralAttributeMapping);
if (exploreKeySubgraph) {
subgraphMap = attributeNode.getKeySubGraphMap();
subgraphMapKey = getEntityCollectionPartJavaClass(pluralAttributeMapping.getIndexDescriptor());
} else {
subgraphMap = attributeNode.getSubGraphMap();
subgraphMapKey = getEntityCollectionPartJavaClass(pluralAttributeMapping.getElementDescriptor());
}
} else {
assert !exploreKeySubgraph;
subgraphMap = attributeNode.getSubGraphMap();
subgraphMapKey = fetchable.getJavaType().getJavaTypeClass();
}
if (subgraphMap != null && subgraphMapKey != null) {
currentGraphContext = subgraphMap.get(subgraphMapKey);
}
}
if (fetchTiming == null) {
if (graphSemantic == GraphSemantic.FETCH) {
fetchTiming = FetchTiming.DELAYED;
joined = false;
} else {
fetchTiming = fetchable.getMappedFetchOptions().getTiming();
joined = fetchable.getMappedFetchOptions().getStyle() == FetchStyle.JOIN;
}
}
return new TraversalResult(previousContextRoot, fetchTiming, joined);
}
use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class FetchMementoHbmStandard method resolve.
@Override
public FetchBuilder resolve(Parent parent, Consumer<String> querySpaceConsumer, ResultSetMappingResolutionContext context) {
final Map<String, FetchBuilder> fetchBuilderMap = new HashMap<>();
fetchMementoMap.forEach((attrName, fetchMemento) -> fetchBuilderMap.put(attrName, fetchMemento.resolve(this, querySpaceConsumer, context)));
final DynamicResultBuilderEntityStandard resultBuilder;
if (fetchable instanceof PluralAttributeMapping) {
resultBuilder = new DynamicResultBuilderEntityStandard((EntityMappingType) ((PluralAttributeMapping) fetchable).getElementDescriptor().getPartMappingType(), tableAlias, navigablePath);
FetchBuilder element = fetchBuilderMap.get("element");
if (element != null) {
if (element instanceof DynamicFetchBuilder) {
resultBuilder.addIdColumnAliases(((DynamicFetchBuilder) element).getColumnAliases().toArray(new String[0]));
} else {
resultBuilder.addIdColumnAliases(((CompleteFetchBuilderEntityValuedModelPart) element).getColumnAliases().toArray(new String[0]));
}
}
FetchBuilder index = fetchBuilderMap.get("index");
if (index != null) {
resultBuilder.addFetchBuilder(CollectionPart.Nature.INDEX.getName(), index);
}
for (Map.Entry<String, FetchBuilder> entry : fetchBuilderMap.entrySet()) {
if (entry.getKey().startsWith(ELEMENT_PREFIX)) {
resultBuilder.addFetchBuilder(entry.getKey().substring(ELEMENT_PREFIX.length()), entry.getValue());
}
}
} else {
resultBuilder = new DynamicResultBuilderEntityStandard(((ToOneAttributeMapping) fetchable).getEntityMappingType(), tableAlias, navigablePath);
fetchBuilderMap.forEach(resultBuilder::addFetchBuilder);
}
return new DynamicFetchBuilderLegacy(tableAlias, ownerTableAlias, fetchable.getFetchableName(), keyColumnNames, fetchBuilderMap, resultBuilder);
}
use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class AbstractCollectionPersister method selectFragment.
/**
* Generate a list of collection index, key and element columns
*/
@Override
public String selectFragment(String alias, String columnSuffix) {
final PluralAttributeMapping attributeMapping = getAttributeMapping();
final QuerySpec rootQuerySpec = new QuerySpec(true);
final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), LockOptions.NONE, (fetchParent, querySpec, creationState) -> new ArrayList<>(), true, getFactory());
final NavigablePath entityPath = new NavigablePath(attributeMapping.getRootPathName());
final TableGroup rootTableGroup = attributeMapping.createRootTableGroup(true, entityPath, null, () -> p -> {
}, new SqlAliasBaseConstant(alias), sqlAstCreationState.getSqlExpressionResolver(), sqlAstCreationState.getFromClauseAccess(), getFactory());
rootQuerySpec.getFromClause().addRoot(rootTableGroup);
sqlAstCreationState.getFromClauseAccess().registerTableGroup(entityPath, rootTableGroup);
attributeMapping.createDomainResult(entityPath, rootTableGroup, null, sqlAstCreationState);
// Wrap expressions with aliases
final SelectClause selectClause = rootQuerySpec.getSelectClause();
final java.util.List<SqlSelection> sqlSelections = selectClause.getSqlSelections();
int i = 0;
for (String keyAlias : keyColumnAliases) {
sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), keyAlias + columnSuffix)));
i++;
}
if (hasIndex) {
for (String indexAlias : indexColumnAliases) {
sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), indexAlias + columnSuffix)));
i++;
}
}
if (hasIdentifier) {
sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), identifierColumnAlias + columnSuffix)));
i++;
}
for (int columnIndex = 0; i < sqlSelections.size(); i++, columnIndex++) {
final SqlSelection sqlSelection = sqlSelections.get(i);
sqlSelections.set(i, new SqlSelectionImpl(sqlSelection.getValuesArrayPosition(), sqlSelection.getJdbcResultSetIndex(), new AliasedExpression(sqlSelection.getExpression(), elementColumnAliases[columnIndex] + columnSuffix)));
}
final String sql = getFactory().getJdbcServices().getDialect().getSqlAstTranslatorFactory().buildSelectTranslator(getFactory(), new SelectStatement(rootQuerySpec)).translate(null, QueryOptions.NONE).getSql();
final int fromIndex = sql.lastIndexOf(" from");
final String expression;
if (fromIndex != -1) {
expression = sql.substring("select ".length(), fromIndex);
} else {
expression = sql.substring("select ".length());
}
return expression;
}
use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class ColumnReference method getTableReference.
TableReference getTableReference(TableGroup tableGroup) {
ModelPartContainer modelPart = tableGroup.getModelPart();
if (modelPart instanceof PluralAttributeMapping) {
final PluralAttributeMapping pluralAttribute = (PluralAttributeMapping) modelPart;
if (!pluralAttribute.getCollectionDescriptor().hasManyToManyOrdering()) {
return tableGroup.getPrimaryTableReference();
}
final MappingType elementMappingType = pluralAttribute.getElementDescriptor().getPartMappingType();
if (elementMappingType instanceof AbstractEntityPersister) {
final AbstractEntityPersister abstractEntityPersister = (AbstractEntityPersister) elementMappingType;
final int tableNumber = abstractEntityPersister.determineTableNumberForColumn(columnExpression);
final String tableName = abstractEntityPersister.getTableName(tableNumber);
return tableGroup.getTableReference(tableGroup.getNavigablePath(), tableName);
} else {
return tableGroup.getPrimaryTableReference();
}
}
return null;
}
use of org.hibernate.metamodel.mapping.PluralAttributeMapping in project hibernate-orm by hibernate.
the class ResultsHelper method finalizeCollectionLoading.
public static void finalizeCollectionLoading(PersistenceContext persistenceContext, CollectionPersister collectionDescriptor, PersistentCollection<?> collectionInstance, Object key, boolean hasNoQueuedAdds) {
CollectionEntry collectionEntry = persistenceContext.getCollectionEntry(collectionInstance);
if (collectionEntry == null) {
collectionEntry = persistenceContext.addInitializedCollection(collectionDescriptor, collectionInstance, key);
} else {
collectionEntry.postInitialize(collectionInstance);
}
if (collectionDescriptor.getCollectionType().hasHolder()) {
// in case of PersistentArrayHolder we have to realign the EntityEntry loaded state with
// the entity values
final Object owner = collectionInstance.getOwner();
final EntityEntry entry = persistenceContext.getEntry(owner);
final PluralAttributeMapping mapping = collectionDescriptor.getAttributeMapping();
final int propertyIndex = mapping.getStateArrayPosition();
final Object[] loadedState = entry.getLoadedState();
loadedState[propertyIndex] = mapping.getValue(owner);
persistenceContext.addCollectionHolder(collectionInstance);
}
final BatchFetchQueue batchFetchQueue = persistenceContext.getBatchFetchQueue();
batchFetchQueue.removeBatchLoadableCollection(collectionEntry);
final SharedSessionContractImplementor session = persistenceContext.getSession();
// add to cache if:
final boolean addToCache = // there were no queued additions
hasNoQueuedAdds && // and the role has a cache
collectionDescriptor.hasCache() && // and this is not a forced initialization during flush
session.getCacheMode().isPutEnabled() && !collectionEntry.isDoremove();
if (addToCache) {
addCollectionToCache(persistenceContext, collectionDescriptor, collectionInstance, key);
}
if (LOG.isDebugEnabled()) {
LOG.debugf("Collection fully initialized: %s", MessageHelper.collectionInfoString(collectionDescriptor, collectionInstance, key, session));
}
final StatisticsImplementor statistics = session.getFactory().getStatistics();
if (statistics.isStatisticsEnabled()) {
statistics.loadCollection(collectionDescriptor.getRole());
}
// todo (6.0) : there is other logic still needing to be implemented here. caching, etc
// see org.hibernate.engine.loading.internal.CollectionLoadContext#endLoadingCollection in 5.x
}
Aggregations