Example usage for org.hibernate SQLQuery setResultTransformer

List of usage examples for org.hibernate SQLQuery setResultTransformer

Introduction

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

Prototype

@Deprecated
Query<R> setResultTransformer(ResultTransformer transformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:com.itrus.ca.common.persistence.BaseDaoImpl.java

License:Open Source License

/**
 * /*  w  w  w .j  a  v a  2s  .  c o m*/
 * 
 * @param query
 * @param resultClass
 */
private void setResultTransformer(SQLQuery query, Class<?> resultClass) {
    if (resultClass != null) {
        if (resultClass == Map.class) {
            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        } else if (resultClass == List.class) {
            query.setResultTransformer(Transformers.TO_LIST);
        } else {
            query.setResultTransformer(Transformers.aliasToBean(resultClass));
            // query.addEntity(resultClass);
        }
    }
}

From source file:com.jredrain.dao.HibernateDao.java

License:Apache License

/**
 * Query//  w ww . ja  va 2  s.c  om
 *
 * @param query
 * @param beanClass
 * @return
 */
private SQLQuery setQueryType(SQLQuery query, Class beanClass) {
    if (getClassMetadata(beanClass) != null) {
        query.addEntity(beanClass);
    } else {
        query.setResultTransformer(BeanResultTransFormer.get(beanClass));
    }
    return query;
}

From source file:com.lynn.dao.BaseDao.java

/**
 * SQL?List<Map>//from w w  w .j a  v a  2 s.c o  m
 * select id,name from member.
* @author WKX
* @param sql
* @param values
* @since 2016112 ?3:32:17
*/
public List<Map<String, Object>> getListMapBySQL(String sql, Object... values) {
    SQLQuery query = this.getSession().createSQLQuery(sql);
    if (values != null) {
        for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
        }
    }
    query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    List<Map<String, Object>> result = query.list();
    return result;
}

From source file:com.mtech.springsecurity.dao.AccountingPeriodDaoImpl.java

public Integer getMaxBatch(SMEEntity entity) {
    Session iSess = getSession();/*w w w .  j av  a 2 s .com*/
    SQLQuery query = iSess.createSQLQuery(
            "select max(ap.batchNumber) batchnumber from accounting_periods ap where ap.sme_id = :id");
    query.setParameter("id", entity.getId());
    query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
    List data = query.list();
    logger.warn("accountingperiod::" + data.size());
    Integer number = null;
    for (Object object : data) {
        java.util.Map row = (java.util.Map) object;
        System.out.print("Max number: " + row.get("batchnumber"));
        number = (Integer) row.get("batchnumber");
    }
    return number;
}

From source file:com.mtech.springsecurity.service.AnalyticsService.java

public String getSalesPerMonth(SMEEntity entity) {
    JSONArray jArray = new JSONArray();
    SQLQuery query = getSession()
            .createSQLQuery("select tab.end_date, tab.mon, coalesce(tab.sales, 0) vals from(\n"
                    + "select ap.end_date, to_char(ap.start_date, 'Mon') mon, sum(sales_value) sales\n"
                    + "    from accounting_periods ap\n"
                    + "        left join sales sa on to_char(sa.sale_date, 'Mon') = to_char(ap.start_date, 'Mon') \n"
                    + "    where ap.batchnumber = " + apDao.getMaxBatch(entity) + "\n"
                    + "group by ap.end_date, to_char(ap.start_date, 'Mon') \n" + ") as tab order by end_date");
    query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
    List data = query.list();/*  ww w. j a  v  a  2  s.c  o m*/
    logger.warn("accountingperiod::" + data.size());
    for (Object object : data) {
        JSONObject jsonObject = new JSONObject();
        java.util.Map row = (java.util.Map) object;
        jsonObject.put("label", row.get("mon"));
        jsonObject.put("value", row.get("vals"));
        jArray.add(jsonObject);
    }
    String salesPerProduct = getSalesPerProduct(entity);
    JSONObject wrapperObject = new JSONObject();
    wrapperObject.put("data", jArray);
    wrapperObject.put("product", salesPerProduct);
    wrapperObject.put("success", true);
    return wrapperObject.toString();
}

From source file:com.mtech.springsecurity.service.AnalyticsService.java

public String getSalesPerProduct(SMEEntity entity) {
    JSONArray jArray = new JSONArray();
    SQLQuery query = getSession()
            .createSQLQuery("select st.stock_name, sum(sa.sales_value) sales_value  from stocks st\n"
                    + "    inner join sales sa on sa.stock_id = st.id\n" + "group by st.stock_name");
    query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
    List data = query.list();//w  w  w.  j av a2 s  .com
    logger.warn("accountingperiod::" + data.size());
    for (Object object : data) {
        JSONObject jsonObject = new JSONObject();
        java.util.Map row = (java.util.Map) object;
        jsonObject.put("label", row.get("stock_name"));
        jsonObject.put("value", row.get("sales_value"));
        jArray.add(jsonObject);
    }

    return jArray.toString();
}

From source file:com.mysema.query.jpa.hibernate.sql.AbstractHibernateSQLQuery.java

License:Apache License

private Query createQuery(String queryString) {
    logQuery(queryString);//from   w ww. ja va 2  s .c om
    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.poka.dao.impl.BaseDao.java

@Override
public List<T> findBySql(String sql) {
    Transaction transaction = null;/*w ww  .  j a  va  2  s  .com*/
    transaction = getCurrentSession().beginTransaction();
    SQLQuery q = getCurrentSession().createSQLQuery(sql);
    List<T> myList = q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    transaction.commit();
    return myList;
}

From source file:com.poka.dao.impl.BaseDao.java

@Override
public List<T> findBySql(String sql, Map<String, Object> params) {
    Transaction transaction = null;/* w  w w  .  j a  v a  2  s.  co m*/
    transaction = getCurrentSession().beginTransaction();
    SQLQuery q = getCurrentSession().createSQLQuery(sql);
    if (params != null && !params.isEmpty()) {
        for (String key : params.keySet()) {
            q.setParameter(key, params.get(key));
        }
    }
    List<T> list = q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    transaction.commit();
    return list;
}

From source file:com.ps.dao.AulaDao.java

@Override
public List<Map<String, ?>> ListarAulaTutor(String idUsuario) {
    List<Map<String, ?>> lista = new ArrayList<>();
    sql = "SELECT * FROM PSV_AULA\n" + "WHERE IDAULA=(SELECT IDAULA \n" + "FROM DET_AULA_USUARIO\n"
            + "WHERE IDUSUARIO='" + idUsuario + "')" + "AND IDROL='RO-201500002'";
    session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {//from  w  w  w  .j  a v  a 2s .c  o  m
        trans = session.beginTransaction();
        SQLQuery result = session.createSQLQuery(sql);
        result.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        List data = result.list();
        for (Object obj : data) {
            Map<String, Object> a = (Map<String, Object>) obj;
            lista.add(a);
        }
        trans.commit();
    } catch (Exception e) {
        System.out.println("ERROR -----------" + e);
        trans.rollback();
    }

    return lista;
}