/** * Return the batch fetch policy for configuring batch fetching. */ public BatchFetchPolicy getBatchFetchPolicy() { if (batchFetchPolicy == null) { batchFetchPolicy = new BatchFetchPolicy(); } return batchFetchPolicy; }
/** * INTERNAL: * Return all attributes specified for batch reading. */ public List<Expression> getBatchReadAttributeExpressions() { return getBatchFetchPolicy().getAttributeExpressions(); }
/** * INTERNAL: * Return temporary map of batched objects. */ public Map<Object, Object> getBatchObjects() { return getBatchFetchPolicy().getBatchObjects(); }
this.batchFetchPolicy = new BatchFetchPolicy(); if (getDescriptor().getObjectBuilder().hasInBatchFetchedAttribute()) { this.batchFetchPolicy.setType(BatchFetchType.IN); this.batchFetchPolicy.setMappingQueries(new HashMap(batchedMappings.size())); initialized = true; int size = batchedMappings.size(); this.batchFetchPolicy.getMappingQueries().put(mapping, nestedQuery); this.batchFetchPolicy.setBatchedMappings(getDescriptor().getObjectBuilder().getBatchFetchedAttributes()); this.batchFetchPolicy.setAttributes(new ArrayList(batchReadAttributeExpressions.size())); if (!initialized) { this.batchFetchPolicy.setMappingQueries(new HashMap(batchReadAttributeExpressions.size()));
return null; } else if ((result != null) || (!originalPolicy.isIN())) { return result; if (originalPolicy.isIN()) { List<AbstractRecord> parentRows = originalPolicy.getDataResults(this); int size = Math.min(rowsSize, originalPolicy.getSize()); if (size == 0) { return null; originalPolicy.setDataResults(this, remainingParentRows); translationRow = translationRow.clone(); translationRow.put(QUERY_BATCH_PARAMETER, foreignKeyValues); batchedObjects.put(foreignKey, Helper.NULL_VALUE); } else if (batchQuery.isReadAllQuery() && ((ReadAllQuery)batchQuery).getBatchFetchPolicy().isIN()) { throw QueryException.originalQueryMustUseBatchIN(this, originalQuery);
/** * INTERNAL: * builds and registers an object from a row for cursors. * Behavior is different from the query version in that refreshing is not * supported. */ protected Object buildAndRegisterObject(AbstractRecord row) { ReadQuery query = this.query; if (query.isObjectLevelReadQuery()) { ObjectLevelReadQuery objectQuery = (ObjectLevelReadQuery)query; if (objectQuery.hasBatchReadAttributes() && objectQuery.getBatchFetchPolicy().isIN()) { objectQuery.getBatchFetchPolicy().addDataResults(row); } if (this.session.isUnitOfWork() && (!query.isReportQuery()) && query.shouldMaintainCache() && (objectQuery.shouldConformResultsInUnitOfWork() || objectQuery.getDescriptor().shouldAlwaysConformResultsInUnitOfWork())) { Object object = objectQuery.conformIndividualResult( objectQuery.buildObject(row), (UnitOfWorkImpl)this.session, this.translationRow, this.selectionCriteriaClone, this.initiallyConformingIndex); // Notifies caller to continue until conforming instance found if (object == null) { return InvalidObject.instance; } return object; } } return query.buildObject(row); }
/** * INTERNAL: * Compute the cache batched attributes. * Used to recompute batched attributes for nested aggregate queries. */ public void computeBatchReadAttributes() { List<Expression> batchReadAttributeExpressions = getBatchReadAttributeExpressions(); this.batchFetchPolicy.setAttributes(new ArrayList(batchReadAttributeExpressions.size())); int size = batchReadAttributeExpressions.size(); for (int index = 0; index < size; index++) { ObjectExpression objectExpression = (ObjectExpression)batchReadAttributeExpressions.get(index); // Expression may not have been initialized. ExpressionBuilder builder = objectExpression.getBuilder(); if (builder.getSession() == null) { builder.setSession(getSession().getRootSession(null)); } if (builder.getQueryClass() == null) { builder.setQueryClass(getReferenceClass()); } // PERF: Cache join attribute names. ObjectExpression baseExpression = objectExpression; while (!baseExpression.getBaseExpression().isExpressionBuilder()) { baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); } this.batchFetchPolicy.getAttributes().add(baseExpression.getName()); } }
baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); this.batchFetchPolicy.getAttributes().add(baseExpression.getName()); if (!this.batchFetchPolicy.getMappingQueries().containsKey(mapping)) { this.batchFetchPolicy.getMappingQueries().put(mapping, nestedQuery);
/** * INTERNAL: * Creates and returns a copy of this query. * @return A clone of this instance. */ @Override public Object clone() { final ObjectLevelReadQuery cloneQuery = (ObjectLevelReadQuery)super.clone(); // Must also clone the joined expressions as always joined attribute will be added // don't use setters as this will trigger unprepare. if (joinedAttributeManager != null) { cloneQuery.joinedAttributeManager = joinedAttributeManager.clone(); cloneQuery.joinedAttributeManager.setBaseQuery(cloneQuery); } if (this.batchFetchPolicy != null) { cloneQuery.batchFetchPolicy = batchFetchPolicy.clone(); } if (this.nonFetchJoinAttributeExpressions != null){ cloneQuery.nonFetchJoinAttributeExpressions = new ArrayList<>(nonFetchJoinAttributeExpressions); } // Don't use setters as that will trigger unprepare if (this.orderByExpressions != null) { cloneQuery.orderByExpressions = new ArrayList<>(this.orderByExpressions); } if (this.fetchGroup != null) { cloneQuery.fetchGroup = fetchGroup.clone(); // don't clone immutable entityFetchGroup } return cloneQuery; }
/** * INTERNAL: * Retrieve the value through using batch reading. * This executes a single query to read the target for all of the objects and stores the * result of the batch query in the original query to allow the other objects to share the results. */ protected Object batchedValueFromRow(AbstractRecord row, ObjectLevelReadQuery query, CacheKey parentCacheKey) { ReadQuery batchQuery = (ReadQuery)query.getProperty(this); if (batchQuery == null) { if (query.hasBatchReadAttributes()) { Map<DatabaseMapping, ReadQuery> queries = query.getBatchFetchPolicy().getMappingQueries(); if (queries != null) { batchQuery = queries.get(this); } } if (batchQuery == null) { batchQuery = prepareNestedBatchQuery(query); batchQuery.setIsExecutionClone(true); } else { batchQuery = (ReadQuery)batchQuery.clone(); batchQuery.setIsExecutionClone(true); } query.setProperty(this, batchQuery); } return this.indirectionPolicy.valueFromBatchQuery(batchQuery, row, query, parentCacheKey); }
BatchFetchType batchType = query.getBatchFetchPolicy().getType(); if (this.batchFetchType != null) { batchType = this.batchFetchType;
this.batchFetchPolicy = new BatchFetchPolicy(); if (getDescriptor().getObjectBuilder().hasInBatchFetchedAttribute()) { this.batchFetchPolicy.setType(BatchFetchType.IN); this.batchFetchPolicy.setMappingQueries(new HashMap(batchedMappings.size())); initialized = true; int size = batchedMappings.size(); this.batchFetchPolicy.getMappingQueries().put(mapping, nestedQuery); this.batchFetchPolicy.setBatchedMappings(getDescriptor().getObjectBuilder().getBatchFetchedAttributes()); this.batchFetchPolicy.setAttributes(new ArrayList(batchReadAttributeExpressions.size())); if (!initialized) { this.batchFetchPolicy.setMappingQueries(new HashMap(batchReadAttributeExpressions.size()));
return null; } else if ((result != null) || (!originalPolicy.isIN())) { return result; if (originalPolicy.isIN()) { List<AbstractRecord> parentRows = originalPolicy.getDataResults(this); int size = Math.min(rowsSize, originalPolicy.getSize()); if (size == 0) { return null; originalPolicy.setDataResults(this, remainingParentRows); translationRow = translationRow.clone(); translationRow.put(QUERY_BATCH_PARAMETER, foreignKeyValues); batchedObjects.put(foreignKey, Helper.NULL_VALUE); } else if (batchQuery.isReadAllQuery() && ((ReadAllQuery)batchQuery).getBatchFetchPolicy().isIN()) { throw QueryException.originalQueryMustUseBatchIN(this, originalQuery);
/** * INTERNAL: * builds and registers an object from a row for cursors. * Behavior is different from the query version in that refreshing is not * supported. */ protected Object buildAndRegisterObject(AbstractRecord row) { ReadQuery query = this.query; if (query.isObjectLevelReadQuery()) { ObjectLevelReadQuery objectQuery = (ObjectLevelReadQuery)query; if (objectQuery.hasBatchReadAttributes() && objectQuery.getBatchFetchPolicy().isIN()) { objectQuery.getBatchFetchPolicy().addDataResults(row); } if (this.session.isUnitOfWork() && (!query.isReportQuery()) && query.shouldMaintainCache() && (objectQuery.shouldConformResultsInUnitOfWork() || objectQuery.getDescriptor().shouldAlwaysConformResultsInUnitOfWork())) { Object object = objectQuery.conformIndividualResult( objectQuery.buildObject(row), (UnitOfWorkImpl)this.session, this.translationRow, this.selectionCriteriaClone, this.initiallyConformingIndex); // Notifies caller to continue until conforming instance found if (object == null) { return InvalidObject.instance; } return object; } } return query.buildObject(row); }
/** * INTERNAL: * Compute the cache batched attributes. * Used to recompute batched attributes for nested aggregate queries. */ public void computeBatchReadAttributes() { List<Expression> batchReadAttributeExpressions = getBatchReadAttributeExpressions(); this.batchFetchPolicy.setAttributes(new ArrayList(batchReadAttributeExpressions.size())); int size = batchReadAttributeExpressions.size(); for (int index = 0; index < size; index++) { ObjectExpression objectExpression = (ObjectExpression)batchReadAttributeExpressions.get(index); // Expression may not have been initialized. ExpressionBuilder builder = objectExpression.getBuilder(); if (builder.getSession() == null) { builder.setSession(getSession().getRootSession(null)); } if (builder.getQueryClass() == null) { builder.setQueryClass(getReferenceClass()); } // PERF: Cache join attribute names. ObjectExpression baseExpression = objectExpression; while (!baseExpression.getBaseExpression().isExpressionBuilder()) { baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); } this.batchFetchPolicy.getAttributes().add(baseExpression.getName()); } }
baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); this.batchFetchPolicy.getAttributes().add(baseExpression.getName()); if (!this.batchFetchPolicy.getMappingQueries().containsKey(mapping)) { this.batchFetchPolicy.getMappingQueries().put(mapping, nestedQuery);
/** * INTERNAL: * Clone the query */ @Override public Object clone() { ObjectLevelReadQuery cloneQuery = (ObjectLevelReadQuery)super.clone(); // Must also clone the joined expressions as always joined attribute will be added // don't use setters as this will trigger unprepare. if (this.joinedAttributeManager != null) { cloneQuery.joinedAttributeManager = this.joinedAttributeManager.clone(); cloneQuery.joinedAttributeManager.setBaseQuery(cloneQuery); } if (this.batchFetchPolicy != null) { cloneQuery.batchFetchPolicy = this.batchFetchPolicy.clone(); } if (this.nonFetchJoinAttributeExpressions != null){ cloneQuery.nonFetchJoinAttributeExpressions = new ArrayList<Expression>(this.nonFetchJoinAttributeExpressions); } // Don't use setters as that will trigger unprepare if (this.orderByExpressions != null) { cloneQuery.orderByExpressions = new ArrayList<Expression>(this.orderByExpressions); } if (this.fetchGroup != null) { cloneQuery.fetchGroup = this.fetchGroup.clone(); // don't clone immutable entityFetchGroup } return cloneQuery; }
/** * INTERNAL: * Retrieve the value through using batch reading. * This executes a single query to read the target for all of the objects and stores the * result of the batch query in the original query to allow the other objects to share the results. */ protected Object batchedValueFromRow(AbstractRecord row, ObjectLevelReadQuery query, CacheKey parentCacheKey) { ReadQuery batchQuery = (ReadQuery)query.getProperty(this); if (batchQuery == null) { if (query.hasBatchReadAttributes()) { Map<DatabaseMapping, ReadQuery> queries = query.getBatchFetchPolicy().getMappingQueries(); if (queries != null) { batchQuery = queries.get(this); } } if (batchQuery == null) { batchQuery = prepareNestedBatchQuery(query); batchQuery.setIsExecutionClone(true); } else { batchQuery = (ReadQuery)batchQuery.clone(); batchQuery.setIsExecutionClone(true); } query.setProperty(this, batchQuery); } return this.indirectionPolicy.valueFromBatchQuery(batchQuery, row, query, parentCacheKey); }