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:com.mysema.query.jpa.hibernate.sql.AbstractHibernateSQLQuery.java

License:Apache License

private Query createQuery(String queryString) {
    logQuery(queryString);/*from   ww w  .  java  2s  .  c o  m*/
    org.hibernate.SQLQuery query = session.createSQLQuery(queryString);
    // set constants
    HibernateUtil.setConstants(query, constants, queryMixin.getMetadata().getParams());
    // set entity paths
    for (Path<?> path : entityPaths) {
        query.addEntity(path.toString(), path.getType());
    }
    // set result transformer, if projection is an EConstructor instance
    List<? extends Expression<?>> projection = queryMixin.getMetadata().getProjection();
    if (projection.size() == 1 && projection.get(0) instanceof FactoryExpression) {
        query.setResultTransformer(new FactoryExpressionTransformer((FactoryExpression<?>) projection.get(0)));
    }
    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 (readOnly != null) {
        query.setReadOnly(readOnly);
    }
    return query;
}

From source file:com.querydsl.jpa.hibernate.sql.AbstractHibernateSQLQuery.java

License:Apache License

private Query createQuery(boolean forCount) {
    NativeSQLSerializer serializer = (NativeSQLSerializer) serialize(forCount);
    String queryString = serializer.toString();
    logQuery(queryString, serializer.getConstantToLabel());
    org.hibernate.SQLQuery query = session.createSQLQuery(queryString);
    // set constants
    HibernateUtil.setConstants(query, serializer.getConstantToLabel(), queryMixin.getMetadata().getParams());

    if (!forCount) {
        ListMultimap<Expression<?>, String> aliases = serializer.getAliases();
        Set<String> used = Sets.newHashSet();
        // set entity paths
        Expression<?> projection = queryMixin.getMetadata().getProjection();
        if (projection instanceof FactoryExpression) {
            for (Expression<?> expr : ((FactoryExpression<?>) projection).getArgs()) {
                if (isEntityExpression(expr)) {
                    query.addEntity(extractEntityExpression(expr).toString(), expr.getType());
                } else if (aliases.containsKey(expr)) {
                    for (String scalar : aliases.get(expr)) {
                        if (!used.contains(scalar)) {
                            query.addScalar(scalar);
                            used.add(scalar);
                            break;
                        }/* w ww.  j ava 2  s. c  o m*/
                    }
                }
            }
        } else if (isEntityExpression(projection)) {
            query.addEntity(extractEntityExpression(projection).toString(), projection.getType());
        } else if (aliases.containsKey(projection)) {
            for (String scalar : aliases.get(projection)) {
                if (!used.contains(scalar)) {
                    query.addScalar(scalar);
                    used.add(scalar);
                    break;
                }
            }
        }

        // set result transformer, if projection is a FactoryExpression instance
        if (projection instanceof FactoryExpression) {
            query.setResultTransformer(new FactoryExpressionTransformer((FactoryExpression<?>) projection));
        }
    }

    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 (readOnly != null) {
        query.setReadOnly(readOnly);
    }
    return query;
}

From source file:org.openmrs.module.amrsreports.db.hibernate.MohHibernateCoreDAO.java

License:Open Source License

@Override
public List<Object> executeSqlQuery(String query, Map<String, Object> substitutions) {
    SQLQuery q = sessionFactory.getCurrentSession().createSQLQuery(query);

    for (Map.Entry<String, Object> e : substitutions.entrySet()) {
        if (e.getValue() instanceof Collection) {
            q.setParameterList(e.getKey(), (Collection) e.getValue());
        } else if (e.getValue() instanceof Object[]) {
            q.setParameterList(e.getKey(), (Object[]) e.getValue());
        } else if (e.getValue() instanceof Cohort) {
            q.setParameterList(e.getKey(), ((Cohort) e.getValue()).getMemberIds());
        } else if (e.getValue() instanceof Date) {
            q.setDate(e.getKey(), (Date) e.getValue());
        } else {//w w  w  .  j a va  2  s . c  o  m
            q.setParameter(e.getKey(), e.getValue());
        }

    }

    q.setReadOnly(true);

    List<Object> r = q.list();

    return r;
}

From source file:org.yamj.core.database.dao.ArtworkDao.java

License:Open Source License

public List<QueueDTO> getArtworkQueue(final CharSequence sql, final int maxResults) {
    SQLQuery query = currentSession().createSQLQuery(sql.toString());
    query.setReadOnly(true);
    query.setCacheable(true);/*w  ww . jav  a 2 s. c om*/
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }

    List<QueueDTO> queueElements = new ArrayList<>();

    List<Object[]> objects = query.list();
    for (Object[] object : objects) {
        QueueDTO queueElement = new QueueDTO();
        queueElement.setId(convertRowElementToLong(object[0]));
        queueElement.setArtworkType(convertRowElementToString(object[1]));
        queueElement.setDate(convertRowElementToDate(object[3]));
        if (queueElement.getDate() == null) {
            queueElement.setDate(convertRowElementToDate(object[2]));
        }
        queueElements.add(queueElement);
    }

    Collections.sort(queueElements);
    return queueElements;
}

From source file:org.yamj.core.database.dao.ArtworkDao.java

License:Open Source License

public List<QueueDTO> getArtworkLocatedQueue(final int maxResults) {
    final StringBuilder sql = new StringBuilder();
    sql.append("SELECT DISTINCT loc.id, loc.create_timestamp, loc.update_timestamp ");
    sql.append("FROM artwork_located loc ");
    sql.append("WHERE loc.status in ('NEW','UPDATED')");

    SQLQuery query = currentSession().createSQLQuery(sql.toString());
    query.setReadOnly(true);
    query.setCacheable(true);/*from   ww w .  j  av a2s .  c o m*/
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }

    List<QueueDTO> queueElements = new ArrayList<>();
    List<Object[]> objects = query.list();
    for (Object[] object : objects) {
        QueueDTO queueElement = new QueueDTO();
        queueElement.setId(convertRowElementToLong(object[0]));
        queueElement.setDate(convertRowElementToDate(object[2]));
        if (queueElement.getDate() == null) {
            queueElement.setDate(convertRowElementToDate(object[1]));
        }
        queueElements.add(queueElement);
    }

    Collections.sort(queueElements);
    return queueElements;
}

From source file:org.yamj.core.database.dao.ConfigDao.java

License:Open Source License

public Map<String, String> readConfig() {
    SQLQuery query = currentSession().createSQLQuery("select config_key, config_value from configuration");
    query.setReadOnly(true);
    query.setCacheable(true);/*from   ww  w .  j a v a2s.c om*/

    HashMap<String, String> config = new HashMap<>();
    List<Object[]> objects = query.list();
    for (Object[] object : objects) {
        String key = convertRowElementToString(object[0]);
        String value = convertRowElementToString(object[1]);
        config.put(key, value);
    }

    return config;
}

From source file:org.yamj.core.database.dao.MediaDao.java

License:Open Source License

public List<QueueDTO> getMediaQueue(final CharSequence sql, final int maxResults) {
    SQLQuery query = currentSession().createSQLQuery(sql.toString());
    query.setReadOnly(true);
    query.setCacheable(true);// ww w  . java 2  s  . com
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }

    List<QueueDTO> queueElements = new ArrayList<>();

    List<Object[]> objects = query.list();
    for (Object[] object : objects) {
        QueueDTO queueElement = new QueueDTO();
        queueElement.setId(convertRowElementToLong(object[0]));
        queueElement.setDate(convertRowElementToDate(object[2]));
        if (queueElement.getDate() == null) {
            queueElement.setDate(convertRowElementToDate(object[1]));
        }
        queueElements.add(queueElement);
    }

    Collections.sort(queueElements);
    return queueElements;
}

From source file:org.yamj.core.database.dao.MetadataDao.java

License:Open Source License

public List<QueueDTO> getMetadataQueue(final CharSequence sql, final int maxResults) {
    SQLQuery query = currentSession().createSQLQuery(sql.toString());
    query.setReadOnly(true);
    query.setCacheable(true);//from   w  w  w .  j  av a 2s  .  c om
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }

    List<QueueDTO> queueElements = new ArrayList<>();

    List<Object[]> objects = query.list();
    for (Object[] object : objects) {
        QueueDTO queueElement = new QueueDTO();
        queueElement.setId(convertRowElementToLong(object[0]));
        queueElement.setMetadataType(convertRowElementToString(object[1]));
        queueElement.setDate(convertRowElementToDate(object[3]));
        if (queueElement.getDate() == null) {
            queueElement.setDate(convertRowElementToDate(object[2]));
        }
        queueElements.add(queueElement);
    }

    Collections.sort(queueElements);
    return queueElements;
}

From source file:org.yamj.core.database.dao.TrailerDao.java

License:Open Source License

public List<QueueDTO> getTrailerQueueForScanning(final int maxResults) {
    final StringBuilder sql = new StringBuilder();
    sql.append("select vd.id,'");
    sql.append(MetaDataType.MOVIE);//w  ww . j av a 2  s.co m
    sql.append("' as metatype,vd.create_timestamp,vd.update_timestamp ");
    sql.append("from videodata vd ");
    sql.append("where vd.trailer_status in ('NEW','UPDATED') ");
    sql.append("and vd.status='DONE' ");
    sql.append("and vd.episode<0 ");
    sql.append("union ");
    sql.append("select ser.id,'");
    sql.append(MetaDataType.SERIES);
    sql.append("' as mediatype,ser.create_timestamp,ser.update_timestamp ");
    sql.append("from series ser ");
    sql.append("where ser.trailer_status in ('NEW','UPDATED') ");
    sql.append("and ser.status='DONE' ");

    SQLQuery query = currentSession().createSQLQuery(sql.toString());
    query.setReadOnly(true);
    query.setCacheable(true);
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }

    List<QueueDTO> queueElements = new ArrayList<>();

    List<Object[]> objects = query.list();
    for (Object[] object : objects) {
        QueueDTO queueElement = new QueueDTO();
        queueElement.setId(convertRowElementToLong(object[0]));
        queueElement.setMetadataType(convertRowElementToString(object[1]));
        queueElement.setDate(convertRowElementToDate(object[3]));
        if (queueElement.getDate() == null) {
            queueElement.setDate(convertRowElementToDate(object[2]));
        }
        queueElements.add(queueElement);
    }

    Collections.sort(queueElements);
    return queueElements;
}

From source file:org.yamj.core.database.dao.TrailerDao.java

License:Open Source License

public List<QueueDTO> getTrailerQueueForProcessing(final int maxResults) {
    final StringBuilder sql = new StringBuilder();
    sql.append("SELECT DISTINCT t.id, t.create_timestamp, t.update_timestamp ");
    sql.append("FROM trailer t ");
    sql.append("WHERE t.status in ('NEW','UPDATED')");

    SQLQuery query = currentSession().createSQLQuery(sql.toString());
    query.setReadOnly(true);
    query.setCacheable(true);/*from   w w w .j ava 2  s. c om*/
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }

    List<QueueDTO> queueElements = new ArrayList<>();
    List<Object[]> objects = query.list();
    for (Object[] object : objects) {
        QueueDTO queueElement = new QueueDTO();
        queueElement.setId(convertRowElementToLong(object[0]));
        queueElement.setDate(convertRowElementToDate(object[2]));
        if (queueElement.getDate() == null) {
            queueElement.setDate(convertRowElementToDate(object[1]));
        }
        queueElements.add(queueElement);
    }

    Collections.sort(queueElements);
    return queueElements;
}