com.mysema.query.jpa.hibernate.AbstractHibernateQuery Maven / Gradle / Ivy
/*
* Copyright 2011, Mysema Ltd
*
* 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.mysema.query.jpa.hibernate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.hibernate.FlushMode;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mysema.commons.lang.CloseableIterator;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.NonUniqueResultException;
import com.mysema.query.QueryException;
import com.mysema.query.QueryMetadata;
import com.mysema.query.QueryModifiers;
import com.mysema.query.SearchResults;
import com.mysema.query.Tuple;
import com.mysema.query.jpa.FactoryExpressionTransformer;
import com.mysema.query.jpa.HQLTemplates;
import com.mysema.query.jpa.JPAQueryBase;
import com.mysema.query.jpa.JPQLTemplates;
import com.mysema.query.jpa.ScrollableResultsIterator;
import com.mysema.query.types.Expression;
import com.mysema.query.types.FactoryExpression;
import com.mysema.query.types.FactoryExpressionUtils;
import com.mysema.query.types.Path;
import com.mysema.query.types.QTuple;
/**
* Abstract base class for Hibernate API based implementations of the JPQL interface
*
* @author tiwe
*
* @param
*/
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, null);
this.session = session;
}
@Override
public long count() {
QueryModifiers modifiers = getMetadata().getModifiers();
String queryString = toCountRowsString();
logQuery(queryString);
Query query = createQuery(queryString, modifiers, true);
reset();
Long rv = (Long)query.uniqueResult();
if (rv != null) {
return rv.longValue();
} else {
throw new QueryException("Query returned null");
}
}
/**
* Expose the original Hibernate query for the given projection
*
* @param expr
* @return
*/
public Query createQuery(Expression> expr) {
queryMixin.addProjection(expr);
String queryString = toQueryString();
return createQuery(queryString, getMetadata().getModifiers(), false);
}
/**
* Expose the original Hibernate query for the given projection
*
* @param expr1
* @param expr2
* @param rest
* @return
*/
public Query createQuery(Expression> expr1, Expression> expr2, Expression>... rest) {
queryMixin.addProjection(expr1);
queryMixin.addProjection(expr2);
queryMixin.addProjection(rest);
String queryString = toQueryString();
logQuery(queryString);
return createQuery(queryString, getMetadata().getModifiers(), false);
}
/**
* Expose the original Hibernate query for the given projection
*
* @param args
* @return
*/
public Query createQuery(Expression>[] args) {
queryMixin.addProjection(args);
String queryString = toQueryString();
logQuery(queryString);
return createQuery(queryString, getMetadata().getModifiers(), false);
}
private Query createQuery(String queryString, @Nullable QueryModifiers modifiers, boolean forCount) {
Query query = session.createQuery(queryString);
HibernateUtil.setConstants(query, getConstants(), 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.intValue());
}
if (offset != null) {
query.setFirstResult(offset.intValue());
}
}
// set transformer, if necessary
List extends Expression>> projection = getMetadata().getProjection();
if (projection.size() == 1 && !forCount) {
Expression> expr = projection.get(0);
if (expr instanceof FactoryExpression>) {
query.setResultTransformer(new FactoryExpressionTransformer((FactoryExpression>) projection.get(0)));
}
} else if (!forCount) {
FactoryExpression> proj = FactoryExpressionUtils.wrap(projection);
if (proj != null) {
query.setResultTransformer(new FactoryExpressionTransformer(proj));
}
}
return query;
}
/**
* Return the query results as an Iterator.
*
* Entities returned as results are initialized on demand. The first
* SQL query returns identifiers only.
*/
@SuppressWarnings("unchecked")
public CloseableIterator iterate(Expression>... args) {
return iterate(new QTuple(args));
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
public
© 2015 - 2025 Weber Informatics LLC | Privacy Policy