Hibernate Criteria API equivalent to HQL selection condition?

I would like to have a combined query for two constant classes.

In HQL, this can be achieved with a select clause.

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

In the above example, Family is a unified class with DemesticCat as its constructor parameters

What is equivalent to the criterion of the proposal of the choice of HQL?

+5
source share
2 answers

For this you need to use ResultTransformer. The Hibernate 3.2: Transformers for HQL and SQL blog provides an example (where StudentDTOis a non-entity 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);  
+5
source

API Projections. , , .

+1

All Articles