带投影的Hibernate条件不会返回实现条件的实体

Rit*_*tes 7 spring hibernate criteria

我正在使用spring-hibernate并使用HibernateDAOSupport类.我有两个表以一对多的方式相互映射.我正在实施以下标准

 DetachedCriteria criteria = getCriteria( "a" )
        .setProjection( Projections.projectionList()
                .add( Projections.groupProperty("a.id" ) )
                .add( Projections.count( "a.id" ), "count" )
                )
        .createCriteria( "huApps", "hu")
        .addOrder( Order.desc( "count" ) )
        ;
Run Code Online (Sandbox Code Playgroud)

这很好用,并创建以下查询

select
        this_.id as y0_,
        count(this_.id) as y1_ 
    from
        apps this_ 
    inner join
        huapps huapp1_ 
            on this_.id=huapp1_.appid 
    group by
        this_.id 
    order by
        y1_ desc
Run Code Online (Sandbox Code Playgroud)

结果,它返回一个列表object[].但我希望它应该返回List<App>(App是我实现/创建标准的类).我希望它会创建查询

select
 this_
    from
        apps this_ 
    inner join
        huapps huapp1_ 
            on this_.id=huapp1_.appid 
    group by
        this_.id 
    order by
        y1_ desc
Run Code Online (Sandbox Code Playgroud)

请帮我写出正确的标准.我也尝试过,sqlProjection()但即使这样也行不通.有什么办法可以实现吗?

Yap*_*pie 1

您尝试为 function 的结果添加新的条件detachedCriteria.createCriteria("huApps", "hu")。该函数返回 huApp 属性类的新标准。

尝试替换您的标准,如下所示:

DetachedCriteria detachedCriteria = DetachedCriteria.forClass(A.class);
detachedCriteria.setProjection(Projections.projectionList()
            .add(Projections.groupProperty("id"))
            .add(Projections.count("id"), "count")
    );

detachedCriteria.createCriteria("huApps", "hu");
detachedCriteria.addOrder(Order.desc("count"));

List<A> list = detachedCriteria.getExecutableCriteria(getSession()).list();
Run Code Online (Sandbox Code Playgroud)

这对我来说很有效。