Java Code Examples for org.hibernate.SQLQuery#setResultTransformer()
The following examples show how to use
org.hibernate.SQLQuery#setResultTransformer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseDao.java From wetech-cms with MIT License | 6 votes |
public <N extends Object>Pager<N> findBySql(String sql, Object[] args, Map<String, Object> alias, Class<?> clz, boolean hasEntity) { sql = initSort(sql); String cq = getCountHql(sql,false); SQLQuery sq = getSession().createSQLQuery(sql); SQLQuery cquery = getSession().createSQLQuery(cq); setAliasParameter(sq, alias); setAliasParameter(cquery, alias); setParameter(sq, args); setParameter(cquery, args); Pager<N> pages = new Pager<N>(); setPagers(sq, pages); if(hasEntity) { sq.addEntity(clz); } else { sq.setResultTransformer(Transformers.aliasToBean(clz)); } List<N> datas = sq.list(); pages.setDatas(datas); long total = ((BigInteger)cquery.uniqueResult()).longValue(); pages.setTotal(total); return pages; }
Example 2
Source File: BaseDaoImpl.java From SpringCloud with Apache License 2.0 | 6 votes |
public <N extends Object> List<N> listBySql(String sql, Object[] args, Map<String, Object> alias, Class<?> clz, boolean hasEntiry) { sql = initSort(sql); SQLQuery sq = getSession().createSQLQuery(sql); setAliasParameter(sq, alias); setParameter(sq, args); if (hasEntiry) { sq.addEntity(clz); } else { sq.setResultTransformer(Transformers.aliasToBean(clz)); } return sq.list(); }
Example 3
Source File: CustomBaseSqlDaoImpl.java From tianti with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public List<Map<String, Object>> querySqlObjects(String sql, Object params, Integer currentPage,Integer rowsInPage){ Query qry = em.createNativeQuery(sql); SQLQuery s = qry.unwrap(SQLQuery.class); //设置参数 if(params != null){ if(params instanceof List){ List<Object> paramList = (List<Object>) params; for(int i = 0, size = paramList.size(); i < size; i++){ qry.setParameter(i+1, paramList.get(i)); } }else if(params instanceof Map){ Map<String, Object> paramMap = (Map<String, Object>) params; for(String key : paramMap.keySet()){ qry.setParameter(key, paramMap.get(key)); } } } if (currentPage != null && rowsInPage != null) {//判断是否有分页 // 起始对象位置 qry.setFirstResult(rowsInPage * (currentPage - 1)); // 查询对象个数 qry.setMaxResults(rowsInPage); } s.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP); List<Map<String, Object>> resultList=new ArrayList<Map<String, Object>>(); try { resultList=s.list(); } catch (Exception e) { }finally{ em.close(); } return resultList; }
Example 4
Source File: CustomBaseSqlDaoImpl.java From tianti with Apache License 2.0 | 5 votes |
/** * 根据SQL语句查询分页 * @param sql * @param currentPage * @param pageSize * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public PageModel queryForPageBySql(String sql,Integer currentPage,Integer pageSize){ PageModel page = new PageModel(); Integer totalCount = 0; Integer totalPage = 0; //EntityManager em = this.emf.createEntityManager(); Query qry = em.createNativeQuery(sql); Query qry2 = em.createNativeQuery(sql); SQLQuery s = qry.unwrap(SQLQuery.class); if (currentPage != null && pageSize != null) {//判断是否有分页 // 起始对象位置 qry.setFirstResult(pageSize * (currentPage - 1)); // 查询对象个数 qry.setMaxResults(pageSize); } s.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP); List<Map> resultList=new ArrayList<Map>(); List list = qry2.getResultList(); totalCount = (list == null ? 0 : list.size()); if(totalCount % pageSize == 0){ totalPage = totalCount/pageSize; }else{ totalPage = totalCount/pageSize + 1; } try { resultList=s.list(); page.setCurrentPage(currentPage); page.setPageSize(pageSize); page.setList(resultList); page.setTotalCount(totalCount); page.setTotalPage(totalPage); } catch (Exception e) { e.printStackTrace(); }finally{ em.close(); } return page; }
Example 5
Source File: BaseDao.java From wetech-cms with MIT License | 5 votes |
public <N extends Object>List<N> listBySql(String sql, Object[] args, Map<String, Object> alias, Class<?> clz, boolean hasEntity) { sql = initSort(sql); SQLQuery sq = getSession().createSQLQuery(sql); setAliasParameter(sq, alias); setParameter(sq, args); if(hasEntity) { sq.addEntity(clz); } else sq.setResultTransformer(Transformers.aliasToBean(clz)); return sq.list(); }
Example 6
Source File: BaseDaoImpl.java From SpringCloud with Apache License 2.0 | 5 votes |
public <N extends Object> Pager<N> findBySql(String sql, Object[] args, Map<String, Object> alias, Class<?> clz, boolean hasEntiry) { sql = initSort(sql); String cq = getCountHql(sql, false); // cq=initSort(cq);不需要再加上就多了一个Order SQLQuery sq = getSession().createSQLQuery(sql); SQLQuery cquery = getSession().createSQLQuery(cq); setAliasParameter(sq, alias); setAliasParameter(cquery, alias); setParameter(sq, args); setParameter(cquery, args); Pager<N> pages = new Pager<N>(); setPagers(sq, pages); if (hasEntiry) { sq.addEntity(clz); } else { sq.setResultTransformer(Transformers.aliasToBean(clz)); } List<N> datas = sq.list(); pages.setDatas(datas); long total = ((BigInteger) cquery.uniqueResult()).longValue(); pages.setTotal(total); return pages; }
Example 7
Source File: BaseDao.java From JgFramework with Apache License 2.0 | 5 votes |
protected SQLQuery getQuery(String queryString, Map<String, Object> where, int start, int limit, boolean returnMap) { Session session = this.getSession(); SQLQuery query = null; if (start > -1 && limit > 0) { query = session.createSQLQuery(queryString); query.setFirstResult(start); query.setMaxResults(limit); } else { query = session.createSQLQuery(queryString); } if (where != null) { for (Entry<String, Object> entry : where.entrySet()) { Object value = entry.getValue(); String key = entry.getKey(); if (value instanceof Collection) { query.setParameterList(key, (Collection) value); } else if (value instanceof Object[]) { query.setParameterList(key, (Object[]) value); } else { query.setParameter(key, value); } } } if (returnMap) { query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); } return query; }
Example 8
Source File: CustomBaseSqlDaoImpl.java From tianti with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) public PageModel queryForPageBySql(String sql,Map<String,Object> params,Integer currentPage,Integer pageSize){ PageModel page = new PageModel(); Integer totalCount = 0; Integer totalPage = 0; Query qry = em.createNativeQuery(sql); Query qry2 = em.createNativeQuery(sql); for(Map.Entry<String,Object> entry: params.entrySet()){ qry.setParameter(entry.getKey(), entry.getValue()); qry2.setParameter(entry.getKey(), entry.getValue()); } SQLQuery s = qry.unwrap(SQLQuery.class); if (currentPage != null && pageSize != null) {//判断是否有分页 // 起始对象位置 qry.setFirstResult(pageSize * (currentPage - 1)); // 查询对象个数 qry.setMaxResults(pageSize); } s.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP); List<Map> resultList=new ArrayList<Map>(); List list = qry2.getResultList(); totalCount = (list == null ? 0 : list.size()); if(totalCount % pageSize == 0){ totalPage = totalCount/pageSize; }else{ totalPage = totalCount/pageSize + 1; } try { resultList=s.list(); page.setCurrentPage(currentPage); page.setPageSize(pageSize); page.setList(resultList); page.setTotalCount(totalCount); page.setTotalPage(totalPage); } catch (Exception e) { e.printStackTrace(); }finally{ em.close(); } return page; }