Java Code Examples for org.hibernate.Query#setResultTransformer()

The following examples show how to use org.hibernate.Query#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: BaseAbstractDAO.java    From oim-fx with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T queryUniqueResult(String queryString, QueryWrapper queryWrapper, Class<T> resultClass, List<ResultType> returnTypeList) {
	Session session = sessionBox.getCurrentSession();
	Query query = createSQLQuery(session, queryString);
	setParameter(query, queryWrapper);
	setScalar(query, returnTypeList);

	if (resultClass != null && !this.isPrimitive(resultClass) && !this.isString(resultClass)) {
		if (null == returnTypeList || returnTypeList.isEmpty()) {
			query.setResultTransformer(new ResultToBean(resultClass));
		} else {
			query.setResultTransformer(Transformers.aliasToBean(resultClass));
		}
	}
	if (null == resultClass) {
		query.setResultTransformer(new ResultToBean(resultClass));
	}
	Object value = query.uniqueResult();
	if (value instanceof BigInteger) {
		if (Long.class == resultClass) {
			value = ((BigInteger) value).longValue();
		}
	}
	return (T) value;
}
 
Example 2
Source File: ProfileDaoImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
	 * {@inheritDoc}
	 */
@Override
public List<UserProfile> getUserProfiles(final int start, final int count) {
	
	//get fields directly from the sakaiperson table and use Transformers.aliasToBean to transform into UserProfile pojo
	//the idea is we *dont* want a SakaiPerson object
	final HibernateCallback<List<UserProfile>> hcb = session -> {
           final Query q = session.getNamedQuery(QUERY_GET_SAKAI_PERSON);
           //see scalars in the hbm
           q.setFirstResult(start);
           q.setMaxResults(count);
           q.setResultTransformer(Transformers.aliasToBean(UserProfile.class));
           q.setCacheMode(CacheMode.GET);
           return q.list();
       };
  	
  	return getHibernateTemplate().execute(hcb);
}
 
Example 3
Source File: ProfileDaoImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
	 * {@inheritDoc}
	 */
@Override
public List<UserProfile> getUserProfiles(final int start, final int count) {
	
	//get fields directly from the sakaiperson table and use Transformers.aliasToBean to transform into UserProfile pojo
	//the idea is we *dont* want a SakaiPerson object
	final HibernateCallback<List<UserProfile>> hcb = session -> {
           final Query q = session.getNamedQuery(QUERY_GET_SAKAI_PERSON);
           //see scalars in the hbm
           q.setFirstResult(start);
           q.setMaxResults(count);
           q.setResultTransformer(Transformers.aliasToBean(UserProfile.class));
           q.setCacheMode(CacheMode.GET);
           return q.list();
       };
  	
  	return getHibernateTemplate().execute(hcb);
}
 
Example 4
Source File: PagingHibernateJdbcDao.java    From wenku with MIT License 6 votes vote down vote up
/**
 * 分页查询:查询结果集
 * @param sql 查询语句,参数用 ? 代替
 * @param args 查询参数
 * @param page 分页条件
 * @param clazz 返回domain实体的class
 * @return 分页实体
 */
@SuppressWarnings("unchecked")
public <T> Page<T> findPage(String sql, Object[] args, Pageable page, Class<T> clazz) {
    long count = count(sql, args);
    List<T> results;
    if (count == 0) {
        results = null;
    } else {
        Query query = createSQLQuery(sql);
        query = setParameters(query, args);
        query = setPageable(query, page);
        query.setResultTransformer(new EntityResultTransformer(clazz));
        results = query.list();
    }
    return wrapResult(results, page, count);
}
 
Example 5
Source File: PagingHibernateJdbcDao.java    From wenku with MIT License 6 votes vote down vote up
/**
 * 分页查询:查询结果集
 * @param sql 查询语句,参数用 ? 代替
 * @param args 查询参数
 * @param page 分页条件
 * @return 分页实体
 */
@SuppressWarnings("unchecked")
public Page<Map<String, Object>> findPage(String sql, Object[] args, Pageable page) {
    long count = count(sql, args);
    List<Map<String, Object>> results;
    if (count == 0) {
        results = null;
    } else {
        Query query = createSQLQuery(sql);
        query = setPageable(query, page);
        query = setParameters(query, args);
        query.setResultTransformer(new MapResultTransformer());
        results = query.list();
    }
    return wrapResult(results, page, count);
}
 
Example 6
Source File: BaseAbstractDAO.java    From oim-fx with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> List<T> queryList(String queryString, QueryWrapper queryWrapper, Class<T> resultClass, List<ResultType> returnTypeList) {

	Session session = sessionBox.getCurrentSession();
	List<T> list = null;

	Query query = createSQLQuery(session, queryString);
	setParameter(query, queryWrapper);
	if (null != returnTypeList) {
		setScalar(query, returnTypeList);
	}
	if (resultClass != null && !this.isPrimitive(resultClass) && !this.isString(resultClass)) {
		if (null == returnTypeList || returnTypeList.isEmpty()) {
			query.setResultTransformer(new ResultToBean(resultClass));
		} else {
			query.setResultTransformer(Transformers.aliasToBean(resultClass));
		}
	}

	if (null == resultClass) {
		query.setResultTransformer(new ResultToBean(resultClass));
	}

	list = query.list();
	if (null == list) {
		list = new ArrayList<>();
	}
	return list;
}
 
Example 7
Source File: TransformerImpl.java    From robe with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void setResultTransformer(Query query){
    switch (this.getTransformType()) {
        case MAP:
            query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
            break;
        case DTO:
            query.setResultTransformer(new AliasToBeanResultTransformer(this.getTransformClass(), this.getMeta()));
            break;
    }

}
 
Example 8
Source File: BaseHibernateDao.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param sql
 * @param <T> T
 * @return <br>
 */
public <T> List<T> queryBySql(final String sql) {

    Session session = getSession();
    session.flush();
    Query query = session.createSQLQuery(sql);

    if (getEntityClazz().equals(Map.class)) {
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    }
    else {
        query.setResultTransformer(new AutoResultTransformer(getEntityClazz()));
    }
    return query.list();
}
 
Example 9
Source File: PagingHibernateJdbcDao.java    From wenku with MIT License 5 votes vote down vote up
/**
 * 查询集合
 * @param sql 查询语句,参数用 ? 代替
 * @param args 查询参数
 * @return
 */
@SuppressWarnings("unchecked")
public List<Map<String, Object>> findListMap(String sql, Object[] args) {
    List<Map<String, Object>> results;
    Query query = createSQLQuery(sql);
    query = setParameters(query, args);
    query.setResultTransformer(new MapResultTransformer());
    results = query.list();
    return results;
}
 
Example 10
Source File: PagingHibernateJdbcDao.java    From wenku with MIT License 5 votes vote down vote up
/**
 * 查询集合
 * @param sql 查询语句,参数用 ? 代替
 * @param args 查询参数
 * @param clazz 返回domain实体的class
 * @return
 */
@SuppressWarnings("unchecked")
public <T> List<T> findList(String sql, Object[] args, Class<T> clazz) {
    List<T> results;
    Query query = createSQLQuery(sql);
    query = setParameters(query, args);
    query.setResultTransformer(new EntityResultTransformer(clazz));
    results = query.list();
    return results;
}
 
Example 11
Source File: PagingHibernateJdbcDao.java    From wenku with MIT License 5 votes vote down vote up
/**
 * 分页查询:查询结果集
 * @param sql 查询语句
 * @param page 分页条件
 * @return 分页实体
 */
@SuppressWarnings("unchecked")
public Page<Map<String, Object>> findPage(String sql, Pageable page) {
    long count = count(sql);
    List<Map<String, Object>> results;
    if (count == 0) {
        results = null;
    } else {
        Query query = createSQLQuery(sql);
        query = setPageable(query, page);
        query.setResultTransformer(new MapResultTransformer());
        results = query.list();
    }
    return wrapResult(results, page, count);
}
 
Example 12
Source File: PagingHibernateJdbcDao.java    From wenku with MIT License 5 votes vote down vote up
/**
 * 分页查询:查询结果集
 * @param sql 查询语句
 * @param page 分页条件
 * @param clazz 返回domain实体的class
 * @return 分页实体
 */
@SuppressWarnings("unchecked")
public <T> Page<T> findPage(String sql, Pageable page, Class<T> clazz) {
    long count = count(sql);
    List<T> results;
    if (count == 0) {
        results = null;
    } else {
        Query query = createSQLQuery(sql);
        query = setPageable(query, page);
        query.setResultTransformer(new EntityResultTransformer(clazz));
        results = query.list();
    }
    return wrapResult(results, page, count);
}
 
Example 13
Source File: QueryBuilder.java    From spring-data-jpa-extra with Apache License 2.0 5 votes vote down vote up
public static <C> Query transform(Query query, Class<C> clazz) {
    ResultTransformer transformer;
    if (Map.class.isAssignableFrom(clazz)) {
        transformer = Transformers.ALIAS_TO_ENTITY_MAP;
    } else if (Number.class.isAssignableFrom(clazz) || clazz.isPrimitive() || String.class.isAssignableFrom(clazz) ||
            Date.class.isAssignableFrom(clazz)) {
        transformer = transformerCache.computeIfAbsent(clazz, SmartTransformer::new);
    } else {
        transformer = transformerCache.computeIfAbsent(clazz, BeanTransformerAdapter::new);
    }
    return query.setResultTransformer(transformer);
}
 
Example 14
Source File: BaseAbstractDAO.java    From oim-fx with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> List<T> queryPageList(String queryString, QueryWrapper queryWrapper, Class<T> resultClass, List<ResultType> returnTypeList) {
	Session session = sessionBox.getCurrentSession();
	List<T> list = null;
	String queryCountSQL = wrapTotalSql(queryString);
	Query queryCount = createSQLQuery(session, queryCountSQL);
	setParameter(queryCount, queryWrapper);
	QueryPage page = queryWrapper.getPage();
	if (page == null) {
		page = new DefaultPage();
	}
	int totalCount = getCount(queryCount);
	if (0 < totalCount) {
		page.setTotalCount(totalCount);
		Query query = createSQLQuery(session, queryString);
		query.setFirstResult(page.getStartResult()); // 从第0条开始
		query.setMaxResults(page.getPageSize()); // 取出10条
		setParameter(query, queryWrapper);
		if (null != returnTypeList) {
			setScalar(query, returnTypeList);
		}
		if (resultClass != null && !this.isPrimitive(resultClass) && !this.isString(resultClass)) {
			if (null == returnTypeList || returnTypeList.isEmpty()) {
				query.setResultTransformer(new ResultToBean(resultClass));
			} else {
				query.setResultTransformer(Transformers.aliasToBean(resultClass));
			}
		}
		if (null == resultClass) {
			query.setResultTransformer(new ResultToBean(resultClass));
		}
		list = query.list();
	}
	if (null == list) {
		list = new ArrayList<T>();
	}
	page.setResultList(list);
	return list;
}
 
Example 15
Source File: SimpleHibernateDao.java    From DWSurvey with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Query distinct(Query query) {
	query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
	return query;
}
 
Example 16
Source File: BasicHibernateDao.java    From base-framework with Apache License 2.0 3 votes vote down vote up
/**
 * 为Query添加distinct transformer,讲查询出来的重复数据进行distinct处理
 * 
 * @param queryOrNamedQuery hql 或者Hibernate的NamedQuery
 * @param values 值
 * 
 * @return List
 */
public <X> List<X> distinct(String queryOrNamedQuery,Object... values) {
	Query query = createQuery(queryOrNamedQuery, values);
	query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
	
	return query.list();
}
 
Example 17
Source File: BasicHibernateDao.java    From base-framework with Apache License 2.0 3 votes vote down vote up
/**
 * 为Query添加distinct transformer,讲查询出来的重复数据进行distinct处理
 * 
 * @param queryOrNamedQuery hql 或者Hibernate的NamedQuery
 * @param values 值
 * 
 * @return List
 */
public <X> List<X> distinct(String queryOrNamedQuery,Map<String, Object> values) {
	Query query = createQuery(queryOrNamedQuery, values);
	query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
	
	return query.list();
}
 
Example 18
Source File: HibernateUtils.java    From lemon with Apache License 2.0 2 votes vote down vote up
/**
 * distinct.
 * 
 * @param query
 *            Query
 * @return Query
 */
public static Query distinct(Query query) {
    query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    return query;
}