Hibernate:如何将复杂查询的结果转换为一个类的列表

时间:2017-04-15 09:46:59

标签: hibernate

我有一个复杂的查询,如下所示

Select a.CustomerName, total = sum(b.count), tot1 = (c.count) from customer a, xyz b, stp c where <lots of joins, sub Queries, etc >

我需要将结果放入像

这样的类的实例中
class CustomerTotal {
    String customerName;
    Integer total;
    Integer total1;
}

有关如何实现上述目标的任何建议。

感谢

1 个答案:

答案 0 :(得分:0)

您可以在AliasToBeanResultTransformer使用 结果转换器,允许将结果转换为用户指定的类,该类将通过setter方法或与别名相匹配的字段填充。

您的查询将如下:

List<CustomerTotal> resultWithAliasedBean = s.createCriteria(Customer.class)
                     add here your required joins
                        .setProjection( Projections.projectionList()
            .add(Projections.property("a.CustomerName"), "customerName")
            .add(Projections.sum("b.count"), "total")
            .add(Projections.property("b.count"), "total1")
                        .setResultTransformer( new AliasToBeanResultTransformer(CustomerTotal.class) )
                        .list();

这里是AliasToBeanResultTransforme

的文档