Hibernate:通过HQL填充UnMapped组件

时间:2014-07-01 10:58:09

标签: java hibernate hql

A类和B类是映射到其自己的数据库表的实体。 C类未映射并包含A和B的所有属性。是否可以构建HQL来填充C?

1 个答案:

答案 0 :(得分:2)

是的,你可以。您可以使用Hibernates ResultTransformer

List resultWithAliasedBean = s.createQuery(
  "select e.student.name as studentName," +
  "       e.course.description as courseDescription" +
  "from   Enrolment as e")
  .setResultTransformer( Transformers.aliasToBean(StudentDTO.class))
  .list();

StudentDTO dto = (StudentDTO) resultWithAliasedBean.get(0);