Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.jpa.hibernate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.hibernate.*;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.*;
import com.querydsl.core.NonUniqueResultException;
import com.querydsl.core.QueryException;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.FactoryExpression;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.*;
/**
* Abstract base class for Hibernate API based implementations of the JPQL interface
*
* @param result type
* @param concrete subtype
*
* @author tiwe
*/
public abstract class AbstractHibernateQuery> extends JPAQueryBase {
private static final Logger logger = LoggerFactory.getLogger(HibernateQuery.class);
@Nullable
protected Boolean cacheable, readOnly;
@Nullable
protected String cacheRegion, comment;
protected int fetchSize = 0;
protected final Map,LockMode> lockModes = new HashMap,LockMode>();
@Nullable
protected FlushMode flushMode;
private final SessionHolder session;
protected int timeout = 0;
public AbstractHibernateQuery(Session session) {
this(new DefaultSessionHolder(session), HQLTemplates.DEFAULT, new DefaultQueryMetadata());
}
public AbstractHibernateQuery(SessionHolder session, JPQLTemplates patterns, QueryMetadata metadata) {
super(metadata, patterns);
this.session = session;
}
@Override
public long fetchCount() {
QueryModifiers modifiers = getMetadata().getModifiers();
try {
Query query = createQuery(modifiers, true);
Long rv = (Long) query.uniqueResult();
if (rv != null) {
return rv;
} else {
throw new QueryException("Query returned null");
}
} finally {
reset();
}
}
/**
* Expose the original Hibernate query for the given projection
*
* @return query
*/
public Query createQuery() {
return createQuery(getMetadata().getModifiers(), false);
}
private Query createQuery(@Nullable QueryModifiers modifiers, boolean forCount) {
JPQLSerializer serializer = serialize(forCount);
String queryString = serializer.toString();
logQuery(queryString, serializer.getConstantToLabel());
Query query = session.createQuery(queryString);
HibernateUtil.setConstants(query, serializer.getConstantToLabel(), getMetadata().getParams());
if (fetchSize > 0) {
query.setFetchSize(fetchSize);
}
if (timeout > 0) {
query.setTimeout(timeout);
}
if (cacheable != null) {
query.setCacheable(cacheable);
}
if (cacheRegion != null) {
query.setCacheRegion(cacheRegion);
}
if (comment != null) {
query.setComment(comment);
}
if (readOnly != null) {
query.setReadOnly(readOnly);
}
for (Map.Entry, LockMode> entry : lockModes.entrySet()) {
query.setLockMode(entry.getKey().toString(), entry.getValue());
}
if (flushMode != null) {
query.setFlushMode(flushMode);
}
if (modifiers != null && modifiers.isRestricting()) {
Integer limit = modifiers.getLimitAsInteger();
Integer offset = modifiers.getOffsetAsInteger();
if (limit != null) {
query.setMaxResults(limit);
}
if (offset != null) {
query.setFirstResult(offset);
}
}
// set transformer, if necessary
Expression> projection = getMetadata().getProjection();
if (!forCount && projection instanceof FactoryExpression) {
query.setResultTransformer(new FactoryExpressionTransformer((FactoryExpression>) projection));
}
return query;
}
/**
* Return the query results as an Iterator. If the query
* contains multiple results pre row, the results are returned in
* an instance of Object[].
*
* Entities returned as results are initialized on demand. The first
* SQL query returns identifiers only.
*/
@Override
public CloseableIterator iterate() {
try {
Query query = createQuery();
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
return new ScrollableResultsIterator(results);
} finally {
reset();
}
}
@Override
@SuppressWarnings("unchecked")
public List fetch() {
try {
return createQuery().list();
} finally {
reset();
}
}
@Override
public QueryResults fetchResults() {
try {
Query countQuery = createQuery(null, true);
long total = (Long) countQuery.uniqueResult();
if (total > 0) {
QueryModifiers modifiers = getMetadata().getModifiers();
Query query = createQuery(modifiers, false);
@SuppressWarnings("unchecked")
List list = query.list();
return new QueryResults(list, modifiers, total);
} else {
return QueryResults.emptyResults();
}
} finally {
reset();
}
}
protected void logQuery(String queryString, Map