Complex Hibernate Projections

前端 未结 4 753
野的像风
野的像风 2020-12-18 08:06

I want to ask, it is possible that I create query projections and criterion for more than one level deep? I have 2 model classes:

@Entity  
@Table(name = \"p         


        
4条回答
  •  长情又很酷
    2020-12-18 08:29

    AFAIK it is not possible to project more than one level deep with aliastobean transformer. Your options are

    • create a flattened Data Transfer Object (DTO)
    • fill the resulting Person in memory yourself
    • implement your own resulttransformer (similar to option 2)

    option 1 looks like this:

    Criteria criteria = getHandlerSession().createCriteria(Person.class)
        .createAlias("wife", "wife", JoinType.LEFT.ordinal())
        .add(Restrictions.eq("wife.age", 19)); 
        .setProjection(Projections.projectionList()
            .add(Projections.property("personID"), "personID")
            .add(Projections.property("name"), "personName")
            .add(Projections.property("wife.name"), "wifeName"));
        .setResultTransformer(Transformers.aliasToBean(PersonWifeDto.class));
    
    return criteria.list();
    

提交回复
热议问题