Example usage for org.hibernate SQLQuery setReadOnly

List of usage examples for org.hibernate SQLQuery setReadOnly

Introduction

In this page you can find the example usage for org.hibernate SQLQuery setReadOnly.

Prototype

@Override
    NativeQuery<T> setReadOnly(boolean readOnly);

Source Link

Usage

From source file:org.yamj.core.hibernate.HibernateDao.java

License:Open Source License

/**
 * Execute a query to return the results
 *
 * Gets the options from the wrapper for start and max
 *
 * Puts the total count returned from the query into the wrapper
 *
 * @param <T>//from   www  .  java 2s.  c  o  m
 * @param T The class to return the transformed results of.
 * @param sqlScalars
 * @param wrapper
 * @return
 */
@SuppressWarnings("rawtypes")
public <T> List<T> executeQueryWithTransform(Class T, SqlScalars sqlScalars, IApiWrapper wrapper) {
    SQLQuery query = sqlScalars.createSqlQuery(currentSession());
    query.setReadOnly(true);
    query.setCacheable(true);

    if (T != null) {
        if (T.equals(String.class)) {
            // no transformer needed
        } else if (T.equals(Long.class)) {
            // no transformer needed
        } else if (T.equals(Object[].class)) {
            // no transformer needed
        } else {
            query.setResultTransformer(Transformers.aliasToBean(T));
        }
    }

    // Add the scalars to the query
    sqlScalars.populateScalars(query);

    List<T> queryResults = query.list();

    // If the wrapper is populated, then run the query to get the maximum results
    if (wrapper != null) {
        wrapper.setTotalCount(queryResults.size());

        // If there is a start or max set, we will need to re-run the query after setting the options
        IOptions options = wrapper.getOptions();
        if (options != null) {
            if (options.getStart() > 0 || options.getMax() > 0) {
                if (options.getStart() > 0) {
                    query.setFirstResult(options.getStart());
                }

                if (options.getMax() > 0) {
                    query.setMaxResults(options.getMax());
                }
                // This will get the trimmed list
                queryResults = query.list();
            }
        }
    }

    return queryResults;
}

From source file:ubic.gemma.persistence.util.CommonQueries.java

License:Apache License

/**
 * @param session      session//ww w.ja v  a  2s .  c om
 * @param genes        genes
 * @param arrayDesigns array design
 * @return map of probe IDs to collections of gene IDs.
 */
public static Map<Long, Collection<Long>> getCs2GeneIdMap(Collection<Long> genes, Collection<Long> arrayDesigns,
        Session session) {

    Map<Long, Collection<Long>> cs2genes = new HashMap<>();

    String queryString = "SELECT CS AS csid, GENE AS geneId FROM GENE2CS g WHERE g.GENE IN (:geneIds) AND g.AD IN (:ads)";
    SQLQuery queryObject = session.createSQLQuery(queryString);
    queryObject.addScalar("csid", LongType.INSTANCE);
    queryObject.addScalar("geneId", LongType.INSTANCE);
    queryObject.setParameterList("ads", arrayDesigns);
    queryObject.setParameterList("geneIds", genes);
    queryObject.setReadOnly(true);
    queryObject.setFlushMode(FlushMode.MANUAL);

    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);
    CommonQueries.addGeneIds(cs2genes, results);
    results.close();

    return cs2genes;

}

From source file:ubic.gemma.persistence.util.CommonQueries.java

License:Apache License

/**
 * @param session session/*from w  w  w  .  j  ava2 s .com*/
 * @param probes  probes
 * @return map of probes to all the genes 'detected' by those probes. Probes that don't map to genes will have an
 * empty gene collection.
 */
public static Map<Long, Collection<Long>> getCs2GeneMapForProbes(Collection<Long> probes, Session session) {
    if (probes.isEmpty())
        return new HashMap<>();

    Map<Long, Collection<Long>> cs2genes = new HashMap<>();

    String queryString = "SELECT CS AS csid, GENE AS geneId FROM GENE2CS g WHERE g.CS IN (:probes) ";
    org.hibernate.SQLQuery queryObject = session.createSQLQuery(queryString);
    queryObject.addScalar("csid", LongType.INSTANCE);
    queryObject.addScalar("geneId", LongType.INSTANCE);
    queryObject.setParameterList("probes", probes, LongType.INSTANCE);
    queryObject.setReadOnly(true);
    queryObject.setFlushMode(FlushMode.MANUAL);

    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);
    CommonQueries.addGeneIds(cs2genes, results);
    results.close();

    return cs2genes;
}