Search in sources :

Example 1 with IdentitySet

use of org.hibernate.internal.util.collections.IdentitySet in project hibernate-orm by hibernate.

the class UnresolvedEntityInsertActions method resolveDependentActions.

/**
	 * Resolve any dependencies on {@code managedEntity}.
	 *
	 * @param managedEntity - the managed entity name
	 * @param session - the session
	 *
	 * @return the insert actions that depended only on the specified entity.
	 *
	 * @throws IllegalArgumentException if {@code managedEntity} did not have managed or read-only status.
	 */
@SuppressWarnings({ "unchecked" })
public Set<AbstractEntityInsertAction> resolveDependentActions(Object managedEntity, SessionImplementor session) {
    final EntityEntry entityEntry = session.getPersistenceContext().getEntry(managedEntity);
    if (entityEntry.getStatus() != Status.MANAGED && entityEntry.getStatus() != Status.READ_ONLY) {
        throw new IllegalArgumentException("EntityEntry did not have status MANAGED or READ_ONLY: " + entityEntry);
    }
    final boolean traceEnabled = LOG.isTraceEnabled();
    // Find out if there are any unresolved insertions that are waiting for the
    // specified entity to be resolved.
    final Set<AbstractEntityInsertAction> dependentActions = dependentActionsByTransientEntity.remove(managedEntity);
    if (dependentActions == null) {
        if (traceEnabled) {
            LOG.tracev("No unresolved entity inserts that depended on [{0}]", MessageHelper.infoString(entityEntry.getEntityName(), entityEntry.getId()));
        }
        // NOTE EARLY EXIT!
        return Collections.emptySet();
    }
    final Set<AbstractEntityInsertAction> resolvedActions = new IdentitySet();
    if (traceEnabled) {
        LOG.tracev("Unresolved inserts beforeQuery resolving [{0}]: [{1}]", MessageHelper.infoString(entityEntry.getEntityName(), entityEntry.getId()), toString());
    }
    for (AbstractEntityInsertAction dependentAction : dependentActions) {
        if (traceEnabled) {
            LOG.tracev("Resolving insert [{0}] dependency on [{1}]", MessageHelper.infoString(dependentAction.getEntityName(), dependentAction.getId()), MessageHelper.infoString(entityEntry.getEntityName(), entityEntry.getId()));
        }
        final NonNullableTransientDependencies dependencies = dependenciesByAction.get(dependentAction);
        dependencies.resolveNonNullableTransientEntity(managedEntity);
        if (dependencies.isEmpty()) {
            if (traceEnabled) {
                LOG.tracev("Resolving insert [{0}] (only depended on [{1}])", dependentAction, MessageHelper.infoString(entityEntry.getEntityName(), entityEntry.getId()));
            }
            // dependentAction only depended on managedEntity..
            dependenciesByAction.remove(dependentAction);
            resolvedActions.add(dependentAction);
        }
    }
    if (traceEnabled) {
        LOG.tracev("Unresolved inserts afterQuery resolving [{0}]: [{1}]", MessageHelper.infoString(entityEntry.getEntityName(), entityEntry.getId()), toString());
    }
    return resolvedActions;
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) IdentitySet(org.hibernate.internal.util.collections.IdentitySet) NonNullableTransientDependencies(org.hibernate.engine.internal.NonNullableTransientDependencies)

Example 2 with IdentitySet

use of org.hibernate.internal.util.collections.IdentitySet in project hibernate-orm by hibernate.

the class QueryTranslatorImpl method list.

@Override
public List list(SharedSessionContractImplementor session, QueryParameters queryParameters) throws HibernateException {
    // Delegate to the QueryLoader...
    errorIfDML();
    final QueryNode query = (QueryNode) sqlAst;
    final boolean hasLimit = queryParameters.getRowSelection() != null && queryParameters.getRowSelection().definesLimits();
    final boolean needsDistincting = (query.getSelectClause().isDistinct() || hasLimit) && containsCollectionFetches();
    QueryParameters queryParametersToUse;
    if (hasLimit && containsCollectionFetches()) {
        LOG.firstOrMaxResultsSpecifiedWithCollectionFetch();
        RowSelection selection = new RowSelection();
        selection.setFetchSize(queryParameters.getRowSelection().getFetchSize());
        selection.setTimeout(queryParameters.getRowSelection().getTimeout());
        queryParametersToUse = queryParameters.createCopyUsing(selection);
    } else {
        queryParametersToUse = queryParameters;
    }
    List results = queryLoader.list(session, queryParametersToUse);
    if (needsDistincting) {
        int includedCount = -1;
        // NOTE : firstRow is zero-based
        int first = !hasLimit || queryParameters.getRowSelection().getFirstRow() == null ? 0 : queryParameters.getRowSelection().getFirstRow();
        int max = !hasLimit || queryParameters.getRowSelection().getMaxRows() == null ? -1 : queryParameters.getRowSelection().getMaxRows();
        List tmp = new ArrayList();
        IdentitySet distinction = new IdentitySet();
        for (final Object result : results) {
            if (!distinction.add(result)) {
                continue;
            }
            includedCount++;
            if (includedCount < first) {
                continue;
            }
            tmp.add(result);
            // NOTE : ( max - 1 ) because first is zero-based while max is not...
            if (max >= 0 && (includedCount - first) >= (max - 1)) {
                break;
            }
        }
        results = tmp;
    }
    return results;
}
Also used : QueryNode(org.hibernate.hql.internal.ast.tree.QueryNode) IdentitySet(org.hibernate.internal.util.collections.IdentitySet) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) QueryParameters(org.hibernate.engine.spi.QueryParameters) RowSelection(org.hibernate.engine.spi.RowSelection) EntityGraphQueryHint(org.hibernate.engine.query.spi.EntityGraphQueryHint)

Example 3 with IdentitySet

use of org.hibernate.internal.util.collections.IdentitySet in project hibernate-orm by hibernate.

the class EntityCopyAllowedLoggedObserver method entityCopyDetected.

@Override
public void entityCopyDetected(Object managedEntity, Object mergeEntity1, Object mergeEntity2, EventSource session) {
    final String entityName = session.getEntityName(managedEntity);
    LOG.trace(String.format("More than one representation of the same persistent entity being merged for: %s", MessageHelper.infoString(entityName, session.getIdentifier(managedEntity))));
    Set<Object> detachedEntitiesForManaged = null;
    if (managedToMergeEntitiesXref == null) {
        // This is the first time multiple representations have been found;
        // instantiate managedToMergeEntitiesXref.
        managedToMergeEntitiesXref = new IdentityHashMap<Object, Set<Object>>();
    } else {
        // Get any existing representations that have already been found.
        detachedEntitiesForManaged = managedToMergeEntitiesXref.get(managedEntity);
    }
    if (detachedEntitiesForManaged == null) {
        // There were no existing representations for this particular managed entity;
        detachedEntitiesForManaged = new IdentitySet();
        managedToMergeEntitiesXref.put(managedEntity, detachedEntitiesForManaged);
        incrementEntityNameCount(entityName);
    }
    // Now add the detached representation for the managed entity;
    detachedEntitiesForManaged.add(mergeEntity1);
    detachedEntitiesForManaged.add(mergeEntity2);
}
Also used : IdentitySet(org.hibernate.internal.util.collections.IdentitySet) Set(java.util.Set) IdentitySet(org.hibernate.internal.util.collections.IdentitySet)

Example 4 with IdentitySet

use of org.hibernate.internal.util.collections.IdentitySet in project hibernate-orm by hibernate.

the class HQLQueryPlan method performList.

/**
	 * Coordinates the efforts to perform a list across all the included query translators.
	 *
	 * @param queryParameters The query parameters
	 * @param session The session
	 *
	 * @return The query result list
	 *
	 * @throws HibernateException Indicates a problem performing the query
	 */
@SuppressWarnings("unchecked")
public List performList(QueryParameters queryParameters, SharedSessionContractImplementor session) throws HibernateException {
    if (traceEnabled) {
        LOG.tracev("Find: {0}", getSourceQuery());
        queryParameters.traceParameters(session.getFactory());
    }
    final RowSelection rowSelection = queryParameters.getRowSelection();
    final boolean hasLimit = rowSelection != null && rowSelection.definesLimits();
    final boolean needsLimit = hasLimit && translators.length > 1;
    final QueryParameters queryParametersToUse;
    if (needsLimit) {
        LOG.needsLimit();
        final RowSelection selection = new RowSelection();
        selection.setFetchSize(queryParameters.getRowSelection().getFetchSize());
        selection.setTimeout(queryParameters.getRowSelection().getTimeout());
        queryParametersToUse = queryParameters.createCopyUsing(selection);
    } else {
        queryParametersToUse = queryParameters;
    }
    //fast path to avoid unnecessary allocation and copying
    if (translators.length == 1) {
        return translators[0].list(session, queryParametersToUse);
    }
    final int guessedResultSize = guessResultSize(rowSelection);
    final List combinedResults = new ArrayList(guessedResultSize);
    final IdentitySet distinction;
    if (needsLimit) {
        distinction = new IdentitySet(guessedResultSize);
    } else {
        distinction = null;
    }
    int includedCount = -1;
    translator_loop: for (QueryTranslator translator : translators) {
        final List tmp = translator.list(session, queryParametersToUse);
        if (needsLimit) {
            // NOTE : firstRow is zero-based
            final int first = queryParameters.getRowSelection().getFirstRow() == null ? 0 : queryParameters.getRowSelection().getFirstRow();
            final int max = queryParameters.getRowSelection().getMaxRows() == null ? -1 : queryParameters.getRowSelection().getMaxRows();
            for (final Object result : tmp) {
                if (!distinction.add(result)) {
                    continue;
                }
                includedCount++;
                if (includedCount < first) {
                    continue;
                }
                combinedResults.add(result);
                if (max >= 0 && includedCount > max) {
                    // break the outer loop !!!
                    break translator_loop;
                }
            }
        } else {
            combinedResults.addAll(tmp);
        }
    }
    return combinedResults;
}
Also used : IdentitySet(org.hibernate.internal.util.collections.IdentitySet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) QueryParameters(org.hibernate.engine.spi.QueryParameters) RowSelection(org.hibernate.engine.spi.RowSelection) QueryTranslator(org.hibernate.hql.spi.QueryTranslator)

Example 5 with IdentitySet

use of org.hibernate.internal.util.collections.IdentitySet in project hibernate-orm by hibernate.

the class UnresolvedEntityInsertActions method addDependenciesByTransientEntity.

@SuppressWarnings({ "unchecked" })
private void addDependenciesByTransientEntity(AbstractEntityInsertAction insert, NonNullableTransientDependencies dependencies) {
    for (Object transientEntity : dependencies.getNonNullableTransientEntities()) {
        Set<AbstractEntityInsertAction> dependentActions = dependentActionsByTransientEntity.get(transientEntity);
        if (dependentActions == null) {
            dependentActions = new IdentitySet();
            dependentActionsByTransientEntity.put(transientEntity, dependentActions);
        }
        dependentActions.add(insert);
    }
}
Also used : IdentitySet(org.hibernate.internal.util.collections.IdentitySet)

Aggregations

IdentitySet (org.hibernate.internal.util.collections.IdentitySet)6 ArrayList (java.util.ArrayList)3 List (java.util.List)2 EntityEntry (org.hibernate.engine.spi.EntityEntry)2 QueryParameters (org.hibernate.engine.spi.QueryParameters)2 RowSelection (org.hibernate.engine.spi.RowSelection)2 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)1 NonNullableTransientDependencies (org.hibernate.engine.internal.NonNullableTransientDependencies)1 EntityGraphQueryHint (org.hibernate.engine.query.spi.EntityGraphQueryHint)1 TypedValue (org.hibernate.engine.spi.TypedValue)1 QueryNode (org.hibernate.hql.internal.ast.tree.QueryNode)1 QueryTranslator (org.hibernate.hql.spi.QueryTranslator)1 MarkerObject (org.hibernate.internal.util.MarkerObject)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 CompositeType (org.hibernate.type.CompositeType)1 IntegerType (org.hibernate.type.IntegerType)1