天天看點

JPA 查詢原生SQL 轉換VO對象

List<String> sqlList = RiverCourseSql.getRiverCourseSQLString(new RiverCourseDataQO());
        List<RiverCourseDataVO> riverCourseDataVO = rsvrfsrBDao.executeNativeQuery2Obj(sqlList.get(0), new PageQO(), RiverCourseDataVO.class);
           

查詢sql,然後執行功能方法。

<E> List<E> executeNativeQuery2Obj(String sql, SortQO sortQO, Class<E> clz);

public <E> List<E> executeNativeQuery2Obj(String sql, SortQO sortQO, Class<E> clz) {
        Query query = this.entityManager.createNativeQuery(sql);
        this.setAliasParameter(query, sortQO);
        ((NativeQueryImpl)query.unwrap(NativeQueryImpl.class)).setResultTransformer(Transformers.aliasToBean(clz));
        List<E> resultList = query.getResultList();
        return resultList;
    }