use of org.eclipse.persistence.jpa.JpaQuery in project cuba by cuba-platform.
the class FetchGroupManager method applyView.
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");
}
}
use of org.eclipse.persistence.jpa.JpaQuery in project cuba by cuba-platform.
the class QueryImpl method getQuery.
protected JpaQuery<T> getQuery() {
if (query == null) {
View view = views.isEmpty() ? null : views.get(0);
if (isNative) {
log.trace("Creating SQL query: {}", queryString);
if (resultClass == null)
query = (JpaQuery) emDelegate.createNativeQuery(queryString);
else {
if (!Entity.class.isAssignableFrom(resultClass)) {
throw new IllegalArgumentException("Non-entity result class for native query is not supported" + " by EclipseLink: " + resultClass);
}
Class effectiveClass = metadata.getExtendedEntities().getEffectiveClass(resultClass);
query = (JpaQuery) emDelegate.createNativeQuery(queryString, effectiveClass);
}
} else {
log.trace("Creating JPQL query: {}", queryString);
transformedQueryString = transformQueryString();
log.trace("Transformed JPQL query: {}", transformedQueryString);
Class effectiveClass = getEffectiveResultClass();
query = buildJPAQuery(transformedQueryString, effectiveClass);
if (view != 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);
}
}
}
if (flushMode == null) {
if (view != null && !view.loadPartialEntities()) {
query.setFlushMode(FlushModeType.AUTO);
} else {
query.setFlushMode(FlushModeType.COMMIT);
}
} else {
query.setFlushMode(flushMode);
}
boolean nullParam = false;
for (Param param : params) {
param.apply(query);
if (param.value == null)
nullParam = true;
}
addMacroParams(query);
// disable SQL caching to support "is null" generation
if (nullParam)
query.setHint(QueryHints.PREPARE, HintValues.FALSE);
// nested collections in some cases
if (maxResults != null && !singleResultExpected)
query.setMaxResults(maxResults);
if (firstResult != null && !singleResultExpected)
query.setFirstResult(firstResult);
if (lockMode != null)
query.setLockMode(lockMode);
if (hints != null) {
for (Map.Entry<String, Object> hint : hints.entrySet()) {
hintsProcessor.applyQueryHint(query, hint.getKey(), hint.getValue());
}
}
for (int i = 0; i < views.size(); i++) {
if (i == 0)
fetchGroupMgr.setView(query, queryString, views.get(i), singleResultExpected);
else
fetchGroupMgr.addView(query, queryString, views.get(i), singleResultExpected);
}
}
// noinspection unchecked
return query;
}
use of org.eclipse.persistence.jpa.JpaQuery in project querydsl by querydsl.
the class EclipseLinkHandler method iterate.
@SuppressWarnings("unchecked")
@Override
public <T> CloseableIterator<T> iterate(Query query, FactoryExpression<?> projection) {
boolean canUseCursor = false;
try {
canUseCursor = query.unwrap(Query.class) instanceof JpaQuery;
}// can't unwrap, just ignore the exception
catch (PersistenceException e) {
}
Iterator<T> iterator = null;
Closeable closeable = null;
if (canUseCursor) {
query.setHint(QueryHints.CURSOR, HintValues.TRUE);
final Cursor cursor = (Cursor) query.getSingleResult();
final int pageSize = cursor.getPageSize();
closeable = new Closeable() {
@Override
public void close() throws IOException {
cursor.close();
}
};
iterator = new Iterator<T>() {
private int rowsSinceLastClear = 0;
@Override
public boolean hasNext() {
return cursor.hasNext();
}
@Override
public T next() {
if (rowsSinceLastClear++ == pageSize) {
rowsSinceLastClear = 0;
cursor.clear();
}
return (T) cursor.next();
}
};
} else {
iterator = query.getResultList().iterator();
}
if (projection != null) {
return new TransformingIterator<T>(iterator, closeable, projection);
} else {
return new IteratorAdapter<T>(iterator, closeable);
}
}
Aggregations