Hibernate纯sql查询VO对象封装

  1. 云栖社区>
  2. 博客>
  3. 正文

Hibernate纯sql查询VO对象封装

易天行j 2017-02-13 10:29:00 浏览384
展开阅读全文

hibernate 纯sql查询返回结果集(未关联映射)组装VO的问题
//须保证别名字段与Vo字段一致

//引号中为vo对象属性需与sql查询返回字段一致
.addScalar("chname").setResultTransformer(Transformers.aliasToBean(Vo.class))

例:

@Override
public List<EmployeeGroupVo> selectLeaderEmpRef(String leid) {
String sql = "SELECT e.uid as uid,e.uid as eid,e.employee_name as chname,l.employee_id as leid,g.group_name as gname FROM t_employee as e,t_group as g,t_group_leader l where e.group_id=g.uid and l.group_id=g.uid";
Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql).addScalar("uid").addScalar("eid")
.addScalar("chname").addScalar("leid").addScalar("gname").setResultTransformer(Transformers.aliasToBean(EmployeeGroupVo.class));
List<EmployeeGroupVo> list = query.list();
return list;
}

网友评论

登录后评论
0/500
评论
易天行j
+ 关注