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;
}
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]);
}
}
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");
}
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");
}
}
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);
}
Aggregations