让Hibernate Query返回组织好的VO对象列表
举例说明:
?
数据库中有表:student(studentid, studentname, age)
?? ? ? ? ? ? ? 表:class(classid, classname, studentid)
?
程序中有VO:StudentInfo(studentid, studentname, age, classname) ? (假设StudentInfo在com.test包中)
?
在不使用hibernate做关系映射的前提下(抛弃了hibernate的精髓,做法不可取),想通过一个HQL直接返回一个已经组织好的List<StudentInfo>,比较精辟的做法是:
?
DAO中:String sql = "select new com.test.StudentInfo(s, c.classname)";sql += " from Student s, Class c where s.studentid=c.studentid";Session session = this.getSession();Query query = session.createQuery(sql);return query.list();?
?
StudentInfo中肯定要做一下准备,写一个合适的构造方法:
?
public StudentInfo(Student s, String classname) { this.studentid = s.studentid; this.studentname = s.studentname; this.age = s.age; this.classname = classname;}?
这样就可以了。
?
另外:
上面HQL的做法详见:hibernate文档的[14.4 select子句]
?
?
?
?