@SuppressWarnings("unchecked") @Override public <T> CloseableIterator<T> iterate(Query query, FactoryExpression<?> projection) { Iterator<T> iterator = null; Closeable closeable = null; if (query instanceof JpaQuery) { JpaQuery<T> elQuery = (JpaQuery<T>) query; elQuery.setHint(QueryHints.RESULT_SET_TYPE, ResultSetType.ForwardOnly); elQuery.setHint(QueryHints.SCROLLABLE_CURSOR, true); final Cursor cursor = elQuery.getResultCursor(); closeable = new Closeable() { @Override public void close() throws IOException { cursor.close(); } }; iterator = cursor; } else { iterator = query.getResultList().iterator(); } if (projection != null) { return new TransformingIterator<T>(iterator, closeable, projection); } else { return new IteratorAdapter<T>(iterator, closeable); } }
private void applyView(JpaQuery query, String queryString, AttributeGroup attrGroup, View view, boolean singleResultExpected) { boolean useFetchGroup = attrGroup instanceof FetchGroup; FetchGroupDescription description = calculateFetchGroup(queryString, view, singleResultExpected, useFetchGroup); if (attrGroup instanceof FetchGroup) ((FetchGroup) attrGroup).setShouldLoadAll(true); if (log.isTraceEnabled()) log.trace((useFetchGroup ? "Fetch" : "Load") + " group for " + view + ":\n" + description.getAttributes().stream().collect(Collectors.joining("\n"))); for (String attribute : description.getAttributes()) { attrGroup.addAttribute(attribute); } MetaClass metaClass = metadata.getClassNN(view.getEntityClass()); if (!metadataTools.isCacheable(metaClass)) { query.setHint(useFetchGroup ? QueryHints.FETCH_GROUP : QueryHints.LOAD_GROUP, attrGroup); } if (log.isDebugEnabled()) { String fetchModes = description.getHints().entrySet().stream() .map(e -> e.getKey() + "=" + (e.getValue().equals(QueryHints.LEFT_FETCH) ? "JOIN" : "BATCH")) .collect(Collectors.joining(", ")); log.debug("Fetch modes for " + view + ": " + (fetchModes.equals("") ? "<none>" : fetchModes)); } for (Map.Entry<String, String> entry : description.getHints().entrySet()) { query.setHint(entry.getValue(), entry.getKey()); } if (description.hasBatches()) { query.setHint(QueryHints.BATCH_TYPE, "IN"); } }
public void setView(JpaQuery query, String queryString, @Nullable View view, boolean singleResultExpected) { Preconditions.checkNotNullArgument(query, "query is null"); if (view != null) { AttributeGroup ag = view.loadPartialEntities() ? new FetchGroup() : new LoadGroup(); applyView(query, queryString, ag, view, singleResultExpected); } else { query.setHint(QueryHints.FETCH_GROUP, null); } }
MetaClass metaClass = metadata.getClassNN(view.getEntityClass()); if (!metadata.getTools().isCacheable(metaClass) || !singleResultExpected) { query.setHint(QueryHints.REFRESH, HintValues.TRUE); query.setHint(QueryHints.REFRESH_CASCADE, CascadePolicy.CascadeByMapping); query.setHint(QueryHints.PREPARE, HintValues.FALSE);