Example usage for org.hibernate SQLQuery getQueryString

List of usage examples for org.hibernate SQLQuery getQueryString

Introduction

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

Prototype

String getQueryString();

Source Link

Document

Get the query string.

Usage

From source file:com.lighting.platform.base.dao.HibernateDao.java

License:Apache License

public <K> Page<K> pageSQLQuery(PageConfig pageConfig, SQLQuery sqlQuery, Object... values) {
    Page<K> page = new Page<K>(pageConfig);
    String totalSql = "select count(*) as total from(" + sqlQuery.getQueryString() + ")";
    Query countQuery = this.getSession().createSQLQuery(totalSql);
    setParams(countQuery, values);/*from   w  ww.  j  a v a 2  s . c o m*/
    Number total = (Number) countQuery.uniqueResult();
    if (total != null) {
        page.setTotal(total.longValue());
        sqlQuery.setFirstResult(page.getOffset()).setMaxResults(page.getPageSize());
        setParams(sqlQuery, values);
        List<K> list = sqlQuery.list();
        page.setRows(list);
    } else {
        page.setTotal(0);
        page.setRows(new ArrayList<K>());
    }
    return page;
}

From source file:com.proper.uip.common.core.dao.HibernateDao.java

License:Apache License

@SuppressWarnings("unchecked")
public <K> Page<K> pageSQLQuery(PageConfig pageConfig, SQLQuery sqlQuery, Object... values) {
    Page<K> page = new Page<K>(pageConfig);
    String totalSql = "select count(*) as total from(" + sqlQuery.getQueryString() + ")";
    Query countQuery = this.getSession().createSQLQuery(totalSql);
    setParams(countQuery, values);/*from   w w  w. j  a va2  s . c o  m*/
    Number total = (Number) countQuery.uniqueResult();
    if (total != null) {
        page.setTotal(total.longValue());
        sqlQuery.setFirstResult(page.getOffset()).setMaxResults(page.getPageSize());
        setParams(sqlQuery, values);
        List<K> list = sqlQuery.list();
        page.setRows(list);
    } else {
        page.setTotal(0);
        page.setRows(new ArrayList<K>());
    }
    return page;
}

From source file:ispyb.server.common.services.ws.rest.session.SessionServiceBean.java

License:Open Source License

@Override
public List<Map<String, Object>> getSessionViewByProposalId(int proposalId) {
    Session session = (Session) this.entityManager.getDelegate();
    SQLQuery query = session.createSQLQuery(ByProposalId);
    System.out.println(query.getQueryString());
    /** Setting the parameters **/
    query.setParameter("proposalId", proposalId);
    return executeSQLQuery(query);
}

From source file:ispyb.server.common.services.ws.rest.session.SessionServiceBean.java

License:Open Source License

@Override
public List<Map<String, Object>> getSessionViewByProposalAndDates(int proposalId, String startDate,
        String endDate) {//from   www .  j  a  v  a  2 s.c om
    Session session = (Session) this.entityManager.getDelegate();
    SQLQuery query = session.createSQLQuery(ByProposalAndDates);
    query.setParameter("startDate", startDate);
    query.setParameter("endDate", endDate);
    query.setParameter("proposalId", proposalId);
    System.out.println(query.getQueryString());
    return executeSQLQuery(query);
}

From source file:ispyb.server.common.services.ws.rest.session.SessionServiceBean.java

License:Open Source License

@Override
public List<Map<String, Object>> getSessionViewByBeamlineOperator(String beamlineOperator) {
    Session session = (Session) this.entityManager.getDelegate();
    SQLQuery query = session.createSQLQuery(ByBeamlineOperator);
    query.setParameter("beamlineOperator", "%" + beamlineOperator + "%");
    System.out.println(query.getQueryString());
    return executeSQLQuery(query);
}

From source file:ispyb.server.mx.services.ws.rest.phasing.PhasingRestWsServiceBean.java

License:Open Source License

@Override
public List<Map<String, Object>> getPhasingViewByDataCollectionGroupId(int dataCollectionGroupId,
        int proposalId) {
    Session session = (Session) this.entityManager.getDelegate();
    SQLQuery query = session.createSQLQuery(ByDataCollectionGroupId);
    query.setParameter("dataCollectionGroupId", dataCollectionGroupId);
    query.setParameter("proposalId", proposalId);
    System.out.println(query.getQueryString());
    return executeSQLQuery(query);
}

From source file:ispyb.server.mx.services.ws.rest.phasing.PhasingRestWsServiceBean.java

License:Open Source License

@Override
public List<Map<String, Object>> getPhasingViewByDataCollectionId(int dataCollectionId, int proposalId) {
    Session session = (Session) this.entityManager.getDelegate();
    SQLQuery query = session.createSQLQuery(ByDataCollectionId);
    query.setParameter("dataCollectionId", dataCollectionId);
    query.setParameter("proposalId", proposalId);
    System.out.println(query.getQueryString());
    return executeSQLQuery(query);
}

From source file:lt.emasina.resthub.server.factory.DataFactory.java

License:Open Source License

public CcData getData(final Session session, final DataHandler handler) throws Exception {
    final Query q = handler.getQuery();
    final SQLQuery query = getPagedSQLQuery(session, handler);

    for (MdColumn c : q.getColumns()) {
        switch (c.getType()) {
        case BLOB:
            query.addScalar(c.getName(), new BlobType());
            break;
        case CLOB:
            query.addScalar(c.getName(), new ClobType());
            break;
        case DATE:
            query.addScalar(c.getName(), new CalendarType());
            break;
        case NUMBER:
            query.addScalar(c.getName(), new BigDecimalType());
            break;
        case STRING:
            query.addScalar(c.getName(), new StringType());
            break;
        }/*from  w w w. j  a va2 s.c  o  m*/
    }

    if (log.isDebugEnabled()) {
        log.debug(query.getQueryString());
    }

    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<CcData> loopRows = executor.submit(new Callable<CcData>() {

        @Override
        @SuppressWarnings("unchecked")
        public CcData call() throws Exception {
            CcData cc = new CcData();
            for (Object o : query.list()) {
                cc.addRow(q, o);
            }
            return cc;
        };
    });

    try {

        return loopRows.get(q.getTimeOut(), TimeUnit.SECONDS);

    } catch (ExecutionException | InterruptedException ex) {
        throw ex;
    } catch (TimeoutException ex) {
        throw new ServerErrorException(Status.SERVER_ERROR_GATEWAY_TIMEOUT, ex);
    }

}

From source file:lt.emasina.resthub.server.factory.DataFactory.java

License:Open Source License

public CcLob getLob(final Session session, final LobHandler handler) throws Exception {
    final Query q = handler.getQuery();
    final SQLQuery query = getPagedSQLQuery(session, handler);

    final MdColumn c = handler.getMdColumn();
    switch (c.getType()) {
    case BLOB:/*from  w w  w .  j  av a  2 s  .  c o m*/
        query.addScalar(c.getName(), new WrapperBinaryType());
        break;
    case CLOB:
        query.addScalar(c.getName(), new TextType());
        break;
    default:
        throw new ClientErrorException(Status.CLIENT_ERROR_BAD_REQUEST,
                "Column %d (%s) expected to be LOB found %s", handler.getColumn(), c.getName(),
                c.getType().name());
    }

    if (log.isDebugEnabled()) {
        log.debug(query.getQueryString());
    }

    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<CcLob> fetchData = executor.submit(new Callable<CcLob>() {

        @Override
        @SuppressWarnings("unchecked")
        public CcLob call() throws Exception {
            CcLob cc = new CcLob();
            Object o = query.uniqueResult();
            if (o != null) {
                switch (c.getType()) {
                case CLOB:
                    cc.setValue((String) o);
                    break;
                case BLOB:
                    cc.setValue((Byte[]) o);
                    break;
                }
            }
            return cc;
        };
    });

    try {

        return fetchData.get(q.getTimeOut(), TimeUnit.SECONDS);

    } catch (ExecutionException | InterruptedException ex) {
        throw ex;
    } catch (TimeoutException ex) {
        throw new ServerErrorException(Status.SERVER_ERROR_GATEWAY_TIMEOUT, ex);
    }

}

From source file:lt.emasina.resthub.server.factory.DataFactory.java

License:Open Source License

public CcCount getCount(Session session, CountHandler handler) throws SQLException {
    final Query q = handler.getQuery();

    StringBuilder sb = new StringBuilder();
    sb.append("select count(*) from (").append(handler.getQuery().getSql()).append(") ");

    String sql = sb.toString();//from   w  w w  . java  2s.  c  o  m

    final SQLQuery query = session.createSQLQuery(sql);

    handler.applyParameters(query);
    if (log.isDebugEnabled()) {
        log.debug(query.getQueryString());
    }

    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<CcCount> func = executor.submit(new Callable<CcCount>() {

        @Override
        public CcCount call() throws Exception {
            CcCount cc = new CcCount();
            cc.setValue(((BigDecimal) query.uniqueResult()).longValue());
            return cc;
        }

    });

    try {

        return func.get(q.getTimeOut(), TimeUnit.SECONDS);

    } catch (ExecutionException | InterruptedException ex) {
        throw new ServerErrorException(Status.SERVER_ERROR_INTERNAL, ex);
    } catch (TimeoutException ex) {
        throw new ServerErrorException(Status.SERVER_ERROR_GATEWAY_TIMEOUT, ex);
    }
}