我想对两个持久性类进行合并查询。

在HQL中,这可以通过select子句来实现,

select new Family(mother, mate, offspr)
    from DomesticCat as mother
        join mother.mate as mate
        left join mother.kittens as offspr

在上面的示例中,Family是一个以DemesticCat作为其构造器参数的组合类

HQL select子句的Criteria等效项是什么?

最佳答案

为此,您必须使用 ResultTransformer Hibernate 3.2: Transformers for HQL and SQL博客文章给出了以下示例(其中StudentDTO是非实体Bean):

List resultWithAliasedBean = s.createCriteria(Enrolment.class)
  .createAlias("student", "st").createAlias("course", "co")
  .setProjection( Projections.projectionList()
                   .add( Projections.property("st.name"), "studentName" )
                   .add( Projections.property("co.description"), "courseDescription" )
          )
          .setResultTransformer( Transformers.aliasToBean(StudentDTO.class) )
          .list();

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

10-07 20:33