本文整理匯總了Java中org.hibernate.SQLQuery.setResultTransformer方法的典型用法代碼示例。如果您正苦於以下問題:Java SQLQuery.setResultTransformer方法的具體用法?Java SQLQuery.setResultTransformer怎麽用?Java SQLQuery.setResultTransformer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.hibernate.SQLQuery
的用法示例。
在下文中一共展示了SQLQuery.setResultTransformer方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: querySqlObjects
import org.hibernate.SQLQuery; //導入方法依賴的package包/類
@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;
}
示例2: addSclar
import org.hibernate.SQLQuery; //導入方法依賴的package包/類
/**
* 將field type 和 Hibernate的類型進行了對應。這裏其實不是多餘的,如果不進行一定的對應可能會有問題。 問題有兩個: 1.
* 在oracle中我們可能把一些字段設為NUMBER(%),而在Bean中的字段定的是long。那麽查詢時可能會報:
* java.math.BeigDecimal不能轉換成long等錯誤 2.
* 如果不這樣寫的話,可能Bean中的field就得是大寫的,如:name就得寫成NAME,userCount就得寫成USERCOUNT
* 這樣是不是很扯(V_V)
*
* @param <T>
* @param sqlQuery
* SQLQuery
* @param clazz
* T.class
*/
public static <T> void addSclar(SQLQuery sqlQuery, Class<T> clazz) {
if (clazz == null) {
throw new NullPointerException("[clazz] could not be null!");
}
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if ((field.getType() == long.class) || (field.getType() == Long.class)) {
sqlQuery.addScalar(field.getName(), StandardBasicTypes.LONG);
} else if ((field.getType() == int.class) || (field.getType() == Integer.class)) {
sqlQuery.addScalar(field.getName(), StandardBasicTypes.INTEGER);
} else if ((field.getType() == char.class) || (field.getType() == Character.class)) {
sqlQuery.addScalar(field.getName(), StandardBasicTypes.CHARACTER);
} else if ((field.getType() == short.class) || (field.getType() == Short.class)) {
sqlQuery.addScalar(field.getName(), StandardBasicTypes.SHORT);
} else if ((field.getType() == double.class) || (field.getType() == Double.class)) {
sqlQuery.addScalar(field.getName(), StandardBasicTypes.DOUBLE);
} else if ((field.getType() == float.class) || (field.getType() == Float.class)) {
sqlQuery.addScalar(field.getName(), StandardBasicTypes.FLOAT);
} else if ((field.getType() == boolean.class) || (field.getType() == Boolean.class)) {
sqlQuery.addScalar(field.getName(), StandardBasicTypes.BOOLEAN);
} else if (field.getType() == String.class) {
sqlQuery.addScalar(field.getName(), StandardBasicTypes.STRING);
} else if (field.getType() == Date.class) {
sqlQuery.addScalar(field.getName(), StandardBasicTypes.TIMESTAMP);
}
}
sqlQuery.setResultTransformer(Transformers.aliasToBean(clazz));
}
示例3: queryForPageBySql
import org.hibernate.SQLQuery; //導入方法依賴的package包/類
/**
* 根據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;
}
示例4: queryForMap
import org.hibernate.SQLQuery; //導入方法依賴的package包/類
@Override
public Map queryForMap(String querySql, Object... params) {
Query query = entityManager.createNativeQuery(querySql);
SQLQuery nativeQuery = query.unwrap(SQLQuery.class);
nativeQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
if (params != null) {
for (int i = 0; i < params.length; i++) {
nativeQuery.setParameter(i, params[i]);
}
}
return (Map) nativeQuery.uniqueResult();
}
示例5: queryForMapList
import org.hibernate.SQLQuery; //導入方法依賴的package包/類
@Override
public List<Map> queryForMapList(String querySql, Object... params) {
Query query = entityManager.createNativeQuery(querySql);
SQLQuery nativeQuery = query.unwrap(SQLQuery.class);
nativeQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
if (params != null) {
for (int i = 0; i < params.length; i++) {
nativeQuery.setParameter(i, params[i]);
}
}
return nativeQuery.list();
}