hibernate纯sql查询结果集映射为DTO(VO,DO)
感谢glamey兄弟的文章,正好解决了当前遇到的问题。原文链接如下:http://glamey.iteye.com/blog/721019
假设我们现在有一个DTO,其属性包括两张表的属性,我们现在需要将sql语句查询得到的内容转为一个DTO对象,其解决方法如下:
String sql = "select u.userName as userName ,p.title as title ,p.addTime as addTime from user as u,post as p where u.id=p.userId" Query q = factory.getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(PostVO.class));
setResultTransformer(new VOResultTransformer(PostVO.class));
Query q = session.createQuery("select new com.hibernate.MsgInfo(m.id, m.cont, m.topic.title, m.topic.category.name) from Msg m");List<MsgInfo> list=q.list();