Search in sources :

Example 91 with Serializable

use of java.io.Serializable in project hibernate-orm by hibernate.

the class DynamicBatchingEntityLoaderBuilder method performOrderedMultiLoad.

@SuppressWarnings("unchecked")
private List performOrderedMultiLoad(OuterJoinLoadable persister, Serializable[] ids, SharedSessionContractImplementor session, MultiLoadOptions loadOptions) {
    assert loadOptions.isOrderReturnEnabled();
    final List result = CollectionHelper.arrayList(ids.length);
    final LockOptions lockOptions = (loadOptions.getLockOptions() == null) ? new LockOptions(LockMode.NONE) : loadOptions.getLockOptions();
    final int maxBatchSize;
    if (loadOptions.getBatchSize() != null && loadOptions.getBatchSize() > 0) {
        maxBatchSize = loadOptions.getBatchSize();
    } else {
        maxBatchSize = session.getJdbcServices().getJdbcEnvironment().getDialect().getDefaultBatchLoadSizingStrategy().determineOptimalBatchLoadSize(persister.getIdentifierType().getColumnSpan(session.getFactory()), ids.length);
    }
    final List<Serializable> idsInBatch = new ArrayList<>();
    final List<Integer> elementPositionsLoadedByBatch = new ArrayList<>();
    for (int i = 0; i < ids.length; i++) {
        final Serializable id = ids[i];
        final EntityKey entityKey = new EntityKey(id, persister);
        if (loadOptions.isSessionCheckingEnabled()) {
            // look for it in the Session first
            final Object managedEntity = session.getPersistenceContext().getEntity(entityKey);
            if (managedEntity != null) {
                if (!loadOptions.isReturnOfDeletedEntitiesEnabled()) {
                    final EntityEntry entry = session.getPersistenceContext().getEntry(managedEntity);
                    if (entry.getStatus() == Status.DELETED || entry.getStatus() == Status.GONE) {
                        // put a null in the result
                        result.add(i, null);
                        continue;
                    }
                }
                // if we did not hit the continue above, there is already an
                // entry in the PC for that entity, so use it...
                result.add(i, managedEntity);
                continue;
            }
        }
        // if we did not hit any of the continues above, then we need to batch
        // load the entity state.
        idsInBatch.add(ids[i]);
        if (idsInBatch.size() >= maxBatchSize) {
            performOrderedBatchLoad(idsInBatch, lockOptions, persister, session);
        }
        // Save the EntityKey instance for use later!
        result.add(i, entityKey);
        elementPositionsLoadedByBatch.add(i);
    }
    if (!idsInBatch.isEmpty()) {
        performOrderedBatchLoad(idsInBatch, lockOptions, persister, session);
    }
    for (Integer position : elementPositionsLoadedByBatch) {
        // the element value at this position in the result List should be
        // the EntityKey for that entity; reuse it!
        final EntityKey entityKey = (EntityKey) result.get(position);
        Object entity = session.getPersistenceContext().getEntity(entityKey);
        if (entity != null && !loadOptions.isReturnOfDeletedEntitiesEnabled()) {
            // make sure it is not DELETED
            final EntityEntry entry = session.getPersistenceContext().getEntry(entity);
            if (entry.getStatus() == Status.DELETED || entry.getStatus() == Status.GONE) {
                // the entity is locally deleted, and the options ask that we not return such entities...
                entity = null;
            }
        }
        result.set(position, entity);
    }
    return result;
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) LockOptions(org.hibernate.LockOptions) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 92 with Serializable

use of java.io.Serializable in project hibernate-orm by hibernate.

the class Loader method extractKeysFromResultSet.

protected void extractKeysFromResultSet(Loadable[] persisters, QueryParameters queryParameters, ResultSet resultSet, SharedSessionContractImplementor session, EntityKey[] keys, LockMode[] lockModes, List hydratedObjects) throws SQLException {
    final int entitySpan = persisters.length;
    final int numberOfPersistersToProcess;
    final Serializable optionalId = queryParameters.getOptionalId();
    if (isSingleRowLoader() && optionalId != null) {
        keys[entitySpan - 1] = session.generateEntityKey(optionalId, persisters[entitySpan - 1]);
        // skip the last persister below...
        numberOfPersistersToProcess = entitySpan - 1;
    } else {
        numberOfPersistersToProcess = entitySpan;
    }
    final Object[] hydratedKeyState = new Object[numberOfPersistersToProcess];
    for (int i = 0; i < numberOfPersistersToProcess; i++) {
        final Type idType = persisters[i].getIdentifierType();
        hydratedKeyState[i] = idType.hydrate(resultSet, getEntityAliases()[i].getSuffixedKeyAliases(), session, null);
    }
    for (int i = 0; i < numberOfPersistersToProcess; i++) {
        final Type idType = persisters[i].getIdentifierType();
        if (idType.isComponentType() && getCompositeKeyManyToOneTargetIndices() != null) {
            // we may need to force resolve any key-many-to-one(s)
            int[] keyManyToOneTargetIndices = getCompositeKeyManyToOneTargetIndices()[i];
            //		that would account for multiple levels whereas this scheme does not
            if (keyManyToOneTargetIndices != null) {
                for (int targetIndex : keyManyToOneTargetIndices) {
                    if (targetIndex < numberOfPersistersToProcess) {
                        final Type targetIdType = persisters[targetIndex].getIdentifierType();
                        final Serializable targetId = (Serializable) targetIdType.resolve(hydratedKeyState[targetIndex], session, null);
                        // todo : need a way to signal that this key is resolved and its data resolved
                        keys[targetIndex] = session.generateEntityKey(targetId, persisters[targetIndex]);
                    }
                    // this part copied from #getRow, this section could be refactored out
                    Object object = session.getEntityUsingInterceptor(keys[targetIndex]);
                    if (object != null) {
                        //its already loaded so don't need to hydrate it
                        instanceAlreadyLoaded(resultSet, targetIndex, persisters[targetIndex], keys[targetIndex], object, lockModes[targetIndex], session);
                    } else {
                        instanceNotYetLoaded(resultSet, targetIndex, persisters[targetIndex], getEntityAliases()[targetIndex].getRowIdAlias(), keys[targetIndex], lockModes[targetIndex], getOptionalObjectKey(queryParameters, session), queryParameters.getOptionalObject(), hydratedObjects, session);
                    }
                }
            }
        }
        final Serializable resolvedId = (Serializable) idType.resolve(hydratedKeyState[i], session, null);
        keys[i] = resolvedId == null ? null : session.generateEntityKey(resolvedId, persisters[i]);
    }
}
Also used : Serializable(java.io.Serializable) EntityType(org.hibernate.type.EntityType) VersionType(org.hibernate.type.VersionType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type)

Example 93 with Serializable

use of java.io.Serializable in project hibernate-orm by hibernate.

the class AbstractLoadPlanBasedCollectionInitializer method initialize.

@Override
public void initialize(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
    if (log.isDebugEnabled()) {
        log.debugf("Loading collection: %s", MessageHelper.collectionInfoString(collectionPersister, id, getFactory()));
    }
    final Serializable[] ids = new Serializable[] { id };
    try {
        final QueryParameters qp = new QueryParameters();
        qp.setPositionalParameterTypes(new Type[] { collectionPersister.getKeyType() });
        qp.setPositionalParameterValues(ids);
        qp.setCollectionKeys(ids);
        qp.setLockOptions(lockOptions);
        executeLoad(session, qp, staticLoadQuery, true, null);
    } catch (SQLException sqle) {
        throw session.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not initialize a collection: " + MessageHelper.collectionInfoString(collectionPersister, id, getFactory()), staticLoadQuery.getSqlStatement());
    }
    log.debug("Done loading collection");
}
Also used : Serializable(java.io.Serializable) SQLException(java.sql.SQLException) QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 94 with Serializable

use of java.io.Serializable in project hibernate-orm by hibernate.

the class CollectionReferenceInitializerImpl method finishUpRow.

@Override
public void finishUpRow(ResultSet resultSet, ResultSetProcessingContextImpl context) {
    try {
        // read the collection key for this reference for the current row.
        final PersistenceContext persistenceContext = context.getSession().getPersistenceContext();
        final Serializable collectionRowKey = (Serializable) collectionReference.getCollectionPersister().readKey(resultSet, aliases.getCollectionColumnAliases().getSuffixedKeyAliases(), context.getSession());
        if (collectionRowKey != null) {
            if (log.isDebugEnabled()) {
                log.debugf("Found row of collection: %s", MessageHelper.collectionInfoString(collectionReference.getCollectionPersister(), collectionRowKey, context.getSession().getFactory()));
            }
            Object collectionOwner = findCollectionOwner(collectionRowKey, resultSet, context);
            PersistentCollection rowCollection = persistenceContext.getLoadContexts().getCollectionLoadContext(resultSet).getLoadingCollection(collectionReference.getCollectionPersister(), collectionRowKey);
            if (rowCollection != null) {
                rowCollection.readFrom(resultSet, collectionReference.getCollectionPersister(), aliases.getCollectionColumnAliases(), collectionOwner);
            }
        } else {
            final Serializable optionalKey = findCollectionOwnerKey(context);
            if (optionalKey != null) {
                // since what we have is an empty collection
                if (log.isDebugEnabled()) {
                    log.debugf("Result set contains (possibly empty) collection: %s", MessageHelper.collectionInfoString(collectionReference.getCollectionPersister(), optionalKey, context.getSession().getFactory()));
                }
                // handle empty collection
                persistenceContext.getLoadContexts().getCollectionLoadContext(resultSet).getLoadingCollection(collectionReference.getCollectionPersister(), optionalKey);
            }
        }
    // else no collection element, but also no owner
    } catch (SQLException sqle) {
        // TODO: would be nice to have the SQL string that failed...
        throw context.getSession().getFactory().getSQLExceptionHelper().convert(sqle, "could not read next row of results");
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Serializable(java.io.Serializable) SQLException(java.sql.SQLException) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 95 with Serializable

use of java.io.Serializable in project hibernate-orm by hibernate.

the class ResultSetProcessorHelper method getOptionalObjectKey.

public static EntityKey getOptionalObjectKey(QueryParameters queryParameters, SharedSessionContractImplementor session) {
    final Object optionalObject = queryParameters.getOptionalObject();
    final Serializable optionalId = queryParameters.getOptionalId();
    final String optionalEntityName = queryParameters.getOptionalEntityName();
    return INSTANCE.interpretEntityKey(session, optionalEntityName, optionalId, optionalObject);
}
Also used : Serializable(java.io.Serializable)

Aggregations

Serializable (java.io.Serializable)1280 Test (org.junit.Test)334 HashMap (java.util.HashMap)265 ArrayList (java.util.ArrayList)215 Map (java.util.Map)127 List (java.util.List)94 Metacard (ddf.catalog.data.Metacard)89 IOException (java.io.IOException)89 HashSet (java.util.HashSet)70 Session (org.hibernate.Session)63 Task (org.apache.hadoop.hive.ql.exec.Task)57 ByteArrayInputStream (java.io.ByteArrayInputStream)50 Set (java.util.Set)50 ObjectInputStream (java.io.ObjectInputStream)43 Date (java.util.Date)41 LinkedHashMap (java.util.LinkedHashMap)40 EntityPersister (org.hibernate.persister.entity.EntityPersister)34 File (java.io.File)33 Iterator (java.util.Iterator)33 URI (java.net.URI)32